Setting Up A Headless Raspberry Pi On Linux

Most of the Raspberry Pi guides online are targeted for newbies want to set up their device using a keyboard and monitor. This is going to be a no-nonsense guide for people who already basically know what they’re doing, and just want to get a headless Raspberry Pi set up for SSH access. All you’ll need for this guide is an SD card and an Ethernet cable.

First, download the latest Raspbian Lite image from: https://www.raspberrypi.org/downloads/raspbian/ . Unzip the image, and you should get a file named something like 2017-07-05-raspbian-jessie-lite.img. At the time I write this, the latest image is Raspbian Jessie Lite. Since Debian Stretch was just marked as stable, I expect this to be updated soon.

Plug your SD card into your computer, make sure it’s fully unmounted, and then copy the disk image onto the card. The should be a command like this (run as root):

# Flash the raspbian disk image to your SD card
cat 2017-07-05-raspbian-jessie-lite.img > /dev/mmcblk0

The exact file name you use may differ. The device name could also be different, although the one I listed should be the default. After the file copy finishes, run the sync command to make sure everything is flushed. Now you need to resize the partitions. You can find a full guide here. I would recommend first running fdisk -l /dev/mmcblk0 which will print out the sector size and sector count for your card. This will help you do the math for creating the partitions. In my case, I created a new 1 GB swap partition at the end of the card, and then resized partition 2 (the root partition) to be larger. If fdisk asks you about erasing the ext4 filesystem signature, say no.

Once you’re happy with this, you need to set up the Raspberry Pi to launch SSH on boot. There’s a trick for this: if a file named /boot/ssh exists, the Raspberry Pi will launch sshd on boot. Mount partition 1 (which is the boot partition) from the SD card, and then use touch to create a file named ssh in that partition. For me that command was:

# GNOME decided to automount the partition at /run/media/evan/boot for me
sudo touch /run/media/evan/boot/ssh

At this point you can unmount everything, remove the SD card from your computer, and plug it into your Raspberry Pi. Boot up the Raspberry Pi with an Ethernet cable connected to a router. Once it comes up, you should be able to find the IP address given to the Raspberry Pi in your router admin interface. Once you’ve figured out what IP address it has, you can login using the username pi and the default password raspberry:

# Default password is: raspberry
ssh pi@192.168.42.75  # or whatever IP address it has

Did you log in? If so, great! First, enable sshd to start by default in the future. You’ll want to do this before rebooting the Raspberry Pi, since the /boot/ssh file gets deleted on boot:

# Ensure SSH starts by default in the future
sudo systemctl enable ssh

Now, log out, and copy your keys over (this should be run from your laptop):

# Again, replace with the actual IP you have
ssh-copy-id pi@192.168.42.75  # or whatever IP address it has

If that worked, disable SSH password auth on the Raspberry Pi:

# Run this on the rpi; set PasswordAuthentication to No
sudo vi /etc/ssh/sshd_config

If you created a swap partition earlier, you’ll likely notice that it’s not being used. The default behavior on Raspbian is to use a 100 MB swap file, not a partition. To correct this, edit /etc/fstab to have a line like:

/dev/mmcblk0p3        swap            swap    defaults          0       0

Now run sudo swapoff to disable swap. Then run sudo mkswap /dev/mmcblk0p3. After running this command, sudo swapon should mount the swap partition.

If you’re like me and have your locale set to en_US.UTF-8 you may need to enable that locale on the Raspberry Pi, or you’ll get annoying localization errors when connecting:

# Enable whatever locale you want
sudo dpkg-reconfigure locales

Since you just set everything up, you may want to do a firmware update at this point:

# Update firmware.
sudo rpi-update

That’s it. You should be able to take things from here. Now would probably be a good time for you to set up wpa_supplicant, but I’ll leave that as an exercise to the reader.