I was able to setup ssh to use private/public key authentication. Now I am able to do

ssh user@server1

And it logs on with the private key. Now I want to connect to another server and use a different key. How do set it up so

ssh user@server1

uses privatekey1

ssh user@server2

and uses privatekey2

  • 7
    No real reason - ones a personal mythbox and the other is a work server and I didn't want to use my home key at work.
    – Joshua
    Aug 12, 2010 at 11:56
  • none of the answers seem to answer the full quesiton, eg, how to create different keys privatekey1 and privatekey2
    – jpw
    Jan 27, 2016 at 18:40

3 Answers 3


You can set this up in your ~/.ssh/config file. You would have something like this:

Host server1
IdentityFile ~/.ssh/key_file1

Host server2
IdentityFile ~/.ssh/key_file2

man ssh_config is a reference

  • 5
    You can also specify User for each host to cut down on key strokes. Lastly when you're generating a new SSH key for each host with ssh-keygen make sure you actually enter an alternate key path. Aug 11, 2010 at 20:49
  • Is there an option to specify a default key, like saying host x ->key1 and then the rest of the hosts key2
    – azerafati
    Jun 19, 2017 at 14:54
  • Yes, use sh Host * And add the defaults, than overrides van be added underneath. Also: sh man_ssh_config Specifically the part in the "Host" section with regards to asterisk '*' & exclamation mark '!' Mar 19, 2018 at 13:08

There are a few options.

  1. Load both keys into your ssh agent using ssh-add. Then both keys will be available when connecting to both servers

  2. Create your $HOME/.ssh/config file and create a Host section for server1 and another for server2. In each Host section, add an IdentityFile option pointing to the appropriate private key file

  • 2
    ssh agent was a great idea, also if I have both the public and private key files, ie key1 and key1.pub, in ~/.ssh it will automatically load them.
    – Joshua
    Aug 12, 2010 at 11:58

Besides the (preferable) option of adding both keys in $HOME/.ssh/config (note that this requires appropriately setting attributes of $HOME/.ssh and $HOME/.ssh/config), you can use

$ ssh -i privatekey1 user@server1


I learned this by way of solving this more complex situation: Multiple ssh access types from a given user1/client to the same user2/server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.