Setting up a headless networked Raspberry Pi

I’m sure most people know about the Raspberry Pi and all of the cool and interesting things you can do with but I’m interested in using my Pi as a headless networked computer.

First you need an image to boot the Pi, I use Raspbian because it’s close to Debian (which I know) and is built for arm hardfloat (which makes it faster on the Pi). SD card images for Raspbian can be found on the official download page.

Once you have written the image to an SD card, you need to find your Pi after it boots. Because Raspbian gets an IP address from DHCP and starts an SSH server by default, the easiest way to do this just scan your local network for systems running an SSH server. You can do this with nmap, the reason you need to run nmap as root is to get the mac address back off the wire.

sudo nmap -p 22 --open  192.168.1.0/24

You get something like this back, notice the “Raspberry Pi Foundation” label by the mac address, this means you’ve found a Pi.

Nmap scan report for 192.168.1.xxx
Host is up (0.00064s latency).
PORT   STATE SERVICE
22/tcp open  ssh
MAC Address: B8:27:EB:XX:XX:XX (Raspberry Pi Foundation)

Once you have found your Pi you need to login and set it up, the user name is pi and the password is raspberry you should really change this.

ssh -l pi <IP address>

First off updating, as raspbian is just a spin of Debian so we can just use apt-get to update.

sudo apt-get update
sudo apt-get dist-upgrade

You can also use apt-get to install packages as well but that’s a different tutorial.

The latest version of the Pi model B has 512MB of RAM which is much better and gives a lot more flexibility but as we want to run a headless Pi we really don’t need to give the GPU much ram so edit /boot/config.txt and add this line to end of the config file

gpu_mem=16M

As we don’t have a monitor plugged into the Pi there isn’t much point in running X windows and disabling it will save some ram.

update-rc.d lightdm disable

By default raspbian sets up a 100M swapfile on the SD card, I don’t really like that as it’s slow and can wear out the SD card so I disable swap.

sudo update-rc.d dphys-swapfile disable

There is a system in Linux called “zram” ie a compressed ramdisk we can use to swap to instead of the SD card. When it’s not in use the zram ramdisk takes up almost no memory so I tend to enable it on most systems I look after. I’ve hacked up an init script that setups a compress ramdisk 1/2 the size of the memory on the Pi and formats it as swap. To install this script download it, make is executable and enable it.

sudo wget -O /etc/init.d/zram http://co-lo.night-shade.org.uk/~tim/Pi/zram
sudo chmod 755 /etc/init.d/zram
sudo update-rc.d zram enable

The Raspberry Pi foundation call the Linux kernel that the Pi boots from the SD card firmware, however as the licensing is “complex” and not something that I’m going to comment on here you can’t update it with standard Debian tools. There is an update script from https://github.com/Hexxeh/rpi-update that will automate downloading and installing the latest firmware.

sudo apt-get install git-core
wget -O rpi-update https://raw.github.com/Hexxeh/rpi-update/master/rpi-update
chmod 755 rpi-update
sudo ./rpi-update

Finally we need to resize the main system partition on the SD card to use all the space the SD card, raspbian includes a method to do this in the config application raspi-config, but you can also just use sfdisk like this:

echo ",+," | sudo sfdisk --force -N2 --no-reread /dev/mmcblk0

Finally you will need to reboot for all of these changes to take effect.

Once the Pi has rebooted, final step is to login again and resize the root filesystem

sudo resize2fs /dev/mmcblk0p2

You now have a tiny little networked computer that you can use to do all sorts of things, I’ll post some examples of what you can do later.

One thought on “Setting up a headless networked Raspberry Pi”

Comments are closed.