This guide will explain how to set up a Raspberry Pi with Raspbian Linux over nothing more than a WiFi connection. If you have been wondering how to configure a Raspberry Pi without using a monitor, keyboard, mouse, or even Ethernet then this is the guide for you. At the end your Raspberry Pi will be online and you will be able to control it via SSH using a secure private key instead of a password.

Prerequisites

This will work with a Raspberry Pi 3 using the on-board WiFi module and will also work with a Raspberry Pi 1/2/Zero using a Linux compatible WiFi adapter. I tested this with both a Raspberry Pi 3 and a Raspberry Pi 1 (B) using a cheap generic Linux compatible USB WiFi adapter found on Amazon.

You will need a desktop/laptop computer to create the SD/microSD card for the Raspberry Pi and connect to it over SSH. Obviously, it must have an SD/microSD card reader. This guide is geared for a Linux desktop and all configuration will be done on the command line. It is possible to do this from Windows or Mac. However, since we need to modify the raspbian file system and that file system is in the ext4 format only a Linux desktop will support reading/writing to ext4 natively. Doing this from Mac or Windows is beyond the scope of this guide.

You will also need an SD card or MicroSD card with at least 4GB of space. I recommend MicroSD as these will work in older Pi’s with full size SD slots using an adapter.

First, ensure you have a secure SSH key. We will need to copy your public key to the Raspberry Pi so it recognizes you. If you don’t already have an SSH key, follow the Github guide to ‘Generating a new SSH key’. If you aren’t sure, try to print out your public key on the terminal of your desktop/laptop.

cat ~/.ssh/id_rsa.pub

If you see what looks like gibberish with your email at the end then you have what we need. If you see No such file or directory or similar then you should follow the guide above.

Copy Raspbian to SD Card

Next, download the latest version of raspbian linux and unzip it. Both the “With Pixel” (Desktop) and “Lite” (Server) version of raspbian will work. I recommend using “Lite” since we won’t be using all the extra desktop software included in the other version.

You should now have a file like 2016-11-25-raspbian-jessie-lite.img on your computer. Use your terminal navigate to the folder containing this file. You should verify using the ls command that the raspbian img file is in your current directory.

Insert your SD card into your desktop. Note the device name (like /dev/mmcblk0 or /dev/sde) and unmount any SD card partitions. If you are unsure how to find the device name or unmount the partitions, please refer to the raspbian installation guide. Replace /dev/mmcblk0 with your SD card’s device name in the command below to write the raspbian image to the card.

sudo dd bs=4M if=2016-11-25-raspbian-jessie-lite.img of=/dev/mmcblk0

Configure Raspbian for Headless Use

For raspbian to work without a keyboard and monitor we need to configure the following items.

  1. Automatic connection to your WiFi network
  2. SSH authentication through SSH keys only
  3. Authorize your public SSH key
  4. Start SSH service on boot

All of this will be accomplished by modifying the SD card we just created. Everything should be fully configured so that after you insert the SD card and power it up the Raspberry Pi “just works”.

To start, remove and re-insert the SD card we just created. There will be a boot partition and a file system partition. If you aren’t sure which is which, the file system has a home directory in it (and many others). The boot directory doesn’t have a home directory and likely only has one directory.

Steps 1 through 3 require modifying files on the file system partition, so our journey starts there. Navigate to the root of the file system.

cd /media/carl/0aed834e-8c8f-412d-a276-a265dc676112/

Replace the path above with the location of the SD card’s file system partition. It will likely be similar but will not be exactly the same. Steps 1 through 3 will assume you are at the terminal at the raspbian file system’s root.

1. WiFi Configuration

WiFi will already be enabled on both the Raspberry Pi 3 and older Pi’s if you are using a sufficiently Linux compatible USB adapter. The trick is to get it to connect to your network. For that to work we just need to edit one file: /etc/wpa_supplicant/wpa_supplicant.conf.

Run the following command from the raspbian file system root:

sudo nano etc/wpa_supplicant/wpa_supplicant.conf

Using the nano text editor, add your network SSID and password to the end of the file in the following format:

network={
  ssid="network-name-here"
  psk="my-wifi-password-here"
}

Using CTRL+X to close, choosing Y (yes) when asked to save changes.

2. SSHD Configuration

To keep your Pi secure we should disable the ability to use a password when logging in via SSH. This will allow only SSH keys to be used to authenticate you. This has the advantage of saving you effort when logging in as well as you will not need to use a password.

Again we will use nano to edit a file. /etc/ssh/sshd_config in this case.

sudo nano etc/ssh/sshd_config

Find the line that looks like this:

#PasswordAuthentication yes

Change it to look like this:

PasswordAuthentication no

Note we removed the # to uncomment the PasswordAuthentication directive and also changed the default value from yes to no.

Save and close this file.

3. Authorize Your SSH Key

Use the following commands to copy your public ssh key to the pi user’s authorized_keys file so you can have password-less SSH access to the account.

sudo mkdir home/pi/.ssh
sudo cp ~/.ssh/id_rsa.pub home/pi/.ssh/authorized_keys
sudo chown -R 1000:1000 home/pi/.ssh

The final command changes the file ownership to the pi account so it’s easier to modify the authorized keys later. 1000 is the uid of the pi user and is used instead of the username as your linux desktop won’t know the pi user. Your linux machine will probably show a different user owning the pi user’s files (whatever user has a uid of 1000). This is fine.

4. Start SSH Service on Boot

Now we need to modify the other partition on the SD card. Navigate to that partition in your terminal. It will look something like this but will almost certainly be a little different:

cd /media/carl/boot

The latest raspbian disables SSH by default. To have enable the service on the initial boot we just have to create an empty file called ssh on the boot partition. From the boot partition execute the following in your terminal:

touch ssh

Try It Out

Everything should now be configured but there is only one way to know for sure. Plug your newly modified raspbian SD card into a Raspberry Pi. Hopefully after a minute or two you should be able to SSH into the Pi.

To do that you will need to discover what IP address the Raspberry Pi was assigned by your router. The easiest way to do this is probably to log into your router and look for a recently connected device called raspberrypi.

Once you have the address, logging in is as simple as…

ssh pi@192.168.1.123

… where 192.168.1.123 is the local IP address of your Raspberry Pi.

Once logged in, run the raspi-config utility to expand the filesystem to match the size of your SD card, change the hostname, change the password, and make any other configuration changes you’d like.

sudo raspi-config

Now you are all set to make your Raspberry Pi cluster or Raspberry Pi garage door opener or whatever else you can think of.

Happy hacking!