Home / Alt manpages / nameif(8)

  • nameif(8)
  • Admin command
  • linux

Rename a Network Interface by MAC Address with nameif

Swap a motherboard or add a NIC and Linux can rename your interface, breaking scripts and firewall rules that assumed it would stay put. nameif fixes that by pinning a chosen name, such as lan0, to the card's MAC address. Budget 15 minutes for a normal rename, longer if the interface is carrying live traffic and needs a proper maintenance window.

Checkpoint

This changes a kernel network-interface name. Work from a local console or an out-of-band session if the target interface is your current SSH link. The rename has to happen before the interface comes up, and it normally needs root.

1. Read the installed manual first

This guide follows the installed nameif(8) page, supplied here by net-tools 2.10-0.1ubuntu4.4. It documents two ways to feed it names: a configuration file (normally /etc/mactab) or one interface name and MAC address given directly on the command line. One limit worth knowing up front: it only works with Ethernet interfaces.

$ command -v nameif
/usr/sbin/nameif
$ dpkg-query -W -f='${Package} ${Version}\n' net-tools
net-tools 2.10-0.1ubuntu4.4
$ nameif --help
usage: nameif [-c configurationfile] [-s] {ifname macaddress}

--help is not actually a supported flag on this build, which is why it exits non-zero after printing usage. That output is still enough to confirm the option shape. Do not borrow long options you have seen documented for a different release; this one does not have them.

2. Note the real interface name and MAC address

Pull the current name and hardware address with an ordinary, unprivileged command. Below, OLD_IFACE stands in for the physical Ethernet interface you are about to rename. Do not lift the MAC from a different host or a virtual bridge; it has to match the physical card.

$ ip -br link
OLD_IFACE       UP             90:1b:0e:da:cc:ef <BROADCAST,MULTICAST,UP,LOWER_UP>
$ ip link show dev OLD_IFACE
3: OLD_IFACE: <BROADCAST,MULTICAST,UP,LOWER_UP> ...
    link/ether 90:1b:0e:da:cc:ef brd ff:ff:ff:ff:ff:ff

Your interface number, flags and MAC address will all look different. Copy the address exactly, all six hex pairs. Then check the new name is not already taken:

$ ip link show dev NEW_IFACE
Device "NEW_IFACE" does not exist.

If that turns up an existing interface, stop there. A name collision is something to fix before you attempt the rename, not something nameif will sort out for you.

3. Write a minimal mactab entry

The default configuration file is /etc/mactab. Each non-comment line is just the new interface name followed by an Ethernet MAC address, and comments start with #. Keep the file small while you are testing, and pick a name your network manager and firewall rules will also recognise.

# new-name ethernet-mac-address
NEW_IFACE 90:1b:0e:da:cc:ef

Create or edit it with elevated privileges:

$ sudoedit /etc/mactab
$ sudo chmod 0644 /etc/mactab
$ sudo sed -n '1,20p' /etc/mactab
# new-name ethernet-mac-address
NEW_IFACE 90:1b:0e:da:cc:ef

The chmod is optional if the file already has a sensible mode. Do not put passwords, shell commands or interface settings on these lines: nameif only reads names and MAC addresses, it does not touch addresses, routes or DHCP.

4. Take the interface down before renaming it

This is the step that disrupts service. Bringing down a connected interface interrupts traffic and can drop your own terminal session, so save your work first and use an out-of-band console if there is any doubt.

$ sudo ip link set dev OLD_IFACE down
$ ip link show dev OLD_IFACE
3: OLD_IFACE: <BROADCAST,MULTICAST> ...

Some network managers will happily bring the interface straight back up while you are mid-task. If that happens, pause the manager using your distribution's normal maintenance procedure, then confirm the interface stayed down. Do not bolt a service-specific stop command onto this guide: the right unit name differs between installations, and guessing wrong here wastes the whole exercise.

5. Run the rename

Run nameif with elevated privileges while the target interface is still down. With no arguments it reads /etc/mactab and works through its entries.

$ sudo nameif
$ ip link show dev NEW_IFACE
3: NEW_IFACE: <BROADCAST,MULTICAST> ...
    link/ether 90:1b:0e:da:cc:ef brd ff:ff:ff:ff:ff:ff

A successful run prints nothing at all, so check both the new name and the MAC address to be sure. If nameif reports an error, do not bring the interface up yet: check the spelling, the MAC address, duplicate names, file permissions and interface state, in that order.

For a one-off rename that leaves /etc/mactab untouched, use the command-line form instead:

$ sudo nameif NEW_IFACE 90:1b:0e:da:cc:ef

Pointing at a different configuration file works the same way, with -c and its path:

$ sudo nameif -c /path/to/mactab NEW_IFACE 90:1b:0e:da:cc:ef

Do not mix a one-off test with a stale file entry unless you have checked every entry that mode will process; a leftover line can rename something you were not expecting.

6. Bring it back up and fix what still points at the old name

Renaming changes the identifier everything else refers to. Update any network-manager profile, firewall rule, routing script or monitoring check that still mentions OLD_IFACE, then bring the new interface up and check it:

$ sudo ip link set dev NEW_IFACE up
$ ip -br link show dev NEW_IFACE
NEW_IFACE       UP             90:1b:0e:da:cc:ef <BROADCAST,MULTICAST,UP,LOWER_UP>
$ ip addr show dev NEW_IFACE
3: NEW_IFACE: ...
    link/ether 90:1b:0e:da:cc:ef ...

The exact flags and address lines will depend on the host. If a network service owns the interface, use that service's own reload or reconnect operation once its configuration is updated: a renamed link does not automatically inherit the old link's IP configuration from every network manager.

7. Undo a rename that went wrong

If the rename failed outright, leave the interface down and read the error before retrying. The most common trap, and the one the manual explicitly warns about, is running nameif while the interface is still up.

To reverse a rename that succeeded but was not what you wanted, use the old name with the same MAC address, still with the interface down:

$ sudo ip link set dev NEW_IFACE down
$ sudo nameif OLD_IFACE 90:1b:0e:da:cc:ef
$ ip link show dev OLD_IFACE
3: OLD_IFACE: <BROADCAST,MULTICAST> ...
    link/ether 90:1b:0e:da:cc:ef ...

If /etc/mactab now permanently points at the new name, edit or remove that line before the next boot or rename attempt. Deleting a configuration line is a deliberate change, so keep a backup until you have tested the replacement network configuration.

Done means

  • Matched the interface by MAC. You identified the target Ethernet interface by its actual hardware address, not a guess.
  • Picked a free name. You confirmed the new name was not already in use before renaming.
  • Recorded the mapping. The rename is in /etc/mactab, or you used the one-off command form deliberately.
  • Renamed with the interface down. You brought it down first, as the manual requires.
  • Verified and updated everything downstream. The new name and MAC checked out, and dependent configuration was updated to match.
  • Know the way back. You can reverse the rename if the new name causes trouble.