Assigning a static IP on the Raspberry Pi Compute Module 4 running Raspbian 11 requires editing the dhcpcd.conf file instead of relying on older methods. This guide explains how to correctly configure the network interface for a persistent IPv4 address, ensuring your device remains reachable on the local network after reboots.
Understanding Network Management in Bullseye
Raspbian 11 (Bullseye) utilizesdhcpcd5 as the default DHCP client and router daemon. Unlike previous versions where users might have modified /etc/network/interfaces, the modern approach centralizes configuration in a single dedicated file. This change simplifies management but can confuse users accustomed to legacy Debian networking tools. The system reads this configuration at boot to establish network parameters before other services start.
It is crucial to distinguish between the dynamic IP assigned by your router and the static IP you wish to enforce. A static IP ensures that the Raspberry Pi always requests the same address from the DHCP server or uses a manually defined one. This stability is vital for headless setups, server applications, or when accessing the device via SSH without checking the router’s lease table repeatedly. If you are also managing software dependencies, you may find our guide on Installing Node.js: Replacing Direct Installation with NVM on Raspberry Pi 3 (Raspbian) helpful for maintaining consistent environments across network changes.
Preparation and Prerequisites
Before modifying network settings, you must identify the correct network interface name. Modern Linux systems use predictable network names, which often look likeeth0 for wired connections or wlan0 for wireless. You can verify your interface by running ip addr show in the terminal. Look for the interface associated with your active connection; it will display an IP address and MAC address.
You should also determine the IP address range of your local network. For example, if your router assigns addresses like 192.168.1.x, your static IP must fall within this subnet but outside the DHCP pool to avoid conflicts. Check your router’s administration page to see which range is reserved for automatic assignment. Picking an address outside this range, such as 192.168.1.200, minimizes the risk of IP collisions. Additionally, knowing your gateway and DNS servers is essential for proper routing. These details are typically found in the output of ip route and cat /etc/resolv.conf.
Editing the Configuration File
The primary configuration file fordhcpcd is located at /etc/dhcpcd.conf. To edit this file, open a terminal and use a text editor with sudo privileges. The command sudo nano /etc/dhcpcd.conf is recommended for its simplicity and readability. Navigate to the bottom of the file using the arrow keys.
Append the following block of code to the end of the file, replacing the placeholder values with your specific network details:
interface eth0
static ip_address=192.168.1.200/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8
In this snippet, interface eth0 targets the wired connection. If you are using Wi-Fi, replace eth0 with wlan0. The static ip_address line defines your desired IP followed by the CIDR notation (/24 represents a 255.255.255.0 subnet mask). The static routers field specifies the default gateway, which is usually your router’s IP address. Finally, static domain_name_servers lists your DNS servers; using your router’s IP first and then a public DNS like Google’s 8.8.8.8 provides redundancy.
If you encounter issues with package management due to network misconfigurations, refer to Configuring Network Settings on a Broken APT System of Raspberry Pi for troubleshooting steps related to broken dependencies.
Applying and Verifying Changes
After saving the file withCtrl+O and exiting with Ctrl+X in Nano, you need to apply the changes. The simplest method is to reboot the Raspberry Pi using the command sudo reboot. Upon restart, the system will read the new configuration and assign the static IP.
Alternatively, you can restart the dhcpcd service without a full reboot by running sudo systemctl restart dhcpcd. However, this may not always apply cleanly to all interfaces depending on the current state of the network manager. Once the system is back online, verify the configuration by running ip addr show eth0. You should see your newly assigned static IP listed under the interface.
To ensure external connectivity works, test DNS resolution with nslookup google.com and check routing with ping -c 4 192.168.1.1. If these commands succeed, your static IP assignment is successful. If you face issues accessing web pages after this change, consult our article on Problems accessing home network web pages for broader network troubleshooting.
Troubleshooting Common Errors
If the Raspberry Pi fails to connect after applying the static IP, check for syntax errors in/etc/dhcpcd.conf. Ensure there are no typos in the IP address or missing slashes in the CIDR notation. Also, verify that the chosen IP is not already in use by another device on the network. You can scan the network using nmap or check your router’s connected devices list.
Another common issue arises if the interface name has changed. While eth0 is standard for wired connections, some setups might use different naming conventions. Always double-check the interface name using ip link before editing the config file. If you are using a USB-to-Ethernet adapter, the interface name might differ from the onboard port.
Frequently Asked Questions
How do I revert to a dynamic IP address?
Simply remove the lines you added to/etc/dhcpcd.conf or comment them out by adding a hash symbol (#) at the beginning of each line. After saving the file, restart the dhcpcd service or reboot the device to return to DHCP-assigned addressing.
Can I set a static IP for Wi-Fi connections?
Yes, the process is identical to wired connections. Just ensure you specifywlan0 instead of eth0 in the interface directive within the configuration file. Keep in mind that Wi-Fi static IPs require the SSID and password to be configured in /etc/wpa_supplicant/wpa_supplicant.conf or via the GUI network manager.
What happens if my router's gateway IP changes?
If your router’s IP address changes, your static configuration will break because thestatic routers field will point to an invalid destination. You must update the static routers value in /etc/dhcpcd.conf to match the new gateway IP and restart the network service.
Is it safe to use the same static IP for multiple devices?
No, assigning the same static IP to multiple devices on the same network segment causes IP conflicts. This results in intermittent connectivity loss for both devices. Each device must have a unique IP address, whether assigned statically or dynamically.Why does my Raspberry Pi lose internet access after setting a static IP?
This usually occurs if the DNS servers are not correctly specified. Without valid DNS servers, the device cannot resolve domain names, making it appear as though there is no internet. Ensure thestatic domain_name_servers line includes valid IPs, such as your router’s IP or a public DNS provider.