Headless Pi with static IP - wireless edition

Here is an easy way to set up a headless wireless Raspberry Pi without having to connect a keyboard or monitor! In this article we will go through the process of downloading and installing Raspbian Lite on an SD card. We will set the static IP by modifying the files on the card before we boot our Pi. This will give us a preset static IP without the need to connect a keyboard or monitor to the Pi!

The following video demonstrates these instructions. You may follow it and/or the instructions in the article.

The first thing we want to do is grab Raspbian Lite. Once it is downloaded, check the SHA-256 sum to make sure the download is legitimate and not corrupted:

$ sha256sum ~/Downloads/2017-11-29-raspbian-stretch-lite.zip
e942b70072f2e83c446b9de6f202eb8f9692c06e7d92c343361340cc016e0c9f /home/carpie/Downloads/2017-11-29-raspbian-stretch-lite.zip

(Note: Your download directory and file name may differ!) The SHA-256 sum should match the one on the Raspbian download site.

Now unzip it:

unzip /home/carpie/Downloads/2017-11-29-raspbian-stretch-lite.zip

We want to install the resulting image onto a microSD (hereafter SD) card. Go ahead and place the SD card into your computer. If you take a look at the output of dmesg you will see the device containing the SD card:

$ dmesg
...
[833761.2855523] mmc0: new ultra high speed SDR104 SDHC card at address 59b4
[833761.2855808] **mmcblk0**: mmc0:59b4 EB2MW 29.8 GiB
[833761.2855523]  mmcblk0: p1

The output above is from my system. You can see my SD card is at /dev/mmcblk0. You can also see that it has one partition p1. If your system auto mounts SD cards on insert like mine does, you should unmount before continuing:

umount /dev/mmcblk0p1

Now we can write the image to our SD card. Please make sure that the device you identified above really is your SD card and not your hard drive before you run the next command!

sudo dd if=2017-11-29-raspbian-stretch-lite.img of=/dev/mmcblk0 bs=16M

This command may take a while. Please note that we are writing to the device (/dev/mmcblk0) not the partition (/dev/mmcblk0p1). The image file contains its own partitions and will completely overwrite any thing existing on the SD card.

Now remove the SD and reinsert. Your system should mount two partitions boot and rootfs. On my system these mount at /media/carpie/boot and /media/carpie/rootfs respectively.

The first thing we want to do, since this will be a headless system is to enable the SSH server. That is simple enough, we just write an empty file named ssh to the boot partition:

touch /media/carpie/boot/ssh

Now we want to configure the wireless ethernet controller, which in the Pi's case is named wlan0. As super user, edit /media/carpie/rootfs/etc/dhcpcd.conf and add the following lines at the bottom of the file:

interface wlan0
static ip_address=192.168.0.5/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Now edit, again as super user, /media/carpie/rootfs/etc/wpa_supplicant/wpa_supplicant.conf and add the following lines:

network={
    ssid="your_wifi_network_name"
    psk="your_wifi_password"
}

Also, at the top of that same file you should set your WiFi country code. The codes use the ISO 2-letter country code designation.

Now that that's done, we sync and unmount the partitions:

sync
sudo umount /dev/mmcblk0p1 /dev/mmcblk0p2

Remove the SD card from your computer, place it in your Raspberry Pi and apply power. Give the Pi some time to boot up and then from your computer, you should be able to SSH in:

ssh [email protected]

The default password is raspberry and is known to all. We need to change that immediately! Let's do that:

sudo raspi-config

Choose Change User Password and follow the prompts to set a new password. While you are in raspi-config, go ahead and customize whatever you like. I always go in to Localisation Options and set my locale, timezone, and WiFi country. Once you're done, select Finish and select Yes to let your Pi reboot.

There is one more thing I recommend doing any time you've installed a fresh Raspbian image. There is a good chance that some security updates (and other functional updates) have happened to the Raspbian packages since the image was built. We should do an update to get the latest changes:

ssh [email protected]
sudo apt -y update && sudo apt dist-upgrade

Congratulations! You now have a fresh wireless Pi with Raspbian Lite on your network at a static IP. No keyboard or monitor required!