How to setup `static` ip address in Raspberry Pi | LinuxGist
This article will provide details on how you can setup static ip address on a Raspberry Pi or other Linux systems.
Introduction
An IP address (Internet Protocol address) is a unique identifier assigned to each device on a computer network that uses the Internet Protocol for communication. An IP address consists of four numbers separated by dots, each ranging from 0 to 255.
For example: 192.168.1.100
Setting Up a Static IP Address in Raspberry Pi
When you connect your Raspberry Pi to a network, it can either receive an IP address dynamically (from the DHCP server) or statically. A static IP address provides more control and predictability.
Prerequisites
- A Raspberry Pi running a Debian-based operating system (e.g., Raspbian).
- An internet connection.
- Administrator access to your Raspberry Pi.
Steps to Set a Static IP Address on Raspberry Pi
Log in to Your Raspberry Pi
Connect to your Raspberry Pi either via SSH, directly through the console, or using a monitor and keyboard.
Open Terminal
Edit the Network Configuration File
For Raspbian (or similar Debian-based systems), you can edit the network configuration file for your Ethernet or Wi-Fi interface.
For
dhcpcd(most common):1
sudo vi /etc/dhcpcd.confFor
NetworkManager(if you’re using it):1
sudo vi /etc/NetworkManager/system-connections/your-connection-name
Configure the Static IP Address
Depending on which file you edited, follow these steps:
For
dhcpcd.conf:Add or modify the following lines for your Ethernet interface (usually
eth0):1 2 3 4
interface eth0 static ip_address=192.168.1.100/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 8.8.8.8
Replace
eth0with your interface name if it’s different (e.g.,wlan0for Wi-Fi).For
NetworkManager:Add the following lines under the
[ipv4]section in your connection file:1 2 3 4
[ipv4] method=manual address1=192.168.1.100/24,192.168.1.1 dns=192.168.1.1;8.8.8.8;
Save and Exit
Restart the Network Service
Apply the changes by restarting the network service:
1
sudo systemctl restart dhcpcd # For dhcpcd
Or for
NetworkManager:1 2
sudo nmcli connection reload sudo nmcli connection up your-connection-name
Verify Configuration
Check if the IP address has been set correctly by running:
1
ip addr show eth0 # Replace eth0 with your interface nameYou should see the static IP address you configured.
Alternative: Using ifconfig (Deprecated)
In older versions of Raspbian, you might use ifconfig, but this is deprecated in newer systems. The above steps using dhcpcd or NetworkManager are more modern and recommended practices.
Example Output
1
2
3
4
5
6
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:XX:XX:XX brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 86399sec preferred_lft 86399sec
inet6 fe80::ba27:ebff:feb8:xxxx/64 scope link
valid_lft forever preferred_lft forever
In this output, inet under eth0 shows the static IP address you configured.
Conclusion
By following these steps, you have successfully set a static IP address on your Raspberry Pi. This ensures that your device always has the same IP address on the network, making it easier to access and manage.