How to Setup a Raspberry Pi Static IP Address

In this Raspberry Pi static IP tutorial, we will show you how to configure your Raspberry Pi’s network so that it utilizes a static IP address.

Raspberry Pi Static IP Address

We will force the Pi to use a static IP by making modifications to the Raspberry Pi’s DHCP client daemon. This software controls the way the operating system handles IP addresses.

By Modifying the DHCPCD config file, we can tell the software the local IP address that it should use when connecting to a network.

In addition to defining the static IP address on your Raspberry Pi, we recommend that you also set it on your router.

Setting it on the router helps ensure that your router doesn’t assign the IP address to a different device before your Raspberry Pi connects.

You can also change the MAC address of the Raspberry Pi. This change can be handy in a variety of scenarios.

Equipment List

Here is a list of all the pieces of equipment that I made use of for this Raspberry Pi Static IP Address tutorial.

Recommended

Optional

Setting up a Static IP Address on the Raspberry Pi

We make use of the Raspbian operating system in this tutorial. You may or may not be able to set a static IP on other operating systems using these instructions.

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

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

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

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

Testing the Static IP

1. Once your 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.

Using a static IP will come in handy when you need to remember the IP such as using FTP or setting it up to act as a NAS.

I hope that this Raspberry Pi static IP tutorial has helped achieve your task. If you have any feedback on this tutorial, then please don’t hesitate to leave a comment.

20 Comments

  1. Avatar for Mark
    Mark on

    Step #3 does not work for me? I don’t get a file with the list of items that i need to changes in step #4.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Mark,

      Is the file completely empty? If so, what operating system are you running on your Raspberry Pi?

      If it’s not empty then that should be correct, you will be adding lines to the bottom of this file rather than changing ones inside it.

      Cheers,
      Emmet

  2. Avatar for Trevor
    Trevor on

    Excellent tutorial, simple and accurate.
    Thankyou.

  3. Avatar for Rockin Rog
    Rockin Rog on

    Chalk me up also to a BIG Thanks! Very nicely done and very easy to follow along! No more IP changes to the Pi now at random intervals!

  4. Avatar for George
    George on

    Thanks for your work in providing this and other tutorials. It is greatly appreciated!

  5. Avatar for adex77
    adex77 on

    thank you really much, i needed to check 3 tutorials and yours finally say that it should be “wlan0” and not “wlan” like by other tutorials

  6. Avatar for Ernie
    Ernie on

    I have a R Pi 4 with 8G ram and loaded with 64 bit OS. I have just run these steps and it still works great with 64 bit.

    Thank you

  7. Avatar for Ken
    Ken on

    A lot has changed recently on Rasbian OS & Buster. This is no longer the way to configure the network, but I don’t understand the new way yet!

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Ken,

      These steps should continue to function with Raspberry Pi OS (Raspbian) Buster.

      There has been no major changes in the way OS handles its DHCP client.

      Cheers,
      Emmet

    2. Avatar for LGG6_Master
      LGG6_Master on

      I did this on the new raspberry pi os (raspberry pi 4 4gb ram) and it worked perfectly fine

  8. Avatar for iCloudBart
    iCloudBart on

    Thank you very much for this easy to understand tutorial!
    The vertical bar was some search for my azerty windows keyboard (on the pi) lol.
    For anyone interested you have to hold down “altgr” and press the “& 1” key.

  9. Avatar for Jesse Burdecki
    Jesse Burdecki on

    Great guide, easy to follow and fixed my issue. Thanks!

  10. Avatar for Gabriele Zoppoli
    Gabriele Zoppoli on

    Great tutorial, worked immediately! Thanks

  11. Avatar for H
    H on

    hello, How can i get back to DHCP.

    1. Avatar for Gus
      Gus on
      Editor

      Remove the lines we added in step 4 from the dhcpcd.conf file, and reboot. That should revert you back to your original settings.

  12. Avatar for Larry Joy
    Larry Joy on

    At step Nr 2 for the command ‘sudo nano /etc/resolv.conf’ I get:
    # Generated by resolvconf
    nameserver 104.145.255.226
    nameserver 104.145.255.227
    I did not get “domain home” as the first line (after # Generated…)
    Which IP address to use? Or does it matter? Can you explain why two IP addresses?
    –Thank you and regards, Larry

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Larry,

      All you need is the two IP’s mentioned next to nameserver. There is two defined as one is used as a fallback if the first fails.

      In Step 7 when replacing you are able to add multiple IP’s as long as they are seperated by a space. So for your case you would end up with.

      static domain_name_servers=104.145.255.226 104.145.255.227

      Cheers,
      Emmet

  13. Avatar for Adam
    Adam on

    Something I have to do all the time and never remember. Thanks for the easy to follow tutorial!

  14. Avatar for ap
    ap on

    I noticed quite a few different angels of the same Pi case on here. Could you tell me where to get that case?

    1. Avatar for Gus
      Gus on
      Editor

      Hi,

      That is the Raspberry Pi 3 case from KKSB cases. They’re pretty cool designs, and their new Pi 4 case is nice as well.

Leave a Reply

Your email address will not be published. Required fields are marked *