openstack-虚机实现可以ssh 登陆的方法

问题: 在云环境中用户上传了一个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:~#

原文地址:https://www.cnblogs.com/yk0625/p/14092078.html