52

I have two public keys, one for some servers and one for others. How do I specify which key to use when connecting to a server?

64

Assuming you're on a Unix/Linux environment, you can create or edit the file ~/.ssh/config.

That config file allows you to establish the parameters to use for each host; so, for example:

Host host1
  HostName <hostname_or_ip>
  IdentityFile ~/.ssh/identity_file1

Host Host2
  HostName <hostname_or_ip2>
  User differentusername
  IdentityFile ~/.ssh/identity_file2

Note that host1 and host2 can also be not hostnames, but rather labels to identify a server.

Now you can log onto the to hosts with:

ssh host1
ssh host2
| improve this answer | |
  • 45
    You can also use -i <keyfile>, but I'd definitely recommend the config file method in the general case. – womble Jul 30 '11 at 8:43
  • I tried this but I keep getting prompted of the passphrase for my key. Even when I enter the passphrase correctly, the ssh login doesn't work. I tried using a blank passphrase too – Hamman Samuel Feb 29 '16 at 20:27
  • I had to do ssh differentusername@host2 for a proper login, but otherwise this worked wonderfully, thank you! – agrippa Jun 21 '19 at 21:57
-2

On Fedora 27, you can put the private/public keys under ~/.ssh/ and then when you ssh to a host, both of them will be tried automatically.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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