42

I am trying to make a more streamlined means to establish an SSH client connection to a remote server. I have created a public/private keypair and used ssh-copy-id to install the public key onto the remote server.

However, it still was asking for the password unless I put in the path for the identity file with something like ssh -i ~/.ssh/mykey user@host. Should I have to type this to bypass the password with public key authentication?

To bypass this I used .bashrc and created an alias using this path. However, is this the way to do this? Or is it just a question of the server allowing the public key so I can just use the usual ssh user@host?

1 Answer 1

58

If you are able to successfully use keypair authentication with ssh -i ~/.ssh/mykey user@host, you can easily automate this with your SSH client configuration.

For example, if you add this to your ~/.ssh/config file:

Host hostname
  User username
  IdentityFile ~/.ssh/mykey
  IdentitiesOnly yes # see comment in answer below

You can then simply ssh hostname, and your username and identity file settings will be handled by your config file and you're off to the races, as they say.

The IdentityFile directive (which the -i switch for ssh overrides) has a default setting which will look for ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519, and ~/.ssh/id_rsa; any other filenames for private keys must be specified in the config file or with -i on the command line.

If you add IdentityFile to your ssh config, you'll find that the client still sends the default key (see ssh -vv output). This can be problematic when using sites like github with multiple accounts. You'll need to include IdentitiesOnly yes if you want ssh to use only the key you've specified.

6
  • 1
    I created '~/.ssh/config' like this: Host <server name> Hostname <ip address> User <username> IdentityFile ~/.ssh/<Identity File> But when I 'ssh <server name> I receive "Bad owner or permissions on /home/benny/.ssh/config"
    – Troy
    Jan 14, 2019 at 22:01
  • Can the login user be different to the key user? (like adding a key on the server for user USER, but using it for login as ROOT)?
    – Sandburg
    Mar 15, 2019 at 8:43
  • 1
    Private keys are held by the user logging in; public keys are held by the account being logged into. So long as the user's private key matches one of the authorized_keys in the remote account, you should be fine. In other words, for user's key to work for both remoteuser and root, both of the latter two must have user's public key in their own authorized_keys file.
    – DopeGhoti
    Mar 15, 2019 at 15:39
  • 2
    @Troy I think this is too late but config permissions should be strict according to the manual. 600 is just fine (chmod 600 ~/.ssh/config)
    – sçuçu
    Oct 3, 2021 at 20:38
  • 1
    IdentitiesOnly directive saved my day. I was getting too much login attempts error because ssh client was sending other keys that I have before the correct one. Nov 25, 2021 at 21:36

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.