How to add a second private key to ssh in Linux

Written by
Date: 2020-07-06 21:06:00 00:00


You can log into Linux servers using passwords, or log in with no password, using ssh key, but there are times when it is not you who create the private/public key pair, or when you want to use a diferente key pair to log to another server.

One key pair for one server and another for the second one.

Let's suppose you already followed the steps above, so you alread have a key pair installed on your server. Now you have a second key pair you want to use to log to the second server.

Configure the client

In the client side, in the PC from where you want to log to the server, you need to follow two steps, install the second private key and then configure your PC to use that second key to log to the specified server.

Copy the private key

Just copy the private key to .ssh/ in your home directory. Name that file something like /home/user/.ssh/id_rsa2, and give it the appropiate permissions.

chmod 600 /home/user/.ssh/id_rsa2

Congure your PC to use that second key

You now need to create a new file, with your favorite editor, mine is vim

vim /home/user/.ssh/config

And add this text inside:

Host aws
    IdentityFile ~/.ssh/id_rsa2
    User your_user

Save it, and give the right premissions.

chmod 600 /home/user/.ssh/config

That is it, you can now log into, using the your_user user, and your second private will be used. You can add as many block to /home/user/.ssh/config file as keys you can have or create.