How to Assign a Static IP to an Ethernet Interface in Linux

Configuring a static IP for your Ethernet interface on Linux can seem daunting at first, but it’s an essential skill, especially if you’re managing servers or working in a home lab. A static IP ensures your device maintains the same IP address, which is crucial for tasks like hosting services or ensuring stable network connections.
This guide will walk you through how to assign a static IP to your Ethernet (eth) interface in Linux.
What Is a Static IP, and Why Do You Need One?
A static IP address is a fixed IP that doesn’t change, unlike a dynamic IP assigned by a DHCP server. Here are some reasons you might need a static IP:
Server Setup: Hosting a web server or FTP service requires a stable address.
Networking Devices: Ensures printers, NAS devices, or other peripherals are always accessible.
Improved Connectivity: Reduces conflicts in networks with multiple devices.
In Linux, assigning a static IP involves editing network configuration files or using network management tools, depending on your distribution.
Steps to Assign a Static IP on Linux
Here’s how to give your Ethernet interface (e.g., eth0) a static IP address. We’ll cover methods that work across most Linux distributions.
1. Identify Your Network Interface
Before assigning an IP, you need to know your Ethernet interface name. Use the following command:
ip addr
Look for an interface starting with eth or enp, such as eth0 or enp0s3. Note this name for later steps.
2. Backup Configuration Files
Always back up existing configuration files before making changes. For example, if you’re editing a configuration file:
sudo cp /etc/network/interfaces /etc/network/interfaces.bak
This ensures you can revert changes if something goes wrong.
3. Configure a Static IP Using the interfaces File
This method works for Debian-based distributions like Ubuntu.
Open the network configuration file:
sudo nano /etc/network/interfacesAdd the static IP configuration for your Ethernet interface:
auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4Replace
192.168.1.100with your desired static IP,255.255.255.0with your subnet mask, and192.168.1.1with your gateway.Save and exit by pressing
Ctrl + O, thenCtrl + X.Restart the networking service:
sudo systemctl restart networking
4. Using Netplan on Ubuntu (Modern Method)
Netplan is the default network configuration tool for newer versions of Ubuntu.
Edit the Netplan YAML file:
sudo nano /etc/netplan/01-netcfg.yamlAdd or modify the Ethernet configuration:
network: version: 2 ethernets: eth0: dhcp4: no addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]Replace the IP address, gateway, and nameservers with your desired values.
Apply the configuration:
sudo netplan apply
5. Using nmcli on Red Hat-Based Systems
For Fedora, CentOS, or RHEL, you can use nmcli, the NetworkManager command-line tool.
Check your current connection:
nmcli connection showNote the connection name for your Ethernet interface.
Modify the connection to use a static IP:
nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24 nmcli connection modify eth0 ipv4.gateway 192.168.1.1 nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4" nmcli connection modify eth0 ipv4.method manualRestart the connection:
nmcli connection up eth0
6. Verify the Configuration
After applying your changes, verify the static IP is correctly assigned using:
ip addr show eth0
Ensure the IP matches your configuration. You can also test connectivity with:
ping 8.8.8.8
7. Common Troubleshooting Tips
If you encounter issues, try these steps:
Restart Network Services: Use
sudo systemctl restart networkingorsudo systemctl restart NetworkManager.Check Configuration Files: Ensure there are no typos or syntax errors in your configuration.
Inspect Logs: Use
sudo journalctl -xeto identify errors related to networking.
Conclusion
Assigning a static IP to your Ethernet interface in Linux ensures consistent connectivity and is crucial for many network configurations. Whether you’re using Debian, Ubuntu, Fedora, or CentOS, this guide provides a step-by-step process tailored to your distribution. Remember to verify your configuration and troubleshoot any issues to maintain a reliable network setup.
FAQs
1. Why should I use a static IP address?
A static IP is essential for tasks like hosting a server, accessing shared devices, or managing network stability. It ensures your device’s IP doesn’t change over time.
2. Can I switch back to DHCP after setting a static IP?
Yes, you can revert to dynamic IP assignment by modifying your configuration files and setting the interface back to DHCP.
3. What is the difference between Netplan and NetworkManager?
Netplan is a YAML-based configuration tool used primarily in Ubuntu, while NetworkManager is a general-purpose network management tool used in various distributions like Fedora and CentOS.
4. How do I find my gateway and DNS settings?
Use the ip route command to identify your gateway. DNS settings are usually provided by your network administrator or ISP.
5. Can I assign multiple static IPs to one interface?
Yes, you can assign multiple IPs by adding additional addresses entries in your configuration file, such as Netplan or NetworkManager settings.
