3

I'm trying to connect to my host using SSH key. I've generated a key pair and added public key to authorized keys to the server.

However I'm unable to connect to server using my private key. Every time I connect it asks for the password.

I've tried to run ssh-add my_key_name and it said that it's fine and been added. But it is not working.

Also I've tried to add to my config

Host alias
    HostName host
    User user_name
    IdentityFile ~/.ssh/id_rsa

It also is not working. ssh alias working but still asks for password.

3
  • It'd be helpful to get a little more context about what's failing. Using the -vvv verbose flag to give you logging during connections usually shows exactly why it's not working. Additionally, logging into the server, and tailing the secure.log is another great resource if it does indeed happen to be a server misconfiguration. 99% of the time, though, it's a permissions issue on your local machine.
    – ryebread
    Commented Sep 6, 2018 at 15:30
  • I've written a how-to of sorts for this. See: apple.stackexchange.com/questions/285805/how-to-ssh-in-one-line/…
    – Allan
    Commented Sep 6, 2018 at 16:03
  • ssh keys only work if the server connected to already knows the public key corresponding to the private key the client holds. You must take the steps needed to have this happen first. Commented Sep 6, 2018 at 20:28

3 Answers 3

3

For a detailed explanation of what's going on see SSH Agents. Halfway through the page you'll find SSH Agent on OS X & macOS.

See also Apple's Technical Note TN2449 - OpenSSH updates in macOS 10.12.2

You have to add some lines to your config file:

# enable integration between Keychain and SSH Agent  
UseKeychain yes  
AddKeysToAgent yes

Once you have done that, I think you will be asked for your password the first time you ssh into your server. After that it will use the keys you provided.

So your config file should look like this:

Host alias
    HostName host
    User user_name
    IdentityFile ~/.ssh/id_rsa
    # enable integration between Keychain and SSH Agent  
    UseKeychain yes  
    AddKeysToAgent yes
4

To connect using SSH keys you need to copy the key from your machine to the remote server using ssh-copy-id user@remote_ip_address.

You will be asked to enter the password for the remote server and the key will be copied.

If you generated a key pair using a password then you will always have to enter that password when connecting. If not, you can simply connect using ssh user@remote_ip_address and you will be connected to the remote machine.

1
  • Following the instructions I've generated key pair on the server and copied from there a private one
    – Sergey
    Commented Sep 5, 2018 at 18:14
2

Are the permissions of authorized_keys correct? Should be 400 owned by the user.

Check the logs of the remote server, usually helpful to debug auth issues on SSH.

1
  • In addition to checking the permissions of the authorized_keys file, the ~/.ssh directory itself must have permissions of 700.
    – guzzijason
    Commented Sep 6, 2018 at 4:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .