How To Set A Static IP Address On Raspberry Pi
Some Raspberry Pi projects aren't paired with a display and input accessories (a.ka. operating headless), and for good reason. First, the more peripherals you connect to the Pi, the more current it draws, which isn't ideal if you're running only on battery. Then, there's the issue of size and portability. A display, keyboard, and mouse can make the project bulky. So if the developers don't need a user interface to make the project work, they just do away with those components completely.
However, even without these peripherals, you can still control the Raspberry Pi remotely using your Linux terminal or apps like AnyDesk or TeamViewer. Doing so, though, often requires that you know your board's IP address. The problem is, your router might assign a new IP address to your Pi from time to time.
That means whenever you want to access the board, you'll have to go through the hassle of opening a network scanning tool like Fing or Angry IP scanner to figure out what this new IP address is. To avoid extra steps, you can simply assign a static IP address to your board. We'll walk you through some of the ways to set a static IP address on Raspberry Pi before deploying your project.
Preliminary steps
Before you configure a static IP address for your Raspberry Pi, you'll need to retrieve four key details first: the Pi's current IP address, the gateway (your router's IP address), the subnet mask (both in the CIDR notation and dot-decimal notation), and (optionally) the DNS server if you're using a custom one, like a different Raspberry Pi set up as Pi-Hole, for instance. This information is required when setting a static IP address as inputting the wrong or invalid values could prevent your board from connecting to the network. Here's how to get these details:
- Launch the Terminal app (if you're using the desktop version of the Raspberry Pi OS) or continue in the command line interface (if you're on the Lite version).
- Type ifconfig.
- Check the details for wlan0 if you're connected to Wi-Fi or eth0 if you're plugged to the router via LAN cable.
- Take note of the inet and netmask details. The inet value is your current IP address, while the netmask is the subnet mask in dot-decimal notation.
- In the command line interface (CLI), enter ip route.
- Write down the value after the words "default via." This is your router's IP address.
- Look for the IP address that's followed by a slash. The value after the slash is your subnet mask in CIDR notation.
- Back in the CLI, input cat /etc/resolv.conf.
- Remember the IP address next to "nameserver" as this is your DNS server's IP address.
Now that you have your network details, you can proceed with setting your static IP address.
Method 1: Reserve a static IP address for your Pi on your router
The recommended way of setting a static IP address on your Raspberry Pi is directly from the router itself. Here's a guide for three popular brands:
- For NETGEAR routers:
- Log into the admin interface using your username and password.
- In the Basic tab, select Attached Devices.
- Find your Raspberry Pi from the list of connected devices.
- Write down its Device Name and MAC and IP addresses.
- Go to the Advanced tab.
- Expand the Setup menu on the left.
- Choose LAN Setup.
- Under Address Reservation, press Add.
- Fill in the IP Address, MAC Address, and Device Name with the Pi's details you noted earlier.
- Hit Apply to save.
- For ASUS routers:
- Access the admin interface on your browser.
- When prompted, enter your login credentials.
- Under Advanced Settings on the left of the screen, select LAN.
- Go to the DHCP Server tab.
- Select Yes for "Enable the DHCP Server" and "Enable Manual Assignment."
- Under "Manually Assigned IP around the DHCP list" at the bottom, expand the MAC address menu.
- Choose your Raspberry Pi from the list of connected devices.
- Change the last figure of the IP address.
- Press the plus icon to save it.
- Click Apply.
- For TP-Link routers:
- Sign into the admin interface.
- Click on Advanced at the top.
- Navigate to Network > DHCP Server.
- Under Address Reservation, press Add.
- In the Add a Reservation Entry dialog box, click View Existing Devices.
- Choose your Pi.
- Type your preferred IP address.
- Enter a short description of what the device is in the Description field.
- Select Enable This Entry.
- Hit OK to save the details.
It's important to note the process will vary depending on your router manufacturer. So make sure to check your manufacturer's manual.
Method 2: Work from the graphical user interface (GUI)
If your router doesn't support static IP assignments, and your Raspberry Pi is running the desktop version of the Raspberry Pi OS, it's easy to assign a static IP address from the Network Manager interface. Here's how:
- On your desktop, click on the network (a Wi-Fi icon or up and down arrow icon) in the upper right corner.
- Select Advanced Options from the menu.
- Go to Edit Connections.
- In the Network Connections dialog box that appears, choose your current connection (found under Wireless if you're on Wi-Fi or under Ethernet if you're using a LAN cable).
- Press the config icon at the bottom.
- When prompted, type in your password.
- Navigate to the IPv4 Settings tab.
- Change the Method from Automatic (DHCP) to Manual.
- Hit Add to enter your static network details.
- Under the Address column, input the IP address of the Pi you took note of earlier. You can opt to change it to your preferred IP address, but make sure to only edit the last set of digits (if that address is 192.168.1.120, just 120) to make it valid.
- Under the Netmask column, write your network's netmask in the dot-decimal notation.
- Under the Gateway column, type your router's IP address.
- (Optional) Add a DNS server in the DNS servers field.
- Click on Save to save your changes.
- Close the Network Connections window.
- Reboot your Pi for the changes to take effect.
After the system restarts, check whether your IP address is now set to your preferred address by hovering over the network icon and checking the network details that show up.
Method 3: Use the NetworkManager Command Line Interface (nmcli)
So you're running on the Lite version of the Raspberry Pi OS and don't have a GUI to use. In that case, you can turn to the NetworkManager Command Line Interface (nmcli) instead. The nmcli is similar to the NetworkManager interface on the GUI, but, as its name suggests, it's accessible on the command line interface and operates entirely on text commands. Here's a step-by-step guide to use the nmcli to set a static IP address on your Raspberry Pi:
- From the command line interface or Terminal app (if you have a GUI), type "sudo nmcli connection show" to retrieve the name of the network configuration you're currently using. It's typically the first entry on the table.
- Assign an IP address to this network configuration by typing: sudo nmcli con modify "[your network name]" ipv4.addresses [your Pi's IP address]/[your subnet mask in CIDR notation]. So for instance, your command can look like this: sudo nmcli con modify "MyWiFi" ipv4.addresses 192.168.1.32/24.
- Set the default gateway for the network configuration by entering: sudo nmcli con modify "[your network name]" ipv4.gateway [your router's IP address]. Here's how it might appear: sudo nmcli con modify "MyWiFi" ipv4.gateway 192.168.1.1.
- (Optional) If you also want to set the DNS server, input: sudo nmcli con modify "[your network name]" ipv4.dns [your DNS server's IP address], as in: sudo nmcli con modify "MyWiFi" ipv4.dns 8.8.8.8.
- Set the IPv4 method to manual by typing: sudo nmcli con modify ipv4.method manual.
- To apply the new settings, restart the Raspberry Pi by typing: sudo reboot.
Once the Pi powers up, you can now test whether the assigned IP address was retained. In the CLI, type: hostname -I. It should then output your new IP address.
Method 4: Assign the IP address from the NetworkManager Text User Interface (nmtui)
If you're not comfortable working with text-only commands in the nmcli, a more user-friendly alternative is the NetworkManager Text User Interface (nmtui). It's also accessible from the command line like the nmcli, but instead of text commands, it displays an interface you can navigate more easily. Follow these steps to use the nmtui to set a static IP address for your Pi:
- In the CLI, enter sudo nmcli connection show to view your network configuration's name.
- Type: sudo nmtui edit "[your network's name]" as in if your network name is MyWiFi, the command should be: sudo nmtui edit "MyWiFi".
- In the interface editor that appears, navigate to IPv4 Configuration.
- Switch the value to Manual by pressing on Automatic, highlighting Manual, and hitting Enter on your keyboard.
- Expand the IPv4 Configuration details by going to Show on the right using your arrow keys and pressing Enter.
- Select Add next to Addresses.
- Fill the Addresses field with your Pi's IP address and the subnet mask in CIDR notation. So, your input can be 192.168.1.178/24.
- Move to the Gateway field.
- Type your router's IP address.
- (Optional) Add the DNS server's IP address by pressing Add next to DNS servers and inputting the address.
- Select OK at the bottom of the interface to save the changes.
- Back in the CLI, type: sudo systemctl restart NetworkManager to restart the NetworkManager and set the new configuration.
- Check whether the IP address has been set by typing: hostname -I.
You can also opt to restart the Pi once and then check the IP address again to make sure the newly set address will survive reboots. With your static IP address set, start tinkering and building your Raspberry Pi projects.