Skip to main content

The name game: Naming network interfaces in Linux

Name your Linux network interfaces like you would your children. Easy to remember and hard to mix-up.
Image
blackboard with names on it

Image by Pexels

Network interfaces are an absolutely vital part of any business today. From mom-and-pop shops to Fortune 100 companies, everyone is leveraging the network for logistics and communications. Most, if not all, of these businesses can directly tie their bottom line to their network in some way—if the network goes down or someone accidentally cycles the wrong interface, money is on the table. It is for this reason that the system/network administrator needs to pay careful attention anytime they are working on or around the network interfaces.

One of the most basic and sometimes overlooked tasks can be the naming convention for the various interfaces that you depend on every day. I have worked with small companies that only had one or two interfaces for their entire environment. In this case, a naming convention is less of a problem. However, for larger enterprise environments, naming your network interfaces is vital to long term success. Imagine how easy it would be to make a mistake because all of your interfaces are similarly named.

No need to panic though! The brilliant Linux/Unix community saw this issue coming and developed multiple ways to avoid it. I want to take a look at three different commands: nameif, ip link, and ifrename. You can utilize each of these tools to name and interact with your interfaces in various ways.

nameif

The nameif command is an older command that is officially deprecated. However, there are still those out there using it for specific tasks. The one and only goal of this command is to name an interface according to the MAC address of the device(s). For example:

[root@rhel ~]# nameif -c /etc/mactab -s 08:00:27:30:52:a7

The -c option allows you to specify from which configuration file you wish to read. By default, it uses /etc/mactab. The -s flag simply lets the system know to send any error messages to the syslog. You then give it the MAC address of the interface you are renaming. Easy day.

Note that you must run nameif before bringing the interface up, or it will fail.

ifrename 

Network interface names are dynamic by default and are assigned on a first-come, first-served numerical basis: eth0, eth1, eth2, and so on. The ifrename tool allows you to assign a consistent name to your interfaces. You can set this up in several ways. However, the most common usage here is to set the name based on—you guessed it—the MAC address. It makes sense to do it this way since MAC addresses are unique to the device. Much like nameif, you must run ifrename before the interfaces are brought up to avoid failures. This tool also pulls data from configuration files; however, this time, we are using /etc/iftab (unless otherwise specified). Let's look at how to use it.

Syntax: [root@rhel ~]# ifrename [-c configfile] [-i interface] [-n newname]

-c - allows you to specify a config file other than /etc/iftab

-i - allows you to rename only a specific interface rather than all system interfaces

-n - allows you to specify the new name of the interface (use with -i)

-D - allows the user to enter "dry-run" mode. Here, ifrename won't change the name of the interface. It only prints the new name and return. This is a great way to make sure that you have the command correct before running for real.

-V - gives verbose output with additional information used for debugging issues.

ip link

The ip command is the Swiss army knife of Linux networking commands. We are looking specifically at ip link, the command used to name and modify network interfaces.

To list all interfaces, use the following:

[tcarrigan@rhel ~]$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
        link/ether 08:00:27:30:52:a7 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:b7:7b:c7 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
        link/ether 52:54:00:b7:7b:c7 brd ff:ff:ff:ff:ff:ff 

Notice that there is no IP address information given with this command. You need to use the ip addr command to meet that need. For specific interface information, use the following:

[tcarrigan@rhel ~]$ ip link show dev enp0s3 
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
        link/ether 08:00:27:30:52:a7 brd ff:ff:ff:ff:ff:ff

To change the name of an interface, we use the following:

[tcarrigan@rhel ~]$ ip link set eth0 name eth123

Note that you must disable the interface, change the name, and then bring it back up for the changes to take place. You also need to modify the /etc/sysconfig/network-scripts/ifcfg_old_device_name to the new device name in both the title and content of the file.

Wrap up

I had a supervisor who used to say, "There is more than one way to skin a cat." Although there are more polished ways to say it, the point is that there is always an alternative way to complete a task. This is proven with almost every operation that an administrator needs to accomplish. Even something as simple as naming interfaces presents choices for the admin to make. What are your thoughts? If presented with the situation, which of these commands would you use to get the job done?

[ Want more for your network? Download a free ebook on network automation with Ansible. ]

Topics:   Networking   Linux  
Author’s photo

Tyler Carrigan

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.