Setting up a static IP to prevent the Pi's IP Address from changing.
1. Before we 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
Using this command, you should get a result similar to the one we have below.
default via 192.168.68.1 dev wlan0 proto dhcp src 192.168.68.109 metric 600
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.68.1. This IP address is the current router address.
You may also want to get the current DNS server, which you can do by following the next section.
1. Let us retrieve the current DNS server.
We can do this by opening the resolv.conf configuration file by running the following command.
sudo nano /etc/resolv.conf
Below is the output that you should see from the above command.
# Generated by NetworkManager
nameserver 192.168.2.1
nameserver 207.164.234.193
Make a note of the IP next to nameserver.
Exit the nano editor by pressing CTRL + X.
You should now have the router IP and the nameserver IP. You can now move to network tools for Raspberry Pi OS Bookworm or DHCP for old versions of the OS.
This section will go through the steps of setting a static IP address on your Raspberry Pi if you are running the latest version of Raspberry Pi OS.
1. You can use either nmcli or nmtui to access the network manager tools on your Raspberry Pi. We will use nmtui as it offers a text-based GUI that is a bit easier to understand and navigate than the nmcli (Command Line) version.
Enter the following command to bring up the network tools in your terminal.
nmtui
2. On the first screen, you will be greeted with three options. Select edit a connection.
3. On this page, you will see all your possible network connections.
Navigate to the one that you wish to have the static IP. In our case, it is BELL117 (2), which is a Wi-Fi connection.
4. You will now see a range of information about the connection you selected. To set a static IP address, you will want to navigate to <Automatic> next to IPv4 CONFIGURATION and change this to <Manual>.
Now navigate across to <SHOW> and press enter.
You should see a range of fields that you need to fill out. We will cover each of these fields in the next step.
6. You will need to fill in the following fields with the relevant information.
Addresses (1): Enter the IP address you want to assign to your Raspberry Pi. Ensure this is not an IP that could be easily attached to another device on your network. (Use 192.168.68.109)
Gateway (2): Enter the router IP address you retrieved earlier in this tutorial.
DNS Servers (3): Enter the IP of the domain nameserver you want to utilize. This address is the nameserver IP you wrote down from earlier in this tutorial. Alternatively, you can use a third-party one such as Google’s “8.8.8.8”.
7. Navigate down to the bottom of the screen and select <OK>.
8. Now select the <BACK> option.
9. On the last page, you will want to select Quit.
10. For our changes to take effect, you will need to restart the network manager. Luckily, this is as simple as running the following command.
sudo systemctl restart NetworkManager
You will lose the network connection for a few seconds, but the connection should recover.
11. You can now proceed to testing the static IP address.
1. Once your Raspberry Pi has rebooted, you should be able to connect using the IP address you specified.
If you are connecting locally and want to verify that the static IP address is set correctly, you can run the hostname command.
hostname -I
From this command, you should now be able to see your static IP address. If it is the IP you expected, you have successfully set up a static IP address on your Raspberry Pi.
192.168.0.109