Saturday, March 16, 2019

Headless Set-up of a Raspberry Pi Running Raspbian

A headless machine is one that runs without a monitor and keyboard directly attached for input and output.  Instead it relies on remote connectivity for user interaction with the operating system.

There are many Raspberry Pi projects that don't require direct user interaction with the operating system, such as home automation or monitoring, and for these a headless setup is optimal.  To create and/or alter these systems, one typically uses SSH, or some other remote access protocol.


The difficulty with remote setup, however, is not remote connectivity, but the question of "how do you set-up the Pi for remote connectivity without first having remote connectivity?"  If you have a monitor and keyboard to attach to the Pi then you can do the initial setup locally, but if that is not an option you need a way to enable remote connectivity before first boot.  That is what I will show you how to do in this tutorial.


Enabling Wifi Access Before First Boot


If you have an available Ethernet port and cable, then providing your Raspberry Pi with network access for remote connectivity is as easy as plugging in the cable.  However, if you don't have Ethernet access readily available, or more likely, you are using a Pi without an Ethernet port such as a Raspberry Pi Zero-W, then you need to be able to tell it how to connect to Wifi before you actually have access to it.  Here's how to do it.

Once you have burned the operating system on the SD card (see this tutorial on writing the disk image to the SD card), you will need to create a file on the card named wpa_supplicant.conf.

To create the file in Windows, you can simply use Notepad.  To create the file in Linux you can use a graphical text editor, or you can just use a text editor from the command line, which is what I prefer.  For a quick introduction on How to Use the Command Line Text Editor Vim, check out this tutorial.

The file needs to contain the following information in the following format.  Feel free to copy and paste, but don't forget to change YourSSID to your actual SSID, and YourPassword to the passkey for your Wifi.

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YourSSID"
psk="YourPassword"
key_mgmt=WPA-PSK
}

* A couple of quick notes on the contents of this file:  If you are connecting to an unsecured network, then simply omit the line starting with psk, and change the key_mgmt line to look like this: key_mgmt=NONE.  If you are connecting to a network with a hidden SSID, add the following line after the line beginning with ssidscan_ssid=1

Once you have created the file save it as wpa_supplicant.conf.  If you are using Notepad in Windows be sure to put the name of the file in quotes so that it uses the .conf as the file extension instead of appending a .txt to the file and making it a text file.

The file then needs to be copied onto the boot partition of the SD card.  If you are using Windows simply drag and drop the file into the boot partition.


If you are using Linux, first determine the mount point of the boot partition using lsblk.  Then copy the file using the cp command preceded by sudo as shown below.

cp ./wpa_supplicant.conf /media/james/boot/wpa_supplicant.conf



And that's it.  Upon first boot the file you created named wpa_supplicant.conf will overwrite the file /etc/wpa_supplicant/wpa_supplicant.conf on the Linux partition of the SD card, and then the file on the boot partition will be erased.  If you provided the correct information then the Pi will automatically connect to the Wifi network you specified.

You now have Wifi connectivity, but you still won't be able to remotely log onto your Pi until you enable the SSH server.  Luckily there is a quick and easy way to do that as well.

Enabling the SSH Server Before First Boot


In order to enable the SSH server before logging onto your Pi for the first time you must once again place a file on the boot partition, but this time the contents of the file are irrelevant, the only requirement is that it be named ssh.

If you are using Windows, open a new blank document in Notepad, and then save it as "ssh".  Be sure to include the quotation marks so that Notepad doesn't automatically add the .txt file extension.  Just as you did above, drag and drop the newly created file into the boot partition of the SD card.

If you are using Linux you can create the file using the touch command.  Just like you did above, you can use the lsblk command to determine where the boot partition is mounted.  Then navigate there using cd and create the file using touch like so:

cd /media/james/boot
touch ssh

And that's it.  Upon first boot the file named ssh on your boot partition will be erased, and the OpenSSH server will be enabled for one time only.  You will then need to permanently enable the SSH server or else the next time you boot you will not have SSH access.  You can use this tutorial to learn how to permanently enable SSH access to the Pi.

If you need help connecting to the Raspberry Pi using SSH, this tutorial will show you how to connect.

Finally, once you have connected, this tutorial will show you how to do some of the initial configuration required for most projects.

If you have any questions, please let me know in the comments and I will do my best to answer them.

Until next time, Have Fun!

No comments:

Post a Comment