Home Raspberry Pi Raspberry Pi: Static IP Set-up

Raspberry Pi: Static IP Set-up

by Behrad
192 views

In one of the previous post I wrote how you could install the 64bit OS on your RPi 3 or 4. Now it’s time to give your Paspberry a permanent place in your network by assigning it an static IP adres.

What is DHCP?

In almost every home router DHCP is enabled by default. DHCP stands for Dynamic Host Configuration Protocol and makes sure that your laptop receives an IP from the router so you can browse the internet. Usually these IP’s are “leased” for a specific set of time. In most cases you’ll see 86400 seconds which is 1 day.

In case your router. or the device itself, reboots then this IP might change. You can prevent that by changing the IP to a static one. Within IT this is usually done on servers, NAS devices and network devices. Why? Because you want your server to always run, and therefore be accessible with the same IP. Even after a reboot. Since I’m going to use RPi as my “sever” I want to assign it a static IP. This way my RPi will always have one and the same IP till I manually change it again. This process is a very simple one.

Assigning a static IP on your Raspberry Pi

First of all open Putty of MPutty and connect to your RPi, Now we need to make sure that we have a couple of information ready and verified; Our router & DNS server IP addresses. So let’s start with finding out our Router IP adres with the command:

ip r | grep default

The above command will give you an output similar to the below:

default via 192.168.10.1 dev eth0 src 192.168.10.150 metric 202

This tells you that:

  • 192.168.10.1 = is your router and you need to write this down.
  • eth0 = you’re connected to your RPi using a cable
  • 192.168.10.150 = Is the current IP assigned to your RPi
  • Metric 202 = Metric is a measure of priority in networking. Routes with a lower “metric” are preferred to ones with a higher metric.

In most cases your router is also your DNS server. But don’t take my word for it and let’s search for this information. You can exit the output with CTRL + X.

sudo nano /etc/resolv.conf

The output will look like this and now you also have the IP adres of your DNS server.

# Generated by resolvconf
nameserver 192.168.10.1

Before we actually change the IP adres, you need to decide if you would like to assign the static IP to the “eth0” or “wlan0”. I have mine connected with a cable so I’ll stick to that. Also you need to think about which IP you’ll be using for your RPi. So now it’s time to actually change the IP adres with the below command:

sudo nano /etc/dhcpcd.conf

Locate the following #Example static IP configuration in the config file. First remove the # at the begin of the lines to make the line “active” and replace:

  • <NETWORK> = eth0 or wlan0. Depending on your decision.
  • <STATICIP> = The IP adres of your choosing. TIP: Have a look on your routers DHCP to see if the IP adres which you have in mind is actually free! 😉
  • <ROUTERIP> = Your routers IP adres. This you have noted down after our first command.
  • <DNSIP> = Your DNS IP adres. This you have noted down after our second command.
interface <NETWORK>
static ip_address=<STATICIP>/24
static routers=<ROUTERIP>
static domain_name_servers=<DNSIP>

You can save the file by pressing CTRL + S or CTRL + X, followed by “Y” + Enter.

In order for changes to be completed a reboot is needed. You can do this with the following command:

sudo reboot

After the reboot, which will take a minute or two, you should be able to connect to your RPi using your newly assigned IP adres. This way your RPi will always remain the IP adres you’ve assigned until you change it again using the very same steps as you just followed!

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More