Ssh – How to use ssh-agent for offering host specific keys from ~/.ssh/config file and manage the passphrases

sshssh-agent

I configured different hosts and keys for them in ~/.ssh/config file.
But, to manage the passphrases of different keys, I seek help from ssh-agent by adding the keys to it, via ssh-add command. Now when I ssh my hosts, the agent offers all the keys it has one-by-one.

I searched how to restrict this behavior of ssh-agent and, used

IdentitiesOnly yes

in the config file for all hosts.

I successfully restricted the ssh-agent from offering the keys it has, by doing so, but the ssh-agent is no more managing the pass-phrases and I have to enter pass-phrase every time I ssh.

Is there a way to render only the key for the specific host I am ssh – ing (reading from the ~/.ssh/config file) and manage the passphrase too?

Best Answer

The issue can be resolved by doing the following:

  1. Edit your client's ~/.ssh/config file such that the host entry has the following:

    Host fooName
        Hostname foo.name.tld
        User usrname
        IdentityFile ~/.ssh/fooName.pub
        IdentitiesOnly yes
    

    Note that the IdentityFile directive refers to the public key and not the private key file.

  2. Add the relevant private key to ssh-agent using ssh-add. You should be prompted for a password at this time.

Related Question