How to generate and use SSH authentication keys

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

Secure Shell Keys are one of the most common ways to securely connect to a remote machine. If you're a customer at cloudcontainers, you will have already seen the term pass while creating a virtual machine. The keys allow an authentication mechanism without, by definition, always requiring a password. This will help speed up logging in or connecting to your virtual machines, which is why it's such an attractive option for a lot of people. In this guide we'll go over how to exactly create an SSH-key and how to configure your containers to use it.

Generate the SSH key

First, we open our command line and typ in the following command to generate the key:

ssh-keygen -t rsa


You will be asked two questions, one of which is what file to save the key in: you can keep the default file name. Press enter.

You will also be asked for a passphrase. Many people use SSH keys to avoid having to typ in their passwords; However, if your SSH key manages to leak out, which can happen in all sorts of ways, the person who's obtained the key will be able to log in very easily. I recommend using a password, but if you handle these files with extreme caution, it's not absolutely necessary.


After you've answered these questions, two files will be generated. id_rsa and id_rsa.pub, the .pub file is your public key. The server or remote machine will use this file to validate if your personal key fits the public key; the other file is your personal key.


Upload public key to container

There are two ways to upload your public key for your containers in order to use them to verify remote connections:


Add the key to your Cloudcontainers account

Now add your public key to your Cloudcontainers account, so your VM's will know what to verify your personal keys with. Go the your account page, if you're logged in you should be able to use this link: https://my.cloudcontainers.net/#/account 

Navigate to the tab: "SSH keys".  Give the key a name by filling in the "Key name" field, you can name it whatever you want but if you have a lot of keys, you should probably name it something that'll help you organize them a bit.

Next, open the .pub file and copy its content. It will look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8dE2kSX+e/bNDiEt9GqjnQWhMOXp7hycSrZO+49nDK/pX1rN+WGuWh6rTr0LKYNen+hJjFOilzBqoyHI90+ndOD+aMjOxWh9YzY/7DmUUqd49I33unCVSlCB95ts2uING2aWJQQ/tV+g+BQXQ0fUljBw59lAu42bvVvxHPI+ot+LvQ2FYRT9sJZwj2nxuGDXyYRXOYqcFF5yIqvLQEL2id+Jp0/V1hkjlyG/7rUVNrAdRGKyHGtrT9/Lu10WJuJr7FfT/P8/5UXd++K3DUrRcqqCUefwSHnAIT8K9eGWkryUviN+t8B60hdqrh7qA6FbckKGd9sRcDCob57BbRQqf klaas@KARL

Paste it in the "Key value" field.

You should now be able to use this key to make a connection to new virtual machines you make.

ssh -i ~/.ssh/id_rsa root@82.97.163.68


Add the key to an existing machine

If you already have a machine running that you want to add your key to, we'll need to modify the "authorized_keys" file on the machine.

First, open the .pub file and copy its content, it will look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8dE2kSX+e/bNDiEt9GqjnQWhMOXp7hycSrZO+49nDK/pX1rN+WGuWh6rTr0LKYNen+hJjFOilzBqoyHI90+ndOD+aMjOxWh9YzY/7DmUUqd49I33unCVSlCB95ts2uING2aWJQQ/tV+g+BQXQ0fUljBw59lAu42bvVvxHPI+ot+LvQ2FYRT9sJZwj2nxuGDXyYRXOYqcFF5yIqvLQEL2id+Jp0/V1hkjlyG/7rUVNrAdRGKyHGtrT9/Lu10WJuJr7FfT/P8/5UXd++K3DUrRcqqCUefwSHnAIT8K9eGWkryUviN+t8B60hdqrh7qA6FbckKGd9sRcDCob57BbRQqf klaas@KARL


Next, open the "authorized_keys" file on the server using the following command:

nano /root/.ssh/authorized_keys


Here we can paste our public key, afterwards save and exit the file.

You should now be able to use this key to make a connection the virtual machine:

ssh -i ~/.ssh/id_rsa root@82.97.163.68


Thank you for reading this tutorial!

Comments

Comments are turned off.