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.
1 |
$ 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 in ~/.ssh/config
file.
Below is a sample config for ~/.ssh/config file. It defines 2 hosts, with the relevant connection parameters.
1 2 3 4 5 6 7 8 9 |
Host server1 HostName server1.foo.example.com IdentityFile ~/.ssh/id_rsa_server1 User ibrahim Host server2 HostName server2.foo.example.net IdentityFile ~/.ssh/id_rsa_server2 User ibrahim |
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:
1 |
$ 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:
1 |
$ ssh server2 |
For more information on the possible ssh configuration parameters, refer to the ssh_config
man page.