How to Configure Static and Dynamic IP Addresses in Linux
Whether you’re managing a server or a desktop, every Linux system needs a proper IP configuration. In this guide, you’ll learn how to set static and dynamic IP addresses using ip, Netplan, and NetworkManager — with real-world examples.
ip, Netplan, and NetworkManager. Step-by-step guide for both server and desktop environments.Introduction: Why IP Configuration Matters
Every Linux system needs an IP address to communicate — whether with a local network or the internet.
Without one, you can’t browse, update, or connect remotely.
By default, many systems use DHCP (Dynamic Host Configuration Protocol) to automatically obtain an IP.
However, in servers and production environments, administrators often prefer static IPs — stable, predictable addresses that never change.
In this guide, we’ll cover both methods, using:
- The
ipcommand (temporary configuration) - Netplan (modern Ubuntu/Debian configuration)
- NetworkManager (nmcli) (for desktops or RHEL-based systems)
⚙️ 1. Checking Your Current IP Address
Start by viewing your current configuration:
ip addr show
or simply:
ip a
Example output:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> eth0
inet 192.168.1.25/24 brd 192.168.1.255 scope global dynamic
eth0→ your network interface192.168.1.25/24→ IP address and subnet maskdynamic→ indicates DHCP-assigned address
🔄 2. Configuring a Temporary Static IP (Using ip Command)
If you need to assign a new IP temporarily (for testing):
sudo ip addr add 192.168.1.50/24 dev eth0
sudo ip route add default via 192.168.1.1
This assigns the IP and sets a default gateway.
To verify:
ip addr show dev eth0
To remove the IP:
sudo ip addr del 192.168.1.50/24 dev eth0
⚠️ Note:
This configuration is temporary. It will disappear after reboot or network restart.
Use Netplan or NetworkManager for persistent setups.
🧱 3. Setting a Static IP with Netplan (Ubuntu, Debian)
Modern Ubuntu/Debian versions use Netplan (YAML-based configuration).
Configuration files are located in /etc/netplan/.
📘 Example: /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
Apply Changes:
sudo netplan apply
Verify:
ip a show eth0
ping 8.8.8.8
✅ This configuration will persist after reboot.
🧠 Explanation of Each Field:
| Key | Meaning |
|---|---|
dhcp4: no | Disable DHCP |
addresses | Static IP and subnet mask |
gateway4 | Default gateway |
nameservers | DNS server IPs |
You can also configure IPv6 with addresses: [2001:db8::1/64].
💡 Troubleshooting Tip:
If networking fails after applying:
sudo netplan --debug apply
This will show detailed error output.
🔁 4. Configuring Dynamic IP (DHCP) via Netplan
If you prefer DHCP:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
Apply:
sudo netplan apply
Check:
ip a show eth0
You’ll see dynamic next to the IP address — confirming DHCP is active.
🧰 5. Configuring IPs with NetworkManager (nmcli)
On systems like Fedora, CentOS, or Ubuntu Desktop, NetworkManager is commonly used.
You can manage connections using nmcli.
🔹 View Active Connections
nmcli connection show
Example output:
NAME UUID TYPE DEVICEWired connection 1 8a7d2a1c-1e23-4d34-bc8a-a2b55a23a1f2 ethernet eth0
🔹 Assign a Static IP
sudo nmcli connection modify "Wired connection 1" \
ipv4.addresses 192.168.1.120/24 \
ipv4.gateway 192.168.1.1 \ipv4.dns "8.8.8.8 1.1.1.1" \
ipv4.method manual
Apply and reconnect:
sudo nmcli connection down "Wired connection 1"
sudo nmcli connection up "Wired connection 1"
🔹 Switch Back to DHCP
sudo nmcli connection modify "Wired connection 1" ipv4.method autosudo nmcli connection up "Wired connection 1"
🔍 Verify Configuration
nmcli device show eth0
You’ll see details including IP address, DNS, and gateway.
🧩 6. Editing Legacy Configuration Files (for Older Systems)
For older systems without Netplan or NetworkManager, you may still find:
📄 /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.1.50
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 1.1.1.1
Restart networking:
sudo systemctl restart networking
🌐 7. Verifying Network Connectivity
After setting IPs, always test your connection.
Test Basic Connectivity
ping -c 4 8.8.8.8
Test DNS
dig google.com
Trace the Route
traceroute 8.8.8.8
If DNS fails but pinging IPs works → DNS misconfiguration.
If pinging gateway fails → routing or interface issue.
🧠 8. Understanding DHCP Leases
DHCP dynamically assigns IPs from a pool.
To see your current lease:
sudo cat /var/lib/dhcp/dhclient.leases
Renew the lease:
sudo dhclient -r
sudo dhclient
Check logs:
journalctl -u NetworkManager | grep DHCP
🚀 9. Best Practices
✅ Use static IPs for servers, DHCP for desktops.
✅ Keep Netplan and nmcli configurations backed up.
✅ Avoid duplicate IPs — use ping before assigning.
✅ Always verify connectivity before deploying firewalls.
✅ Document your network setup (gateway, DNS, subnet).
🧭 Summary
Linux gives you multiple ways to configure networking — from quick temporary setups with ip, to persistent configurations with Netplan and NetworkManager.
You’ve learned how to:
- Assign static and dynamic IPs
- Configure gateways and DNS
- Test and troubleshoot connections
💡 Whether you’re setting up a home server or managing enterprise systems — mastering IP configuration is the foundation of Linux networking.
🔗 Next Steps
Continue building your Linux networking skills:
- Linux Networking Basics: Understanding IP, DNS, and Routing
- Firewall Configuration with UFW and iptables
- Troubleshooting Network Issues in Linux
Or explore our full “Linux Networking for SysAdmins” eBook on dargslan.com.