How to Configure Static IP Address on eth0 Using nmcli Without DHCP Leak in Linux Networking


4 views

When configuring static IP addresses with NetworkManager's nmcli, many administrators encounter a peculiar situation where both static and DHCP-assigned addresses appear on the interface. Here's what we typically see in ip a output:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether dc:a6:32:3b:22:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.1.222/24 brd 172.17.1.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.17.12.14/16 brd 172.17.255.255 scope global dynamic noprefixroute eth0
       valid_lft 602857sec preferred_lft 527257sec

The proper method involves several steps to ensure DHCP is completely disabled:

# First, create a new connection profile
sudo nmcli connection add con-name "static-eth0" ifname eth0 type ethernet ip4 172.17.1.222/24 gw4 172.17.0.1

# Set DNS servers
sudo nmcli connection modify "static-eth0" ipv4.dns "172.17.0.221,172.17.0.220"

# Critical step: Disable DHCP completely
sudo nmcli connection modify "static-eth0" ipv4.method manual
sudo nmcli connection modify "static-eth0" ipv4.ignore-auto-dns yes
sudo nmcli connection modify "static-eth0" ipv4.dhcp-send-hostname no
sudo nmcli connection modify "static-eth0" ipv4.dhcp-client-id ""

# For IPv6 (if needed)
sudo nmcli connection modify "static-eth0" ipv6.method ignore

# Apply the changes
sudo nmcli connection down "static-eth0"
sudo nmcli connection up "static-eth0"

After applying these changes, verify with:

ip a show eth0
nmcli connection show "static-eth0" | grep -E 'ipv4\.(method|address|dns)'

If you still see DHCP-assigned addresses, check for these common issues:

  • Multiple active connection profiles for eth0
  • DHCP server pushing persistent leases
  • NetworkManager caching old configurations

For systems where NetworkManager isn't required, you can configure the interface directly:

# /etc/network/interfaces (Debian/Ubuntu)
auto eth0
iface eth0 inet static
    address 172.17.1.222
    netmask 255.255.255.0
    gateway 172.17.0.1
    dns-nameservers 172.17.0.221 172.17.0.220

# Disable NetworkManager for this interface
sudo nmcli device set eth0 managed no

When configuring static IP addresses on Linux systems using NetworkManager's nmcli, a common issue arises where both static and DHCP-assigned addresses appear on the interface simultaneously. This occurs because:

  • The connection profile isn't properly disabling DHCP
  • Multiple active connection profiles might exist for the same interface
  • NetworkManager's automatic connections feature might be interfering

Here's how to properly configure a static IP without DHCP interference:

nmcli connection add con-name "static-eth0" ifname eth0 type ethernet ip4 172.17.1.222/24 gw4 172.17.0.1
nmcli connection modify "static-eth0" ipv4.dns "172.17.0.221 172.17.0.220"
nmcli connection modify "static-eth0" ipv4.method manual
nmcli connection modify "static-eth0" ipv4.ignore-auto-dns yes
nmcli connection modify "static-eth0" ipv4.dhcp-timeout 0
nmcli connection modify "static-eth0" connection.autoconnect yes

After applying the configuration:

# Verify single IP assignment
ip addr show eth0

# Check active connections
nmcli connection show --active

# Delete any unwanted DHCP profiles
nmcli connection delete "Wired connection 1"

For reference, here's what the correct configuration file (/etc/NetworkManager/system-connections/static-eth0.nmconnection) should contain:

[connection]
id=static-eth0
uuid=5da74c14-d9da-4e15-90c9-5f37913d5610
type=ethernet
interface-name=eth0
autoconnect=true

[ipv4]
address1=172.17.1.222/24,172.17.0.1
dns=172.17.0.221;172.17.0.220;
dns-search=
method=manual
ignore-auto-dns=true
dhcp-timeout=0

[ipv6]
addr-gen-mode=stable-privacy
method=ignore

If you still see DHCP-assigned addresses:

  1. Restart NetworkManager: systemctl restart NetworkManager
  2. Disable other profiles: nmcli connection down "Wired connection 1"
  3. Check for multiple active connections: nmcli device show eth0