Language: EN

raspberry-pi-ip-estatica

Set up Static IP on Raspberry Pi

In this post, we will see how to set up a static IP on a Raspberry Pi running the Raspbian operating system through the command console.

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:

#interface eth0

#static ip_address=192.168.0.10/24

#static ip6_address=fd51:42f8:caae:d92e::ff/64

#static routers=192.168.0.1

#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

raspberry-pi-dhcpd

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.

interface wlan0
static ip_address=192.168.1.200/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8

Where,

  • 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 8.8.8.8). 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:

sudo reboot

Finally, we check that we do indeed have the IP we have configured by executing the command

ifconfig wlan0

raspberry-pi-ip-estatica-resultado

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!