Setting up a headless Raspbian server with a static IP

Update For a method of setting a static IP that doesn't require connecting a keyboard or monitor to your Pi, see my instructions either for a wired or wireless setup.

Updated Video instructions now included!

Several of my articles here on carpie.net make use of a Raspberry Pi as a headless server. Instead of explaining it in each article, being a proper programmer and all :), I've factored those instructions out into an article of their own.

Making a "headless" Pi just means that, once we've got a Pi setup, it can sit in a dusty, dreary corner of our home or office dutifully doing its job with nothing connected to it but power and maybe a network cable. Since we are setting up a headless server in this article, we will use the Raspbian Lite image for the Pi. This image is basically Raspbian without all the graphic desktop stuff which we don't need for a headless machine. First, go follow the Pi people's nice installation guide to get that image on to your SD/microSD card (which I will simply refer to as SD from here on) and then we can proceed.

Put the SD card you loaded with Raspbian in the Pi, hook up a monitor and keyboard and then power the Pi. When Raspbian boots, log in using the default user name pi and default password raspberry. We need to do a few Raspbian-specific set up steps to get going. Enter:

sudo raspi-config

You will be presented with a menu of options. The first task we are going to do is expand the file system. This allows Raspbian to access all the space on your SD card. Select Expand Filesystem, then press Enter. The system says the file system is expanding. Press Enter for OK and that step is done.

Next, let's change that default password since it is widely known. Select Change User Password, press Enter and follow the prompts. Don't forget the password! You'll need it to log in to the Pi from now on...

Select Internationalisation Options. This menu will let you change your locale, your time zone, keyboard layout etc. Do what you need here and then return the main menu. US users should be sure to change the keyboard layout to a US one as it defaults to GB. Otherwise, you're in for a surprise when you try to type a double quote :)

Select Finish and the let the Pi reboot (to expand the file system). When the system returns, login using pi and your new password.

Getting the Pi server on your network

By default, the Pi will use DHCP to get on your network. This is typically fine. However, using this method means each time your Pi boots it is likely to get a different IP address. This is not ok for our backup system. We need it to have the same address all the time. This is known as a static IP. There are a couple of ways to get a static IP for your Pi. If your network router is providing your DHCP service (it most likely is) and has the concept of "address reservation" it is easiest to use that. Just get the MAC address of your Pi and put that with a static address of your choosing in your router's reservation system. You can get your MAC address from your Pi by typing:

ifconfig -a

First choose the network interface you want to use (eth0 is your wired network interface and wlan0 is your wireless interface). Then look for the colon-separated 6-section field following HWaddr for your MAC. For example, the MAC address of the following machine's wired interface is b8:27:eb:00:01:02:

ifconfig -a

eth0      Link encap:Ethernet  HWaddr b8:27:eb:00:01:02
...

The other option is to leave your router out of the picture and just set a static IP for your Pi. In our example, we assume your router's IP is 192.168.0.1. we'll choose a theoretical unused IP address of 192.168.0.5 for our Pi's wired interface. First, edit /etc/dhcpcd.conf and add the following lines:

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

If you are using the wireless interface, replace interface eth0 above with interface wlan0. Also, in the wireless case only you'll need to edit /etc/wpa_supplicant/wpa_supplicant.conf and add the following lines:

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

Obviously, replace ssid and psk with the values appropriate for your wifi network.

Once you have the network settings where you want them, reboot to make sure the network will come up on boot:

sudo reboot

When the system comes back up, log in and you should have a working network connection. You can check your IP status by:

ifconfig -a

eth0      Link encap:Ethernet  HWaddr b8:27:eb:00:01:02  
          inet addr:192.168.0.5  Bcast:192.168.0.255  Mask:255.255.255.0
...

Check connectivity to your router:

ping -c2 192.168.0.1

PING 10.90.9.1 (10.90.9.1) 56(84) bytes of data.
64 bytes from 10.90.9.1: icmp_seq=1 ttl=64 time=0.195 ms
64 bytes from 10.90.9.1: icmp_seq=2 ttl=64 time=0.155 ms
...

You now have a Pi with a static IP on your network! At this point, your Pi can become headless. This means you can now remove the monitor and keyboard connections. You can access the Pi now through SSH. From your host machine try:

ssh [email protected]

You should be able to log in using pi and the password you set up. At this point, you can follow the rest of this guide either using SSH in this manner, or using the keyboard/monitor setup for direct access.

Updating Raspbian to get latest security fixes

There is a good chance that Raspbian has had updates since the image you downloaded was made. Now that we have a working network, we can update to get any security fixes and other updates. Let's do that before proceeding:

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

Summary

In this article, we have installed a "headless" RaspberryPi with Rasbpian Lite. We've assigned it a static IP so that its IP does not change on every boot. At this point, we could use our Pi as a DHCP/DNS server, or as a automated backup server, or any number of other things!