Setting up a Static IP Address on the Raspberry Pi



1. To begin setting up a static IP address on our Raspberry Pi, we will first need to retrieve some information about our current network setup.

Let’s first retrieve the currently defined router for your network by running the following command.

ip r | grep default

Copy

By using this command, you should get a result similar to the one we have below.

default via 192.168.0.1 dev eth0 proto dhcp src 192.168.0.159 metric 202

Make a note of the first IP mentioned in this string.

For example, the IP that we will make a note of from this command is “192.168.0.1“. This IP address is the current router address.

2. Next, let us also retrieve the current DNS server.

We can do this by opening up the “resolv.conf” configuration file by running the following command.

sudo nano /etc/resolv.conf

Copy

From this command, you should see the lines of text below.

# Generated by resolvconf

nameserver 192.168.0.1

Make a note of the IP next to “nameserver“. This will define the name server in our next few steps.

3. Now that we have retrieved both our current “router” IP and the nameserver IP we can proceed to modify the “dhcpcd.conf” configuration file by running the command below.


This config file allows us to modify the way the Raspberry Pi handles the network.

sudo nano /etc/dhcpcd.conf

Copy

4. Within this file, enter the following lines.

First, you have to decide if you want to set the static IP for your “eth0” (Ethernet) connector or you “wlan0” (WiFi) connection. Decide which one you want and replace “<NETWORK>” with it.

Make sure you replace “<STATICIP>” with the IP address that you want to assign to your Raspberry Pi. Make sure this is not an IP that could be easily attached to another device on your network.

Replace “<ROUTERIP>” with the IP address that you retrieved in step 1 of this tutorial

Finally, replace “<DNSIP>” with the IP of the domain name server you want to utilize.

This address is either the IP you got in step 2 of this tutorial or another one such as Googles “8.8.8.8” or CloudFlare’s “1.1.1.1“.

interface <NETWORK>

static ip_address=<STATICIP>/24

static routers=<ROUTERIP>

static domain_name_servers=<DNSIP>

Now save the file by pressing CTRL + X then Y followed by ENTER.

5. Now that we have modified our Raspberry Pi’s DHCP configuration file so that we utilize a static IP address, we need to go ahead and restart the Raspberry Pi.

Restarting the Raspberry Pi will allow our configuration changes to be loaded in and the old ones flushed out.

Upon rebooting, the Raspberry Pi will attempt to connect to the router using the static IP address we defined in our “dhcpd.conf” file.

Run the following command to restart your Raspberry Pi.

sudo reboot


Comments

Popular Posts