How to choose the key used by SSH for a specific host?

Posted on In QA

How to choose a key used rather than the default ~/.ssh/id_rsa when ssh to a remote server for a specific host?

You have at least 2 choices for choosing the key used by ssh. Taking ~/.ssh/key1 and user@example.com as the example here.

Method one, specify the key in command line with the -i option of ssh. Check more in the manual.

The command line to be used is:

ssh -i ~/.ssh/key1 user@example.com

Method two, use the ssh config file. This is useful when you do not have the -i option available, such as using git, rsync or lftp.

You can add to your ~/.ssh/config lines as follows.

Host example.com
  IdentityFile ~/.ssh/key1

This tells ssh that for host example.com, use the key file in ~/.ssh/key1.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

2 comments

  1. Hi Eric,

    Thanks for the tutorial. Your ~/.ssh/config file example contains a typo. It should be IdentityFile, not IdentifyFile.

    Host example.com
    IdentityFile ~/.ssh/key1

    Regards,
    v

Leave a Reply

Your email address will not be published. Required fields are marked *