We'll need both your router IP address and name server IP. We can find this information by running a few commands in a terminal on the Pi. Remote into the Pi using SSH or open a terminal window from within Raspbian.
To find your router IP address, enter the following command:
The router IP address will appear after the text "default via"—take note of it. The name server can be found in the resolv.conf file. Open it using the following command.
ip r | grep default
Take note of the name server IP address and close the file with CTRL + X.
sudo nano /etc/resolv.conf
The static IP is set by adding it to a file on the Raspberry Pi. In the terminal window, run the following command to edit the dhcpcd.conf file.
sudo nano /etc/dhcpcd.conf
This document has a few lines of code that can be activated by removing the
# to the left of each line. Use the following ledger to properly set your static IP address.
- Network = If you're using a wired connection, set this to
eth0. If you're using a wireless connection, set this to
- Static_IP = This is the static IP address you want to assign to the Raspberry Pi.
- Router_IP = This is the IP address for the router.
- Name_Server = This is the name server address. You can use another DNS IP here if you'd like.
Enter your information into the file, be sure to remove the
<> brackets. Check the screenshot for an example.
interface <Network> static ip_address=<Static_IP>/24 static routers=<Router_IP> static domain_name_servers=<Name_Server>
When that's completed, save the file using CTRL + X.
When the changes have been made, restart the Raspberry Pi. Now is a good time to test your project and make sure the IP address isn't changing. Disconnect and reconnect your Pi from the network. If the IP address changes, verify the information in the previous step saved properly. If it stays the same, congratulations! You've set a static IP on the Raspberry Pi.