Skip to content

[DRAFT]

When you plug your device into the network, you will be assigned an IP address from the router. For most use cases, this is all that is needed, but for servers it is strongly recommended that the IP address is made static.

This means ensuring that no matter how many times the device is rebooted / disconnected and reconnected it will always get the same IP address.

Depending on your operating system / device. There are many ways to achieve this, we will list a few here.

Raspbian / Raspberry Pi OS

First, lets make take some notes about our current network settings with the following command:

ip r | grep default

This will spit out something along the lines of:

default via 192.168.1.254 dev eth0 proto dhcp src 192.168.1.80 metric 202`

The first IP address here (192.168.1.254) is the IP address of our router. The second one (192.168.1.80) is the IP address that the router has assigned our Raspberry pi. Depending on your requirements, you can either choose to keep this IP or change it entirely. Just make sure that the IP address you choose is not already in use.

We also need to know the DNS server that has been set. to do this run the following command:

cat /etc/resolv.conf

Which will output something along the lines of:

# Generated by resolvconf
nameserver 192.168.1.254

We are interested in the IP address next to nameserver.

Now that you have the above IP addresses noted down, it's time to modify dhcpcd.conf to set it as static. To do so, first run this command:

sudo nano /etc/dhcpcd.conf

This will bring up a text editor (nano) - into which you can enter your desired settings.

One other thing we will need to know is the interface. If you are plugged into the network via a cable (strongly recommended), then you will want eth0, or if you have connected via WiFi, then you will want wlan0

For demonstration purposes, we should now have the following noted down:

$ROUTER=192.168.1.254
$STATIC_IP=192.168.1.80
$NAMESERVER=192.168.1.254
$INTERFACE=eth0

Scroll all the way to the bottom of this file and enter the following lines (replacing $VARIABLEs with your own values):

interface $INTERFACE
static ip_address=$STATIC_IP/24
static routers=$ROUTER
static domain_name_servers=$NAMESERVER

To save: press CTRL + X, then Y and then press ENTER to confirm.

Finally, reboot your Raspberry Pi to apply the new settings.

All Operating Systems

Providing you do not wish to use Pi-hole as a DHCP server, you should be able to set a static IP address from the router, however each router will be different. At a high level, the basic idea is usually along the same lines:

  • Head into the router settings - usually by typing in your router's IP, i.e 192.168.0.1.
  • Find the LAN DHCP Settings.
  • Assign desired IP address to the device

Last update: August 10, 2021
Back to top