SSH Config
Sometimes you need to use different SSH keys to connect to different remote hosts. You could be a little bit lazy and you don’t want to remember which is the right SSH key to use.
The best way is to configure your SSH agent in order to use the right SSH key based on the host, using a configuration file named.ssh/config
.
This is a simple textfile with a well-known syntax.
For example, if you would like to connect to host bar.example.com
using SSH key id_rsa_bar
and user bar
, and also connect to host foo.example.com
using SSH key id_rsa_foo
and user foo
, the file will be like the following.
Host bar.example.com
User bar
IdentityFile ~/.ssh/id_rsa_barHost foo.example.com
User foo
IdentityFile ~/.ssh/id_rsa_foo
As a former colleague said, using .ssh/config
is very powerful, as you could combine the former example with wildcards, so you could one config for *.example.com and another for 10.*, specifying ports users and keys.
Originally published at https://gabriele-decapoa.github.io.