When working with Linux servers, particularly in cloud environments or automated deployments, we often need to rename network interfaces persistently without relying on udev rules or system reboots. The standard method using ip link set
only provides temporary changes that don't survive restarts.
The basic commands for interface renaming are:
ifconfig peth0 down
ip link set peth0 name eth0
ifconfig eth0 up
While this works for the current session, we need a way to make this change persistent across reboots.
Here are three reliable methods to achieve persistent renaming:
Method 1: Network Manager Hook (For Systems Using NetworkManager)
# Create a dispatcher script
cat > /etc/NetworkManager/dispatcher.d/99-rename-interface << 'EOF'
#!/bin/bash
INTERFACE="peth0"
NEW_NAME="eth0"
if [ "$1" = "$INTERFACE" ] && [ "$2" = "up" ]; then
ip link set $INTERFACE down
ip link set $INTERFACE name $NEW_NAME
ip link set $NEW_NAME up
fi
EOF
chmod +x /etc/NetworkManager/dispatcher.d/99-rename-interface
Method 2: Systemd Networkd Configuration (For Systemd-based Systems)
# Create a network configuration file
cat > /etc/systemd/network/10-rename.link << 'EOF'
[Match]
MACAddress=xx:xx:xx:xx:xx:xx
[Link]
Name=eth0
EOF
Method 3: Init Script for Traditional Systems
# Create an init script in /etc/init.d/
cat > /etc/init.d/rename-interfaces << 'EOF'
#!/bin/sh
### BEGIN INIT INFO
# Provides: rename-interfaces
# Required-Start: $network $remote_fs
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Rename network interfaces
### END INIT INFO
case "$1" in
start)
ip link set peth0 down
ip link set peth0 name eth0
ip link set eth0 up
;;
*)
echo "Usage: $0 start"
exit 1
;;
esac
EOF
chmod +x /etc/init.d/rename-interfaces
update-rc.d rename-interfaces defaults
- Always verify interface MAC addresses before creating persistent rules
- When using systemd-networkd, ensure the service is enabled:
systemctl enable systemd-networkd
- For cloud instances, check vendor-specific documentation as they may have additional requirements
Interface renaming during system boot adds minimal overhead. However, if you're working with network-intensive applications:
- Schedule the renaming early in the boot process
- Avoid renaming interfaces that are part of bond or bridge configurations
- Test the impact using
time
commands during development
For AWS instances where you need to rename the primary interface from ens5 to eth0:
# Create udev rule as fallback
echo 'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", KERNEL=="ens5", NAME="eth0"' > /etc/udev/rules.d/70-persistent-net.rules
# Add pre-up command to interfaces file
echo -e "auto eth0\niface eth0 inet dhcp\n pre-up ip link set ens5 down && ip link set ens5 name eth0" >> /etc/network/interfaces
Many Linux administrators know the basic commands for temporary interface renaming:
ifconfig peth0 down
ip link set peth0 name eth0
ifconfig eth0 up
However, this change won't persist across reboots. The traditional approach using udev rules (70-persistent-net.rules) isn't always desirable, especially in environments where you need more control over the naming process.
For a persistent solution without udev, we can create a custom startup script. Here's how to implement it on Debian/Ubuntu systems:
1. Create the Renaming Script
sudo nano /usr/local/bin/rename_interface
Add the following content:
#!/bin/bash
# Bring down the interface
ifconfig peth0 down
# Rename the interface
ip link set peth0 name eth0
# Bring up the new interface
ifconfig eth0 up
2. Make the Script Executable
sudo chmod +x /usr/local/bin/rename_interface
3. Create a Systemd Service
For systems using systemd (most modern distributions):
sudo nano /etc/systemd/system/rename-interface.service
Add this configuration:
[Unit]
Description=Network interface renaming
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/rename_interface
[Install]
WantedBy=multi-user.target
4. Enable and Start the Service
sudo systemctl enable rename-interface.service
sudo systemctl start rename-interface.service
For systems not using systemd, you can add the commands to /etc/network/interfaces:
auto eth0
iface eth0 inet dhcp
pre-up ifconfig peth0 down
pre-up ip link set peth0 name eth0
After implementing the solution, verify with:
ip link show
Common issues include:
- Interface not being down before renaming
- Script execution order conflicts with network manager
- Missing dependencies (iproute2 package)
For more complex scenarios where you might need to rename based on certain conditions:
#!/bin/bash
# Check if peth0 exists
if ip link show peth0 >/dev/null 2>&1; then
ifconfig peth0 down
ip link set peth0 name eth0
ifconfig eth0 up
fi