Wednesday, February 20, 2019

Remote Access to the Raspberry Pi via SSH

You can access the Raspberry Pi from a remote computer using the SSH protocol (aka Secure Shell).  This provides for secure, encrypted communication, terminal access, and file transfer between your remote machine and the Pi.

Before you can access the Pi via SSH, you will need to enable the SSH server on the Raspberry Pi.  To learn how to enable the SSH server on a Raspberry Pi, please see this tutorial.

You will also need to know the username and password to log into the Pi.  By default the username is pi, and the password is raspberry, but if you are going to enable SSH access to the Pi, you should really change the password to something other than the default.

Accessing the Raspberry Pi via SSH from Linux


You can easily initiate an SSH session to the Pi from the Linux command line using the ssh command.  If you know the IP address of the Pi, the command would look something like this, where the argument following the -l option is the username for the Pi, and 192.168.4.24 is the IP address for this particular Pi:

ssh -l pi 192.168.4.24


In the example above, you can see that we are prompted to verify a key from the Pi.  This will only happen the first time you login to the Pi from any given computer.  This is done for security purposes, but is beyond the scope of this tutorial.  For now, just type yes.

You may also notice that the Raspberry Pi warns the user that the default password has not been changed.  This can allow unauthorized users to access you Pi, and should be remedied as soon as possible.  In my tutorial on the Initial Setup of the Raspberry Pi Using raspi-config, the first thing I cover is changing the default password.

You will know that you have successfully logged onto the Pi when your prompt no longer begins with your username@hostname of your Linux computer, and instead you are presented with:

pi@raspberrypi:~ $


Using the Hostname Instead of the IP Address


If you don't know the IP address of your Pi, which will often occur when you setup the Pi headlessly, you can also access the pi using it's hostname followed by ".local".  By default, the host name is raspberrypi, so if you haven't changed the hostname the command would look something like this:

ssh -l pi raspberrypi.local


Finally, as often occurs in Linux, there is another commonly used syntax of which you may want to be aware.  Instead of specifying the username by using the -l option, you can also simply precede the IP address or hostname with the username followed by an @.  For instance, the following two commands would do the exact same thing:

ssh -l pi raspberrypi.local

ssh pi@raspberrypi.local


When you are done working on your Pi, simply type logout to end your SSH session and return to your Linux terminal.

Access the Raspberry Pi via SSH from Windows


If you are using a computer running Windows, you will need to install software to run an SSH client.  I recommend PuTTY, which you can download from the following site:

Once you have installed PuTTY, starting an SSH session is relatively straight forward.  Simply input the IP address (in this case 192.168.4.24) or the hostname (raspberrypi.local by default).  The "Port" will be set to 22 and "Connection type" will be set to SSH automatically, which are the correct settings for a typical SSH session.  The only thing left to do is click Open.


Just as when we first SSHed into the Raspberry Pi from our Linux computer, Putty will ask us to verify that we are sure we are logging into the correct machine by presenting us with the Pi's SSH key.  I will discuss this further in a future tutorial, but at this point just click Yes.


And once again we will know that we are logged onto the Pi when we are presented with the following prompt:

pi@raspberrypi:~ $

Note, this prompt is in the format user@hostname: so if you change the hostname or login using a different user, then your prompt will change accordingly.



When you are done with your SSH session, simply type logout to logout of your Raspberry Pi and close PuTTY.

Now that you know how to login to your Pi remotely, if you would like to learn how to navigate around from the command prompt, take a look at my tutorial on Navigating in Linux from the Command Prompt.

If you don't know how to edit files, you might be interested in my tutorial on File Editing Basics Using Vim.

To learn how to copy files over SSH, check out my tutorial on SCP.

Have Fun!

No comments:

Post a Comment