ssh key-based authentication.

This is a guide for setting up key-based authentication on the mac.

Key-based authentication is an alternative way of authenticating over ssh. Instead of a user name and password, you use a public/private key pair and authenticate this way. A user can optionally be specified as well.

To begin you’ll need to open Terminal.app in your Applications/Utilities folder.

Issue the following command into terminal:

mkdir ~/.ssh

This creates the necessary directory to hold your key.

Now navigate to the directory

cd ~/.ssh

In this directory issue the following command

ssh-keygen

Press enter at every prompt, you should see something similiar to this:

Jesse-Coles-MacBookPro:~/.ssh a$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/a/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/a/.ssh/id_rsa.
Your public key has been saved in /Users/a/.ssh/id_rsa.pub.
The key fingerprint is:
66:e2:df:a0:09:dd:41:aa:4b:d4:6f:f3:22:b3:a2:b4 a@Jesse-Coles-MacBookPro.local

The next step is to copy the public key to the remote machine. First though, the directory must be created.

Initiate an ssh connection to the remote machine.

ssh user@host

Enter your password like usual.

Next create the .ssh on the remote machine.

mkdir ~/.ssh

Now end the connection so you can copy the file to the remote machine.

exit

And copy the id_rsa.pub file to the remote machine

scp id_rsa.pub @:~/.ssh/new.pub

Notice i named the destiation file new.pub, this is so that if the destination machine already has key-based authentication already set up, you won’t overwrite its public key.

Now you need to initiate another ssh connection the the remote machine. This will be the last time you type your password for ssh.

ssh user@host

Now copy your public key to the users list of authorized keys

cat ~/.ssh/new.pub >> ~/.ssh/authorized_keys
exit

Your done!

From now on when you ssh to the remote machine, you will not be prompted for you password.

Related Posts