使用 virt-edit 离线修改 Cloud-Init 配置文件

首先安装 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

相关文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注