以ubuntu 虚拟机为例,很多时候创建了KVM虚拟机之后,想ssh登录进去做一些操作,但是却不知道用户名和密码,最早的时候ubuntu 的cloud image 内置了一个cirros的用户名,密码也是cirros,现在好像没有这个用户了,所以想ssh 虚拟机的话,可以借助cloudinit 来在启动时创建一个指定的用户。
以下以ubuntu22.04为例说明一下操作步骤
1 下载对应os 架构image
wget https://cloud-images.ubuntu.com/releases/22.04/release-20240319/ubuntu-22.04-server-cloudimg-amd64.img
2 创建cloud-init.cfg 配置文件,设置一个默认用户ubuntu设置密码为password
root@test# cat cloud-init.cfg #cloud-config system_info: default_user: name: ubuntu / home: /home/ubuntu password: password chpasswd: { expire: False } hostname: ubuntu # configure sshd to allow users logging in using password # rather than just keys ssh_pwauth: True
3 生成cloud-init disk
cloud-localds cloud-init.iso cloud-init.cfg
4 启动虚拟机
root@test:# virt-install --connect qemu:///system --name test-vm --memory 2048 --vcpus 2 --disk ubuntu-22.04-server-cloudimg-amd64.img --import --os-variant ubuntu22.04 --noautoconsole --network network=default,model=virtio --graphics none --console pty,target_type=serial --disk data/ubuntu/cloud-init.iso,device=cdrom root@test# virsh list Id Name State ----------------------------- 24 test-vm running root@s11:~/nf# virsh console 24 Connected to domain 'test-vm' Escape character is ^] (Ctrl + ]) Ubuntu 22.04.4 LTS ubuntu ttysclp0 ubuntu login: ubuntu Password: <>
5 登录进虚拟机后,找到vm的IP
ubuntu@ubuntu:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enc1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:37:f5:86 brd ff:ff:ff:ff:ff:ff inet 192.168.122.178/24 metric 100 brd 192.168.122.255 scope global dynamic enc1 valid_lft 3562sec preferred_lft 3562sec inet6 fe80::5054:ff:fe37:f586/64 scope link valid_lft forever preferred_lft forever
6.用ssh 登录虚拟机
root@test# ssh [email protected] The authenticity of host '192.168.122.178 (192.168.122.178)' can't be established. ED25519 key fingerprint is SHA256:un8GId28KD2GbMoxQkE2jJJEQRPFXbSlBZ69LEkd57o. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.122.178' (ED25519) to the list of known hosts. [email protected]'s password: Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-101-generic s390x) ubuntu@ubuntu:~$
Tips: 退出console时,用Ctrl+C或exit都退不出来,要用Ctrl+] (右中括号) 就可以了
参考:https://www.cnblogs.com/even160941/p/17605840.html
标签:00,自定义,--,虚拟机,KVM,lft,ubuntu,cloud From: https://www.cnblogs.com/happyorange/p/18145966