Sunday, February 15, 2015

OpenStack - How to Access VM using SSH Key Pairs



I will demonstrate how to SSH to VM using SSH Key Pairs in this post.  This is very useful as it allows us to access the VM without keying in password.

The first step will be to log into the OpenStack environment (I am using DevStack for this post) and go to Access & Security -> Key Pairs



Next, click on the "Create Key Pair" button and give a name to the Key Pair:



Click on the "Create Key Pair" button to create the key pair and download the pem file (it will be key1.pem in my case)



Next, move the pem file that you have downloaded to the DevStack server and change its read/write permission (I have created a folder called 'keys' to store my private keys):

stack@DevStack:~/keys$ pwd
/home/stack/keys
stack@DevStack:~/keys$ chmod 600 key1.pem 
stack@DevStack:~/keys$ ll
total 12
drwxrwxr-x 2 stack stack 4096 Feb 15 18:38 ./
drwxr-xr-x 8 stack stack 4096 Feb 15 19:06 ../
-rw------- 1 stack stack 1675 Feb 15 18:38 key1.pem

stack@DevStack:~/keys$ 


The next step will be to create a test VM (you can refer to my earlier post on how to create test VM if you need more information). The only difference in this case is we need to make sure "Key1" is selected as the Key Pair under the Access & Security tab when we create the test VM:



After the VM is created, we need to assign floating IP to it so that we can access it from the public network (you can refer to my earlier post if you need more information on how to do that).  Once we have performed the required steps, we should see something like this in the Instances tab (notice that the Key Pair column shows "Key1", which was what I have chosen during VM creation):



Next, we will go to the folder with the pem file and SSH to our VM using its floating IP:



In this way, you will be able to SSH to the VM without providing password =)



2 comments:

Ranjitha said...

By doing port Forwarding in network settings in virtualbox as:
ssh,tcp,9590(host port),22(guest port)
In virtualbox guest os ubuntu if i try the command
ssh -p 9590 stack@127.0.0.1
Im getting error as : ssh: connect to host 127.0.0.1 port 9590: Connection refused

But i can able to connect to ssh. Only problem is host port that is forwarded is not listening in ubuntu

Later if i tried to do ssh to access openstack instance by the command:
ssh -i first.pem cirros@174.24.4.2
Im getting error as :
ssh: connect to host 172.24.4.2 port 22: Connection timed out

Can anyone please tell me the solution for this....

Juncheng Anthony Lin said...

Hi Ranjitha,

Did you set up your Ubuntu VM with SSH capabilities? I think I mentioned in my earlier post that you need to select "OpenSSH Server" when you install the Ubuntu OS.

As for the connection timed out issue, it seems like your Ubuntu OS is not able to reach the floating IP. Was everything set up properly, e.g. allow pings, SSH?

Thanks.

/Anthony