tarpn_logo
 home    builders    Search

buildersSSH to Raspberry PI ➜ SSH to Raspberry PI from MacOSX

SSH to Raspberry PI from MacOS using RSA key

Since SSH is a built in command line function in the MacOS, it is easy enough to use it to access the Raspberry PI and no additional software is required. The procedure shown in this file lets you use RSA Keys instead of hand entered passwords to complete the log-in process to the Raspberry PI. I also show you haw to make an alias for connecting to the Raspberry PI so you don't have to type the IP address or user name of the PI each time.

Before you do this procedure you should set up your home network so the Raspberry PI always gets the same IP address. Do this in your router.

In this example I use jones1 as the name of the node and linda as the name of the mac user. I also use 192.168.0.101 as the IP address of the node.
Replace these figures with your own Mac short username, nodename, and IP address.

Steps

First generate a public and private key. The public key goes to the Raspberry PI and the private key goes in the user folder on the Mac.

ssh-keygen -t rsa -C "jones1 node access"
This command will result in a couple of queries:

Enter file in which to save the key:   if you have never created one of these keys, just hit return to create the two files id_rsa.pub and id_rsa. If you have created one of these already on your Mac then hitting return may overwrite the existing key files. In that case type in a file name like node or jones1. I will assume you just hit return for the purposes of this example.

Enter passphrase:   Enter a decent password. You will only have to type this once for each Mac account or computer that you use. I recommend writing down this password or saving it in a secure place outside of your Mac since you won't use it again until you decide to connect to your Raspberry PI from a different computer, or if your Mac gets blown up.
Enter passphrase again:   Type your password again.
When you hit return the machine will create the two files and print out some diagnostic info that should like something like this:

Your identification has been saved in /Users/linda/.ssh/id_rsa.
Your public key has been saved in /Users/linda/.ssh/id_rsa.pub.
The key fingerprint is:
67:42:d7:2b:35:2f:e9:0a:45:87:e5:8f:be:b3:37:89 jones1 node access
The key's randomart image is:
+--[ RSA 2048]----+
|            .    |
|           =     |
|        . + *    |
|       . o o B   |
|        S = = o  |
|         = + .   |
|        .   o. . |
|         . .E.+  |
|          . o+ . |
+-----------------+

Now you need to put the public key onto the Raspberry PI.
cat ~/.ssh/id_rsa.pub   This will cause the public key data to be printed in the Terminal window. Select the line of text starting with ssh and ending with the name string, jones1 node access in this case, and do a command-C copy of the text.
ssh over to pi@192.168.0.101 and log in.   Substitute the IP address your Raspberry PI gets assigned. Now do cd .ssh then cat > authorized_keys then command-V paste, and then control-D which closes the cat. You should now open another terminal window and attempt to do ssh pi@192.168.0.101 to get to the node again.
A dialog box will open on the Mac asking you for your passphrase. Use the decent password you specified above, and check the box that says save password.

That should do it. You will not need a password to connect from that Mac and user to that Raspberry PI again. Try doing ssh pi@192.168.0.101 (or your IP address) again.

Now lets create an alias in your .bashrc file so you don't need to do the ssh command anymore.

First create an alias to show that we understand the syntax. Type this:
alias jones1="ssh pi@192.168.0.101" and hit return.
Now if you type jones1 return, you will connect to your node.
If that works, let us make it permanent by writing the same command to your .bashrc file.
Get back to your Mac's terminal prompt by exiting from the Raspberry PI log-in, or by creating another Terminal window.
type
cat >> .bashrc
alias jones1="ssh pi@192.168.0.101"    Substitute the IP address your Raspberry PI gets assigned.
and then do control-D to close the cat.
If you log out and log back in to your Mac account, the alias will be automatically applied at log-in.

Now if you type jones1 you should get connected to your node with no fuss or bother.
If you want to change the text alias you type to get to the node, use a text editor like bbedit or TextWrangler or vi to edit the .bashrc file.

© Tadd Torborg, 2014↝2018 -- all rights reserved