In this post, we will see how to configure a static IP in Raspberry Pi Panel PC working with Raspbian operating system through the command console.
In many occasions, we will not want to dedicate our Raspberry Pi to a “normal” computer with screen and keyboard, but we will dedicate it to permanent applications.
Examples of permanent applications are, among many others, a file server, a web server, home automation applications such as controlling lights, making an alarm, or a thermostat, and even when making robots.
In these cases, having a static IP is a very important requirement, if not almost essential, to connect with our Raspberry at all times from another device.
Does your project require a dynamic or static IP?
Through the IP address, we can connect with our device in a simple way.
This IP can be:
Dynamic, it can change every time we reboot the device.
Static, remains fixed between restarts.
In many cases, our devices are connected 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 of not having to configure the devices or make sure that each one has a different IP. Simply, turn on your computer, laptop, your mobile, your (… whatever …) and the router is assigned IP without you to worry about anything.
However, normally if we have a permanent installation we will want the IP address to be fixed. Because you will not be thankful that you are going to access your files, or turn on the lights of your house, and you can not connect because the IP has changed, right?
To configure a static IP, we must ensure that it is outside the DHCP range (you will have to verify it in the configuration of your router) and that no other device is assigned the same IP.
To configure a static IP in Raspian we must edit the file /etc/dhcpcd.conf with the command:
sudo nano /etc/dhcpcd.conf
If we look at the content of the file, we will see some commented lines (they begin with '#') that have an example of static IP configuration:
# Example static IP configuration:
#static domain_name_servers=192.168.0.1 126.96.36.199 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 so that it looks like this.
interface = Name of the interface that we want to configure
static ip_address = Fixed address we want (leave the / 24 at the end)
static routers = Address of the gateway (of the router)
static domain_name_servers = Address of the DND server (usually the router). If we want more than one DNS server, you can add them separator for a space.
Then 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 verify that, effectively, we have the IP that we have configured running with the command.
And we already have the static IP configuration.