首先安装 libguestfs-tools
apt install libguestfs-tools -y
下载你的镜像,这里以 Debian 12 官方的 Cloud 为例。
# 启用 root 登录
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/cloud/cloud.cfg -e 's/disable_root: [Tt]rue/disable_root: False/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/cloud/cloud.cfg -e 's/disable_root: 1/disable_root: 0/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/ssh/sshd_config -e 's/PermitRootLogin [Nn]o/PermitRootLogin yes/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/ssh/sshd_config -e 's/#PermitRootLogin [Yy]es/PermitRootLogin yes/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/ssh/sshd_config -e 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/'
# 启用密码登录
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/cloud/cloud.cfg -e 's/lock_passwd: [Tt]rue/lock_passwd: False/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/cloud/cloud.cfg -e 's/lock_passwd: 1/lock_passwd: 0/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/cloud/cloud.cfg -e 's/ssh_pwauth: 0/ssh_pwauth: 1/'
virt-edit -a debian-12-genericcloud-amd64-20231013-1532.qcow2 /etc/ssh/sshd_config -e 's/PasswordAuthentication no/PasswordAuthentication yes/'
# 安装常用工具
virt-customize --install cloud-init,htop,nano,vim,qemu-guest-agent,cron,curl,wget,screen,git -a debian-12-genericcloud-amd64-20231013-1532.qcow2