Table of Contents
Using a static IP on Raspberry Pi will come in handy when you need to remember the IP such as using FTP or setting it up to act as a NAS.
In this Raspberry Pi static IP tutorial, we will see how to configure your Raspberry Pi’s network so that it utilizes a 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 the 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.
We make use of the Raspberry pi OS operating system in this tutorial.
1. Information about current Network
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.109 metric 100
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. Retrieve current DNS Server
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 8.8.8.8
Make a note of the IP next to “nameserver
“. This will define the name server in our next few steps.
3. Configure static IP on Raspberry Pi in dhcpcd.conf file
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
Within this file, enter the following lines.
interface <NETWORK> static ip_address=<STATICIP>/24 static routers=<ROUTERIP> static domain_name_servers=<DNSIP>
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 the following changes:
– 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
“.
Example:
interface eth0 static ip_address= 192.168.0.109 static routers= 192.168.0.1 static domain_name_servers= 8.8.8.8
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 go ahead and restart the Raspberry Pi.
sudo reboot
4. Test the Configuration
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.
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.
Hi, I’m Vivek, a Senior Embedded Innovation Specialist. I have been working on Embedded Systems and IoT for the past 11 years. I love to share my knowledge and train those who are interested. Nerdyelectronics.com was started out of this interest. You can read my full profile in this link.