How to Permanently Disable NIC (eth0) in CentOS: One-Liner Script for IP Release & Boot Prevention


2 views

When decommissioning servers or reconfiguring networks, properly disabling network interfaces requires more than just ifdown. Here's the comprehensive approach:

ifdown eth0 && \
nmcli connection delete eth0 && \
sed -i '/^iface eth0/d' /etc/network/interfaces && \
systemctl disable NetworkManager-wait-online.service

This compound command handles all requirements sequentially:

  • Interface Shutdown: ifdown eth0 immediately deactivates the interface
  • NetworkManager Cleanup: nmcli connection delete eth0 removes persistent configuration
  • Legacy Config Removal: The sed command strips eth0 declarations from interfaces file
  • Boot Prevention: Disabling the wait-online service prevents auto-reconnection attempts

For systems without NetworkManager:

ip addr flush dev eth0 && \
ifconfig eth0 down && \
chkconfig network off && \
sed -i '/eth0/,$!b;//{:x;N;$!bx};s/^/#/m' /etc/sysconfig/network-scripts/ifcfg-eth0

Confirm successful disablement with:

ip -4 addr show eth0 | grep -q "inet" && echo "FAIL" || echo "SUCCESS"
systemctl is-enabled NetworkManager-wait-online.service | grep -q disabled || echo "Boot check failed"

Common issues and solutions:

# If interface keeps reappearing:
echo "blacklist eth0" >> /etc/modprobe.d/blacklist.conf
depmod -a

# For RHEL/CentOS 8+ systems:
nmcli device set eth0 managed no

For system administrators needing to completely disable a network interface while maintaining script compatibility, here's the atomic solution:

ifdown eth0 && \
nmcli connection delete eth0 && \
systemctl disable NetworkManager-wait-online.service && \
echo -e "DEVICE=eth0\\nONBOOT=no" > /etc/sysconfig/network-scripts/ifcfg-eth0

This compound command performs four critical functions sequentially:

  1. Interface Shutdown: ifdown eth0 immediately deactivates the interface
  2. NetworkManager Cleanup: Removes any persistent NM configuration
  3. Boot Prevention: Stops network waiting services during startup
  4. Persistent Config: Writes the permanent disable setting to interface config

For systems using modern network management:

ip link set eth0 down && \
ip addr flush dev eth0 && \
chkconfig network off && \
sed -i '/^IPADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0

After execution, confirm the changes with:

ip addr show eth0              # Should show NO-CARRIER
nmcli device status            # Should show disconnected
ls /etc/sysconfig/network-scripts/ifcfg-eth0  # Should show ONBOOT=no

For complete interface eradication:

rm -f /etc/udev/rules.d/70-persistent-net.rules && \
echo "" > /etc/udev/rules.d/70-persistent-net.rules && \
udevadm control --reload-rules
  • If NetworkManager recreates the connection: nmcli con add autoconnect no ifname eth0
  • For legacy systems: Edit /etc/network/interfaces instead
  • When using bond/vlan: Remove slave relationships first