Multiple ssh private keys

From Cosmin's Wiki

Jump to: navigation, search

Home > Linux how to's > Multiple ssh private keys



Sometimes, when working from a Unix/Linux/OsX box, you have multiple private keys that you're using for accessing different servers. For that, you have several options. Say you want to ssh a box with custom port 123, then you could do:

ssh -p 123 -i ~/.ssh/my.private.key user@host

The more elegant option however, is to create a config file in your .ssh directory and specify in there multiple keys:

touch ~/.ssh/config
chmod 600 ~/.ssh/config
echo "IdentityFile ~/.ssh/key1.private" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/key2.private" >> ~/.ssh/config

Then, for every connection you make, both keys would be used. The config file allows you however many more options you could use. Below is an example of such a config file:

Host host1.com
        IdentityFile ~/.ssh/key1.priv
        Port 123
        User cosmin
 
Host *.host2.com
        IdentityFile ~/.ssh/key2.private
        Port 456
        User cosmin