Search code examples
macossshprivate-key

How do I install my SSH keys on a new computer?


I just switched from PC to Mac. I had been connecting to a server via SSH using a key on the PC. I thought all I'd have to do to connect on the Mac would be copy the .ssh directory into my home directory and then run the ssh command in the terminal with the right credentials, but it doesn't seem to be working.

When I run this command;

ssh [user]@[hostname] -p 2222

I get this result;

Permission denied (publickey).

In my .ssh directory, there are three .pub files and three key files with no extension. There's also a known_hosts file and a key_backup directory with some keys in it.


Solution

  • Supposing you did copy the correct keys, you need to add your identities to ssh-agent. Try the following:

    ssh-add /Users/<username>/.ssh/id_rsa
    

    Where <username> is your username and id_rsa is a private key. It's important to use the absolute path. Upon success, you'll see something like:

    Identity added: /Users/johndoe/.ssh/id_rsa (/Users/johndoe/.ssh/id_rsa)
    

    Repeat for every key you want.