Unix / Linux: Manage Multiple SSH Keys

Objective: Manage multiple ssh private keys and configure ssh to map different private keys to different hosts automatically.

When using ssh rsa public key authentication, ssh will read the private key from ~/.ssh/id_rsa file by default.

If we have multiple private key files, we can tell ssh to use a specific key file by using the -i option.

$ ssh -i ~/.ssh/id_rsa_aws foo@aws

But do you know that there is a better way to manage the private key files? This is by specifying per host ssh configuration ~/.ssh/config file.

Below is a sample config for ~/.ssh/config file. It defines 2 hosts, with the relevant connection parameters.

To connect to server1.foo.example.com using ~/.ssh/id_rsa_server1 as the key file, and login as the user ibrahim, run ssh like this:

$ ssh server1

To connect to server2.foo.example.com using ~/.ssh/id_rsa_server2 as the key file, and login as the user ibrahim, run ssh like this:

$ ssh server2

For more information on the possible ssh configuration parameters, refer to the ssh_config man page.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }