openstack keypair实践

来源:互联网 发布:汉口北淘宝商学院 编辑:程序博客网 时间:2024/06/02 00:33

登陆OS系统可以使用密码登录或者密钥登陆,密码登录是比较常见的一种方式,用户在操作系统中创建了用户名和密码,就可以使用自己的用户名和密码登录进去了;使用密钥登陆是另外一种方式,即用户创建一个密钥对(公钥和私钥),然后将公钥注入到OS中,在登陆时使用私钥校验,校验通过后就可以登入到OS。OpenStack提供了密钥登陆的功能,可以采用密钥注入或者文件注入的方式将公钥注入到OS中,本文采用了密钥注入的方式,以下是操作步骤。

1.生成密钥对

#cd /root/.ssh/

#ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key(/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase):   #Enter回车

Enter same passphrase again:  #Enter回车

Your identification has been saved in/root/.ssh/id_rsa. #Enter回车

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

a0:c6:df:8c:1f:a6:57:62:70:3f:4d:b7:cc:e4:5f:71 [MD5]root@08592946-A8B7-11A5-8567-000000821800

The key's randomart image is:

+--[ RSA 2048]----+

|                |

|                |

|      .         |

|   . ....   . o.E|

|    +  oS. o * .o|

|   . . +o + . = .|

|      o.=o .   ..|

|       +..      .|

|      ...        |

+--[MD5]----------+

#查看生出的私钥和公钥

08592946-A8B7-11A5-8567-000000821800:~/.ssh# ll

total 16

-rw------- 1 root root 1675 Jul 31 10:57 id_rsa  #私钥

-rw-r--r-- 1 root root  423 Jul 31 10:57 id_rsa.pub  #公钥

 

2.调用Novaapi创建keypair

添加密钥对:

~#nova keypair-add  --pub-key~/.ssh/id_rsa.pub jhjkey

~#nova keypair-list #可查看创建成功的密钥对

 

3.在创建VM时使用jhjkey密钥对注入公钥

~#nova boot --file /root/jhj.pub=/root/.ssh/id_rsa.pub  --image22f8ce93-76a3-42cd-9192-6713d5d3662c --flavor m1.small --nicnet-id=24993b81-0834-4f82-842a-924a6f3c609a  --availability_zone az01.shenzhen--fusionsphere jhj-test01  #创建一个VM01,使用密码登录,同时将私钥以文件方式注入该虚拟机中,作为登陆另外一个VM的跳转机

nova boot --key-name jhjkey --image 22f8ce93-76a3-42cd-9192-6713d5d3662c --flavor m1.small --nicnet-id=24993b81-0834-4f82-842a-924a6f3c609a --availability_zone az01.shenzhen--fusionsphere jhj-test02-keypair  #创建VM02,将jhjkey公钥注入虚拟机

 

4.登陆VM01,然后使用私钥sshVM02

nova控制节点上获取VM01vnc登陆地址

nova get-vnc-console ca66c588-208e-4a58-a68e-5c683bd64384 novnc #获取VM01vnc登陆地址

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Type  | Url                                                                             |

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+

| novnc |https://nova-novncproxy.az01.shenzhen--fusionsphere.vodafone.com:8002/vnc_auto.html?token=f0abd826-d918-4b50-81f8-6bcaebd4d80f&password=encode-YzIxMDU1NDg=|

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+

在浏览器上输入URL,即进入VM01shell登陆窗口

 

将私钥文件拷贝VM01上,然后登陆VM02

#ssh -i /root/.ssh/id_rsa ubuntu@192.168.10.9

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux3.13.0-49-generic x86_64)

。。。。。

 

④或者将VM02所在的subnet关联了router,则可以在routerNetwork节点(l3-agent)上登陆VM02

#ip netns exec qrouter-09ca6a0d-1a29-4f71-ae6c-4b3eee0b195d ssh -i/root/.ssh/id_rsa ubuntu@192.168.10.9

 

0 0
原创粉丝点击