Configure SSH Key to Host

SSH allows user to set default behaviors when setting up. One problem with SSH is that when a user has multiple sets of SSH keys deployed across different hosts, SSH tends to get confused. This section covers the configurations of SSH Key to host.

Editing SSH Config

The first thing to do is to edit the ~/.ssh/config file that defines all the configurations.

Add Default Key for Any Hosts

Next, add the default key for any unidentified host. This is the fallback key. Example, say our key is id_KEYLABEL (like id_rsa or id_ed25519):

Host *
  ForwardAgent yes
  ForwardX11 no
  IdentityFile ~/.ssh/id_<KEY LABEL>

One example:

Host *
  ForwardAgent yes
  ForwardX11 no
  IdentityFile ~/.ssh/id_ed25519

Add Key for Specific Host

Next, add the key using the following pattern for each hosts:

# Home account
Host <URI>
  HostName <Anything but usually domain name>
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_<KEY LABEL>

One example:

# Home account
Host home.github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_home

# Company account
Host company.github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_company

Once it's done, save the file and SSH will react accordingly. That's all about configuring SSH to use correct key for correct host.