Raspberry Pi Static IP

Raspberry Pi Static IP

26/01/2023

Just a quick one to consolidate information from the internet.

Although very simple, setting up a static IP for your Raspberry Pi can sometimes lead to some problems.

If you are using your Raspberry Pi as a home server, or just connecting to it via SSH, it’s super useful just power it on, allow it to connect to a preconfigured network and have a local IP that you know, such as 192.168.0.22. The advantage of a static IP is that you know the local IP upon boot. If you don’t set a static IP, you’ll need to check the IP via the terminal on a monitor connected to the Raspberry Pi - or some other solution.

Local IPs

A quick word on local IP addresses. All IP addresses consist of 4 numbers, joined by a .. Each number falls in the range 0 - 255. Usually, local IPs begin with 192.168 since that is common practice. They range between 192.168.0.0 to 192.168.255.255

DHCP Configuration

Rasberry Pi OS uses Dynamic Host Configuration Protocol to assign the IP address to the Raspberry Pi. The specific client is the dhcpcd DHCP client daemon.

Some articles will ask you to check your nameserver to use later:

sudo nano /etc/resolv.conf

although I think that this seems to cause issues sometimes. I think that the best way is just to fire in with the static IP that you would like.

One thing that we do need, is your wifi router’s IP. You can grab this via this command:

ip r | grep default

To break this down a bit, the ip tool can be used to assign an IP to a network interface and configure parameters. In this case, we are using the r command which simply returns the routing table for the Raspberry Pi. We then “pipe” the output to the grep function which looks for lines with the word “default” in them.

I’ve already set up an IP to point to 192.168.0.22 so I get the following routing:

default via 192.168.0.1 dev wlan0 src 192.168.0.22 metric 303

Here I can see that the router IP is 192.168.0.1. It is expected that the router has a very low last number. Note this down since you’ll need it later.

Add a Static IP

We edit the dhcpcd config file, which lives in Linux’s system configuration folder /etc/. Let’s edit it via nano, but make a backup first!

sudo cp /etc/dhcpcd.conf /etc/dhcpcd.backup
sudo nano /etc/dhcpcd.conf

If something goes majorly wrong later, just delete the new dhcpcd.conf and rename the backup file.

Now all you need to do is add a chunk of code to the bottom of the file:

interface wlan0
static ip_address=192.168.0.22/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8

In the ip_address line, I have set the static IP address of my choice, followed by /24. Read the section on subnet masks if you want to know about the /24.

The routers line IP address should be IP address of your router which we found earlier.

Finally, the DNS (domain name server) is set to 8.8.8.8 which is Google’s DNS. Using this has led to fewer issues for me.

Finally, you can reboot the Raspberry Pi.

sudo reboot

To check that everything has worked correctly, use the ifconfig command to verify that your wlan IPv4 address is what you expected.

IP Address and Subnet Masks

As you may have worked out, an IPv4 address is just 32 bits split up into fout 8-bit octets. Subnetting splits up large networks into a group of smaller, interconnected networks to minimize traffic. This happens since traffic doesn’t have to flow through routes unnecessarily.

To break it down, on one network we can have 4,228,250,625 device addresses, which is a lot! When everyone is on the same network, and I want to send a message to another computer on the same network, I need to broadcast to every other device to find out which device matches the IP that I want to send to. This causes a lot of traffic and is not efficient.

When we subnet, we break the network up into smaller, more managable networks. This has the same capacity but now we have grouped devices. Each device’s IP now has a network and host part of the address. The way that we distinguish between the two parts is using the subnet mask. Now the broadcasting device only needs to broadcast to the networks which then broadcast to the appropriate device group.

Going back to our example, the format of IP/mask defines the address and the mask. /24 sets how many bits out of the 32 bits in a subnet mask should be 1s. I.e. if 24 bits are 1’s and the rest are 0’s:

11000000 10101000 00000000 00010110
192.168.0.22

11111111 11111111 11111111 00000000
255.255.255.0

Whereever there is a 1 in the subnet mask, this indicates that the corresponding IP bit should be marked as a network bit.

Therefore, since our subnet mask is 255.255.255.0, our IP is split into the network address of 192.168.0.0 and a host address of 0.0.0.22.

Here is a great Youtube Video that has some graphics and a more in-depth explanation than you’ll find here.