Many times, we will not want to dedicate our Raspberry Pi to a “normal” computer with a screen and a keyboard, but we will dedicate it to permanent applications.
Examples of permanent applications include, among many others, a file server, a web server, home automation applications like controlling lights, setting up an alarm, or a thermostat, and even when building robots.
In these cases, having a static IP is a very important requirement, if not almost essential, in order to connect to our Raspberry at all times from another device.
What is a static address?
Briefly, let’s remember that within each network, a device is identified by a unique IP in that network. Through the IP address, we can connect to our device easily.
This IP can be:
- Dynamic, it can change every time we restart the device.
- Static, it remains fixed between restarts.
In many cases, our devices connect to the local network through a dynamically assigned IP provided by the DHCP (Dynamic Host Configuration Protocol). In this case, our router acts as a DHCP server and assigns us an IP address within a range of addresses.
Dynamic IP addresses by DHCP have the advantage of giving us the freedom to not have to configure the devices, nor ensure that each one has a different IP. Simply, you turn on your computer, laptop, your mobile phone, your (…whatever…) and the router assigns them an IP without you having to worry about anything.
However, if we have a permanent installation, we will generally want the IP address to be fixed. Because you wouldn’t be happy if you try to access your files, or turn on the lights at home and you can’t connect because the IP has changed, right?
To set up a static IP, we must make sure it is outside the DHCP range (you will have to check this in the configuration of your router) and that no other device has the same IP assigned.
Setting up a static IP in Raspbian
To set up a static IP in Raspbian, we must edit the file /etc/dhcpcd.conf with the command:
sudo nano /etc/dhcpcd.conf
If we look at the contents of the file, we will see some commented lines (starting with ’#’) that have an example of static IP configuration
## Example static IP configuration:
#static domain_name_servers=192.168.0.1 22.214.171.124 fd51:42f8:caae:d92e::1
To create our own static IP address, for example for the WiFi interface (wlan0), we copy the commented fragment and modify it to look like this.
static domain_name_servers=192.168.1.1 126.96.36.199
- interface = Name of the interface we want to configure
- static ip_address = Fixed address we want (leave the /24 at the end)
- static routers = Gateway address (router)
- static domain_name_servers = DNS server address (usually that of the router, or external ones like Google’s 188.8.131.52). If you want more than one DNS server, you can add them separated by a space.
Next, we save the changes by pressing ‘Ctrl+X’ and then confirming with ‘Y’ and ‘Enter’. Now we restart the Raspberry Pi with the command:
Finally, we check that we do indeed have the IP we have configured by executing the command
And we have the static IP configuration. In the next posts, we will see how to configure remote access via SSH or VNC for which it will be very convenient (can you guess what?) to have a static IP set up on the Raspberry Pi. Until the next post!