MacOS SSH connect using key

high sierrasshterminal

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.

Best Answer

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