Many Linux administrators encounter this behavior when working with multiple IP addresses on a single network interface. The standard deletion method often removes all IPs sharing the same network prefix, which isn't the desired outcome.
# Adding multiple IPs to eth2
ip addr add 192.168.1.100/24 dev eth2
ip addr add 192.168.1.101/24 dev eth2
# Attempting to remove just one (problematic way)
ip addr del 192.168.1.101/24 dev eth2 # Removes both!
The ip addr del
command interprets the network prefix (/24) as part of the address specification. When you try to delete one address with this prefix, it removes the entire address range associated with that network from the interface.
To specifically target a single secondary IP without affecting others, you need to use the exact IP address without the network mask:
# Correct way to remove specific secondary IP
ip addr del 192.168.1.101 dev eth2
Always check the remaining IP configuration after making changes:
ip addr show dev eth2
For more complex scenarios, consider these approaches:
# Using label (if configured)
ip addr add 192.168.1.101/24 dev eth2 label eth2:0
ip addr del 192.168.1.101/24 dev eth2:0
# Using scope (for specific use cases)
ip addr add 192.168.1.101/24 dev eth2 scope site
ip addr del 192.168.1.101/24 dev eth2 scope site
When managing multiple IPs on a single interface:
- Always omit the netmask when deleting specific secondary IPs
- Consider using interface aliases (eth2:0, eth2:1) for clarity
- Document your IP assignments for maintenance purposes
- Use consistent naming conventions for labeled interfaces
Here's a complete workflow for managing web server IPs:
# Adding main and SSL IP
ip addr add 203.0.113.10/24 dev eth0
ip addr add 203.0.113.11/24 dev eth0 label eth0:ssl
# Removing just the SSL IP later
ip addr del 203.0.113.11 dev eth0:ssl
# Verifying
ip addr show dev eth0 | grep inet
When working with multiple IP addresses on a single network interface in Linux, you might encounter this scenario:
# Adding two IPs to eth2
ip addr add 1.1.1.1/24 dev eth2
ip addr add 1.1.1.2/24 dev eth2
# Attempting to remove one IP
ip addr del 1.1.1.2/24 dev eth2
Surprisingly, both IP addresses get removed instead of just the specified secondary address. This behavior occurs because the ip addr del
command removes the entire network configuration when deleting from the same subnet.
To properly remove just the secondary IP while preserving the primary address, you need to use the label
parameter when adding the IP address:
# Add primary IP (no label needed)
ip addr add 1.1.1.1/24 dev eth2
# Add secondary IP with unique label
ip addr add 1.1.1.2/24 dev eth2 label eth2:0
# Now you can safely remove just the secondary IP
ip addr del 1.1.1.2/24 dev eth2 label eth2:0
For more complex scenarios where you need to manage multiple IPs independently, consider using network namespaces:
# Create a new namespace
ip netns add testns
# Move interface to namespace
ip link set eth2 netns testns
# Add IPs inside the namespace
ip netns exec testns ip addr add 1.1.1.1/24 dev eth2
ip netns exec testns ip addr add 1.1.1.2/24 dev eth2
# Remove specific IP
ip netns exec testns ip addr del 1.1.1.2/24 dev eth2
Always verify your IP configuration before and after making changes:
# Check current IP assignments
ip addr show dev eth2
# Alternatively using ifconfig
ifconfig eth2
# For namespace verification
ip netns exec testns ip addr show dev eth2
For persistent configurations across reboots, edit the network interface files:
# For Debian/Ubuntu systems
nano /etc/network/interfaces
# For RHEL/CentOS systems
nano /etc/sysconfig/network-scripts/ifcfg-eth2
Add the secondary IP with proper labeling in the configuration file to ensure it persists after system reboots.