Thursday, February 21, 2019

Enabling the SSH Server on the Raspberry Pi

If you would like to work on your Raspberry Pi from a remote terminal, I find the easiest way to do so is with SSH, or Secure Shell Protocol.  SSH creates a remote terminal over a secure channel, and can even be used to transfer files in conjunction with SCP, or Secure Copy Protocol.

However, in order for SSH or SCP to work, we must first enable an SSH Server.  This tutorial will cover four ways to enable the SSH server on the Raspberry Pi.



Enabling the SSH Server with raspi-config


Perhaps the most common way of enabling an SSH server is by using Raspbian's built-in configuration utility: raspi-config.  In order to utilize this method you will need to have access to your Pi via keyboard and mouse.

Simply precede the command raspi-config with sudo to indicate you would like to run the command as root like so, and you will be presented with a menu that can be navigated using the arrow keys.

sudo raspi-config


Once in the menu, navigate down to option #5: Interfacing Options, and press [Enter].  This will take you to another menu, where SSH is option number #P2.  Again use the arrow keys to select option #P2 and press [Enter].


At this point you will be asked if you would like the SSH server to be enabled, to which you should answer <Yes>.  And that's it, easy peasey.  It will take you back to the main raspi-config menu where you can use the right arrow key to select <Finish>, which will then take you back to the command line.

Enabling the SSH Server from the Command Line


An even easier but less common way to enable the SSH server is directly from the command line using systemctl.  Simply use the following command to enable the SSH server:

sudo systemctl enable ssh

if you would like the SSH server to only run for the current session, and then default back to inactive after reboot, you can use the following command:

sudo systemctl start ssh


Enabling the SSH Server Headlessly with Linux


If you don't have a keyboard and monitor to access your Raspberry Pi, then you have a paradox: how to enable remote access without first having remote access.  Luckily the authors or Raspbian have a solution for you.

Once you create the SD card, but before you insert it into your Pi, you need to add a file named SSH (with no file extension) to the boot partition of the SD card.  This will enable the SSH server the next time the Pi boots.  After that the file will be deleted, so the SSH server will not be enabled on any subsequent boots.  Because of this, if you would like to permanently enable the SSH server, it is important to use one of the previous methods listed above to permanently enable the SSH server before you reboot your Pi.

To create the file use df -h to determine where the boot partition is mounted.  In the example below, you can see that the boot partition is listed as /dev/sdb1, and is mounted to /media/james/boot.  Simply navigate to the mount point, and create the file using touch ssh.  See the image below for the full procedure.


Enabling the SSH Server Headlessly with Windows


If you want to create a file named SSH in the boot partition of the micro-SD card, you can just as easily do it in Windows as you can with Linux, but there is one thing you have to be careful about.  Windows likes to automatically add file extensions, which will cause this procedure to fail.  If you simply create a file named ssh in notepad, it will look like the file is only named ssh on your screen, but in reality it will be named ssh.txt.

Luckily there is a simple way to get around this problem.  Simply open Notepad and click Save As.  Name the file "ssh" including the quotation marks, which will create the file with no extension.  Once you have created the file, simply insert the SD card and drag-and-drop the ssh file into your boot partition.

As mentioned before, don't forget that this will only enable the SSH server on the initial boot, so remember to permanently enable the SSH server using one of the other methods mentioned above.

If you also need instructions on how to configure Wifi headlessly using Windows or Linux, this tutorial can help you out.

Have Fun!

No comments:

Post a Comment