35

When connecting via SSH my terminal wants to use id_rsa by default. I don't want to use that key for this particular server. So I am forced to specify the proper key path when connecting:

This works to connect: ssh -i /Users/myuser/.ssh/mykey serveruser@server.com

But I would prefer to use the following to connect: ssh user@server.com

My Question: Is there a way to indicate in known_hosts or other config that SSH should use the key located at /Users/myuser/.ssh/mykey when serveruser is connecting to server.com?

Thanks!

MastaP
  • 453
  • 1
  • 4
  • 5
  • Have you considered using a function or script to replace `ssh`? It is then a straightforward task to scan the parameters and add the extra parameter to use the other key when connecting to `server.com`. – AFH Dec 12 '17 at 17:06

2 Answers2

50

One option you could consider would be to user the .ssh/config file.

Example: .ssh/config

Host server.com
    HostName server.com
    User serveruser
    IdentityFile /Users/myuser/.ssh/mykey

By doing this you could execute "ssh server.com". The config file would use the specified Username and Identity File.

Erik
  • 1,426
  • 10
  • 7
  • 2
    Addition: You can use `~/.ssh/mykey` for a local key file, also on Windows. – Lii May 26 '19 at 11:33
  • Note that if you don't have the config file in your .ssh folder you must create it first and note that the User line is optional – Georgi Peev Jun 30 '20 at 20:17
  • Furthermore the `HostName` line is not required in this example, as it just tells the ssh client how to resolve the `Host` value (e.g. if you want to use a short alias there). – MA-Maddin Jun 23 '23 at 15:34
3

An additional option would be to use ssh-agent.

If you add all of your identities to it

ssh-add .ssh/id_rsa
ssh-add .ssh/mykey

When you connect to the remote host, the one that works is the one that is used.

Erik
  • 1,426
  • 10
  • 7