問題: 在雲環境中用戶上傳了一個image 鏡像,但是不清楚鏡像的用戶名和密碼,無法ssh 登陸到虛機內部。嘗試通過openstack keypair 方式配置后,也是無法免密登陸到虛機。
解決過程及方法記錄:
1. 通過keypair創建出虛機可以ping通,ssh 登錄失敗
nova boot --flavor 1104 --image volume_vm_img-nfs-1 --nic net-id=964f8071-70ac-4d27-92ca-673135568e35 --availability-zone az1:ca-compute-0-2.internal.ca.fuel --key-name nfs_key nfs-server-1

通過密鑰登陸提示拒絕

2. 通過nova show + 虛機名 可以查看到虛機創建在那台節點,虛機的實例名

3. 登錄虛機所在的計算節點,查詢虛機,准備設置讓虛機可以virsh console
compute-0-2:~# virsh list
Id Name State
----------------------------------------------------
11 instance-00000371 running
12 instance-0000036e running
15 instance-0000037d running
17 instance-00000380 running
4. 先暫停計算節點的nova-compute 服務,關閉虛機
root@compute-0-2:~# service nova-compute stop
root@compute-0-2:~# virsh destroy instance-00000380
Domain instance-00000380 destroyed
5.編輯實例的xml 文件
root@compute-0-2:~# virsh edit instance-00000380
將圖1 的白色部分內容放置圖2 位置(,放在</interface>下,<serial type='file'>上)保存退出


6.重新定義實例的xml 文件,啟動實例
root@compute-0-2:~# ls /etc/libvirt/qemu/
instance-0000036e.xml instance-00000371.xml instance-0000037d.xml instance-00000380.xml networks
root@compute-0-2:~# virsh define /etc/libvirt/qemu/instance-00000380.xml
Domain instance-00000380 defined from /etc/libvirt/qemu/instance-00000380.xml
root@compute-0-2:~# virsh start instance-00000380
Domain instance-00000380 started
7. Console 登錄到虛機里,修改配置 /etc/ssh/sshd_config 文件,設置運行root登陸
root@compute-0-2:~# virsh console instance-00000380
Connected to domain instance-00000380
Escape character is ^]
root@host-192-168-20-12:~# vim /etc/ssh/sshd_config
...
#PermitRootLogin without-password
PermitRootLogin yes
PasswordAuthentication yes
PermitEmptyPasswords yes
...
修改設置root 密碼(r00tme)
root@host-192-168-20-12:~# passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
重啟ssh服務
root@host-192-168-20-12:~# systemctl restart ssh.service
Ctrl + ] 退出登錄
8. 在compute節點上恢復nova-compute 服務
root@compute-0-2:~# service nova-compute start
通過命名空間登錄虛機 Ip netns list 查看當前的命名空間 虛機使用的是那個net-id 我們登錄用相對應的命名空間(qdhcp-(net-id))
qdhcp-964f8071-70ac-4d27-92ca-673135568e35
root@control-node:~# ip netns exec qdhcp-964f8071-70ac-4d27-92ca-673135568e35 ssh 192.168.20.12
root@192.168.20.12's password:
Last login: Mon May 11 21:38:49 2020
root@host-192-168-20-12:~#
