pve创建ubuntu cloud虚拟机模版

使用命令创建虚拟机

pve主机中执行命令

# 下载ubuntu22 cloudimg镜像文件
wget https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img

# 定义虚拟机id为9001
VM_ID=9001

# 创建2核2G的虚拟机
qm create $VM_ID --memory 2048 --core 2 --name ubuntu22 --net0 virtio,bridge=vmbr0

# 导入下载的镜像,存储在local-lvm
qm importdisk $VM_ID ubuntu-22.04-server-cloudimg-amd64.img local-lvm

# 设置scsi控制器和磁盘
qm set $VM_ID --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-$VM_ID-disk-0

# 磁盘设置为10GB
qm resize ${VM_ID} scsi0 10G

# 添加cloud-init设备
qm set $VM_ID --ide0 local-lvm:cloudinit

# 设置引导
qm set $VM_ID --boot c --bootdisk scsi0

# 用qemu代理
# qm set ${VM_ID} --agent enabled=1

# 配置默认cloud-init:自动获取ip、关闭首次自动升级、root用户密码
qm set $VM_ID --ipconfig0 ip=dhcp --ciupgrade 0 --ciuser "root" --cipassword "root"

设置模版前,可以先启动虚拟机进行修改软件源、时区等操作

使用vnc连接虚拟机执行命令

 # 开启root用户使用密码连接ssh(vnc中无法粘贴,就手动修改以下两条)
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config​
 
 # 重启ssh服务
service sshd restart
 
# 更换apt源为中科大源
sed -i 's#http://archive.ubuntu.com#https://mirrors.ustc.edu.cn#g' /etc/apt/sources.list
sed -i 's#http://security.ubuntu.com#https://mirrors.ustc.edu.cn#g' /etc/apt/sources.list

# 更新软件
apt update
apt upgrade

# 设置时区为上海
timedatectl set-timezone Asia/Shanghai

# 清理machine-id,若不清理,所有克隆的虚机会获取相同ip
cp -f /dev/null /etc/machine-id

# 清理历史命令
history -c

将虚拟机设置成模版

pve主机上执行命令

qm template ${VM_ID}

你可能感兴趣的:(ubuntu,运维,pve)