21) Setup a Raspberry Pi Static IP Address

<-- Previous    Next –>

Fixing the IP address of your Raspberry Pi is useful where you want to connect to it using ftp or SSH. You do not have to have a static IP address on your local network but it may prove useful in some situations.

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

First, we need to identify the router of the network.

In the terminal enter:

ip r | grep default

This will result in something like:

default via dev eth0 proto dhcp src metric 202

The first ip address is the [ROUTERIP] in this case

The second is the IP address of the Raspberry Pi. You need to decide on what static IP address you want to use instead of this address.

The [STATICIP] is the IP address that you want to assign to your Raspberry Pi. Make sure this is not an IP that is being used by another device on your network.

Now we want to retrieve the DNS server.

In the terminal enter:

sudo nano /etc/resolv.conf

This will result in something like the following:

# Generated by resolvconf
domain home

The IP address is the [DNSIP]

We now need to modify the dhpcd.conf file

So in the terminal enter:

sudo nano /etc/dhcpcd.conf

We need to add the following to the file:

interface [NETWORK]
static ip_address=[STATICIP]/24
static routers=[ROUTERIP]
static domain_name_servers=[DNSIP]

In my case this is:

interface eth0
static ip_address=
static routers=
static domain_name_servers=

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

Now that we have modified our Raspberry Pi’s DHCP configuration file so that we utilize a static IP address, we need to 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 the dhcpd.conf file.

Run the following command to restart your Raspberry Pi.

sudo reboot

Once the Raspberry Pi has finished restarting you now should be able to connect using the IP address you specified.

If you are connecting locally and want to verify the static IP address set correctly, you can do that by running the following command.

hostname -I

From this command, you should now be able to see your new static IP address. If it is the IP that you expected, then you have now successfully set up a static IP address on your Raspberry Pi.

You should also be able to check the IP address by looking at your router configuration.

<-- Previous    Next –>