安装cloudinit工具

1
apt install cloud-utils

启动虚拟机,以CentOS为例

镜像下载地址:

https://cloud.centos.org/centos/7https://img-cft.captainz.cc/hexo/

命令:

1
touch /home/vhost/centos-cloud/centos-cloudinit.cfg

填入:

1
2
3
4
5
6
7
8
9
10
11
12
13
#cloud-config
ssh_authorized_keys:
- ssh-rsa AAA3NzaCDAQABA....... xxx@xxx
hostname: centos
manage_etc_hosts: true
user: root
ssh_pwauth: 1
disable_root: 0
password: passwd
chpasswd:
expire: False
runcmd:
- echo root:passwd |sudo chpasswd root && sudo sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config && sudo sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config && sudo service sshd restart

命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
cloud-localds  /home/vhost/centos-cloud/cloud.img /home/vhost/centos-cloud/centos-cloudinit.cfg

virt-install \
--name centos7 \
--os-type Linux --os-variant centos7.0 \
--hvm \
--memory 1024 \
--vcpus 2 \
--network bridge=br0,model=virtio \
--cdrom=/home/vhost/centos-cloud/cloud.img \
--disk /home/vhost/centos-cloud/CentOS-7-x86_64-GenericCloud.qcow2,bus=virtio \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole

直接可以用ssh客户端以 root@passwd 登入。