Changing Ethernet Device Names in EL7 and Fedora 15+

From Alteeve Wiki
Jump to navigation Jump to search

 AN!Wiki :: How To :: Changing Ethernet Device Names in EL7 and Fedora 15+

Traditionally in Linux, network devices were given the name ethX, where X was a digit starting at 0 and incremented up. The drawback to this was that there was no consistent or deterministic way to know which physical network ports would get which ethX name. To address this, Fedora 15 (and other distributions) began using a new tool called biosdevname which names each network device based on where it is found in the system. The result is that, usually, you can predict the name of a given physical interface.

This tutorial will show you how to change these device names to anything you would like. This tutorial is not meant to advocate against the new network naming. However, there are cases ranging from simple familiarity to application compatibility that might make you want to use different network device names. For those cases, this tutorial hopes to help you.

Assumptions

This tutorial makes a few assumption which may not apply to your system. Please adjust accordingly.

This tutorial assumes you have six network cards;

  • eth0
  • eth1
  • eth2
  • eth3
  • eth4
  • eth5

We will rename these to;

  • bcn_link1
  • bcn_link2
  • sn_link1
  • sn_link2
  • ifn_link1
  • ifn_link2

The reason for these names is that, in my case, I wish to give names to interfaces that reflect their purpose in my servers. Specifically, I need three bonded networks; BCN, SN and IFN in my clusters. You can just as easily use eth0 to eth5 or any other name that suits your purposes.

This tutorial also assumes that you have a minimal install and that the system is designed to be a server. As such, NetworkManager is removed and the network daemon will be used. No attempt was made to test if the method of renaming network devices shown here works with NetworkManager.

Overview

We will need to take the following steps;

  1. Identify the network device names assigned to each interface.
  2. Generate the /etc/udev/rules.d/70-persistent-net.rules file.
  3. Unplug and plug back in each network interface to create the map of current to desired names.
  4. Rename and edit the /etc/sysconfig/network-scripts/ifcfg-X files to reflect the desired names.
  5. Edit the /etc/udev/rules.d/70-persistent-net.rules file to create the MAC address to device name maps.
  6. Restart the machine to make the changes take effect.

Identify Interfaces

To start, you want to see what the names your system has already given to the network interfaces. This can be done with ip addr, which reports all network cards, regardless if they are started or not.

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:bd:13:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.254/24 brd 192.168.122.255 scope global dynamic eth0
       valid_lft 3445sec preferred_lft 3445sec
    inet6 fe80::5054:ff:febd:1307/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:39:18:09 brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:01:3f:c8 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:c9:83:aa brd ff:ff:ff:ff:ff:ff
6: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:b3:63:3e brd ff:ff:ff:ff:ff:ff
7: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:fe:eb:0f brd ff:ff:ff:ff:ff:ff

So I see the six network cards named ethX. You might have emX, pXpY or so on. The current names themselves doesn't matter too much, so long as you see all of your interfaces.

Mapping Current Names to Desired Names

So now that we know which cards you have, we need to sort out which name maps to which device. There are several ways you can do this;

  • Sorting out the Consistent Network Device Names to physical interfaces. I find this to be an imperfect art.
  • Use ethtool -p <dev> and watch to see which port's light blinks. This requires driver support and a clear view of the network cards.
  • Unplug a network cable and then run ip addr and see which interface changed from <BROADCAST,MULTICAST,UP,LOWER_UP> to <NO-CARRIER,BROADCAST,MULTICAST,UP> and note the device name.
  • Watch the system log and unplug each cable to see what device is reported as having disconnected. This is the method we'll use.

Before we proceed, let me give some background on what is happening behind the scenes.

In my case, I have three networks, each bonded across two interfaces, each link going to a separate switch (switch 1 and switch 2). I want to name these devices after their purpose;

Before I started, I figured out which physical port I wanted to use for a given network and link. You will obviously have your own plan and it will likely differ from the one shown here. This is no problem at all. So long as you know what name you want to give each physical interface, you are ready to go.

Initial List

Start by creating a list of interfaces names you want. Then, as you pull each cable, mark the current name of the connection that went down beside the desired name.

So here is the list of desired names that I start with;

Current Name Desired Name
bcn_link1
bcn_link2
sn_link1
sn_link2
ifn_link1
ifn_link2

Before we begin, we'll want to watch messages being printed to the system log. We can do this using journalctl's 'follow' switch, -f switch, like so;

journalctl -f -n 0
-- Logs begin at Wed 2015-07-22 02:03:47 GMT. --

The '-n 0' tells it to show no previous lines in the log.

Now, start by pulling the first cable and you should see a message indicating the current name of the interface having gone down. In my case, I am pulling the network cable which I want to be named bcn_link1 (the "back-channel network link to switch number 1).

Unplug the first cable and you should see log entries like this:

Jul 22 03:38:24 an-a03n01.alteeve.ca nm-dispatcher[14204]: Dispatching action 'dhcp4-change' for eth0
Jul 22 03:46:12 an-a03n01.alteeve.ca NetworkManager[816]: <info>  (eth4): link disconnected

Plug the cable back in and you should see an entry like this;

Jul 22 03:46:17 an-a03n01.alteeve.ca NetworkManager[816]: <info>  (eth4): link connected

So in my case, I see that the current name is eth4. So I will record this next to 'bcn_link1' in my table;

Current Name Desired Name
eth4 bcn_link1
bcn_link2
sn_link1
sn_link2
ifn_link1
ifn_link2

Now I unplug the device I want to be called bcn_link2;

Jul 22 04:00:53 an-a03n01.alteeve.ca NetworkManager[816]: <info>  (eth5): link disconnected

Plug it back in;

Jul 22 04:01:01 an-a03n01.alteeve.ca NetworkManager[816]: <info>  (eth5): link connected

So here I see that the interface currently named eth5 is the one I want to rename to 'bcn_link2', so I add it to the table.

Current Name Desired Name
eth4 bcn_link1
eth5 bcn_link2
sn_link1
sn_link2
ifn_link1
ifn_link2

Repeat this for the next four interfaces. Once done, you should have something like;


Current Name Desired Name
eth4 bcn_link1
eth5 bcn_link2
eth2 sn_link1
eth3 sn_link2
eth0 ifn_link1
eth1 ifn_link2

There, now I know the existing device name and the name I wish to give it.

Writing The udev Rules File

The way we're going to create the actual MAC address to device name mapping will be via udev rules. Specifically, we're going to edit or create the /etc/udev/rules.d/70-persistent-net.rules. As of Fedora 17, this file is no longer created, so you will need to create it yourself. I've written a little script to do this;

Note: If this script produces no output, run ip addr. If the output looks like: [eth0 Link encap:Ethernet HWaddr 00:1b:21:72:99:ab], then your system is using the old-style output and this script won't work.
#!/usr/bin/perl

use strict;
use warnings;
use IO::Handle;
my $conf = {};

# Read the 'ip addr'
my $device      = "";
my $mac_address = "";
my $file_handle = IO::Handle->new();
my $shell_call  = "ip addr";
open ($file_handle, "$shell_call 2>&1 |") or die "Failed to call: [$shell_call], error: $!\n";
while (<$file_handle>)
{
	chomp;
	my $line = $_;
	if ($line =~ /^\d+: (\S+):/)
	{
		$device = $1;
		next if $device eq "lo";
		$mac_address = "";
		next;
	}
	if ($line =~ /ether (.*?) /)
	{
		$mac_address = $1;
		next if not $device;
		$conf->{$device} = $mac_address;
	}
}

foreach my $device (sort {$a cmp $b} keys %{$conf})
{
	my $say_dev = lc($device);
	my $say_mac = lc($conf->{$device});
	print "\n# Added by 'generate-udev-net' for detected device '$device'.\n";
	print "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$say_mac\", NAME=\"$say_dev\"\n";
}

exit(0);

You can download and run this and it will print a skeleton udev file. If the output looks good, re-run it again and pipe the output to the 70-persistent-net.rules file.

Download it;

cd ~
wget -c https://alteeve.ca/files/generate-udev-net
--2015-07-22 04:18:01--  https://alteeve.ca/files/generate-udev-net
Resolving alteeve.ca (alteeve.ca)... 65.39.153.64
Connecting to alteeve.ca (alteeve.ca)|65.39.153.64|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 897 [text/plain]
Saving to: ‘generate-udev-net’

100%[====================================================================>] 897         --.-K/s   in 0s      

2015-07-22 04:18:02 (89.2 MB/s) - ‘generate-udev-net’ saved [897/897]

Then you can run it;

chmod 755 generate-udev-net
./generate-udev-net
# Added by 'generate-udev-net' for detected device 'eth0'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:bd:13:07", NAME="eth0"

# Added by 'generate-udev-net' for detected device 'eth1'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:39:18:09", NAME="eth1"

# Added by 'generate-udev-net' for detected device 'eth2'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:01:3f:c8", NAME="eth2"

# Added by 'generate-udev-net' for detected device 'eth3'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:c9:83:aa", NAME="eth3"

# Added by 'generate-udev-net' for detected device 'eth4'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:b3:63:3e", NAME="eth4"

# Added by 'generate-udev-net' for detected device 'eth5'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:fe:eb:0f", NAME="eth5"

What we see above are the rules telling the system that the network device with the specified MAC address is to be given the given name. We are not changing the names yet. That will come in the next step.

If the output looks good to you, re-run the program, but this time we will redirect the output to the 70-persistent-net.rules file.

./generate-udev-net > /etc/udev/rules.d/70-persistent-net.rules
cat /etc/udev/rules.d/70-persistent-net.rules
# Added by 'generate-udev-net' for detected device 'eth0'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:bd:13:07", NAME="eth0"

# Added by 'generate-udev-net' for detected device 'eth1'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:39:18:09", NAME="eth1"

# Added by 'generate-udev-net' for detected device 'eth2'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:01:3f:c8", NAME="eth2"

# Added by 'generate-udev-net' for detected device 'eth3'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:c9:83:aa", NAME="eth3"

# Added by 'generate-udev-net' for detected device 'eth4'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:b3:63:3e", NAME="eth4"

# Added by 'generate-udev-net' for detected device 'eth5'.
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:fe:eb:0f", NAME="eth5"

If you are more comfortable writing this file yourself, instead of downloading some stranger's program, you can copy the output above, replace the ATTR{address}=="xx:xx:xx:xx:xx:xx" value with the MAC addresses shown in the output from the earlier ip addr and setting the device names you wish.

Note: The MAC address values must be in lower-case.

Changing The Device Names

Now that we have our skeleton /etc/udev/rules.d/70-persistent-net.rules and our rename table, we can change the device names.

I like to always make a backup of any file before I edit it;

cp /etc/udev/rules.d/70-persistent-net.rules ~/

Now we will edit our newly created /etc/udev/rules.d/70-persistent-net.rules file and change the NAME="..." values according to our table. In my case, the new file will look like this;

vim /etc/udev/rules.d/70-persistent-net.rules

The changes are easier to see in a diff;

diff -u /etc/udev/rules.d/70-persistent-net.rules /root/70-persistent-net.rules
--- /root/70-persistent-net.rules	2015-07-22 04:22:14.355000000 +0000
+++ /etc/udev/rules.d/70-persistent-net.rules	2015-07-22 04:22:44.472000000 +0000
@@ -1,18 +1,18 @@
 
 # Added by 'generate-udev-net' for detected device 'eth0'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:bd:13:07", NAME="eth0"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:bd:13:07", NAME="ifn_link1"
 
 # Added by 'generate-udev-net' for detected device 'eth1'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:39:18:09", NAME="eth1"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:39:18:09", NAME="ifn_link2"
 
 # Added by 'generate-udev-net' for detected device 'eth2'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:01:3f:c8", NAME="eth2"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:01:3f:c8", NAME="sn_link1"
 
 # Added by 'generate-udev-net' for detected device 'eth3'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:c9:83:aa", NAME="eth3"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:c9:83:aa", NAME="sn_link2"
 
 # Added by 'generate-udev-net' for detected device 'eth4'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:b3:63:3e", NAME="eth4"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:b3:63:3e", NAME="bcn_link1"
 
 # Added by 'generate-udev-net' for detected device 'eth5'.
-SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:fe:eb:0f", NAME="eth5"
+SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:fe:eb:0f", NAME="bcn_link2"

Now, next time we reboot, the system will use the given names for the specified MAC addresses!

Reboot and Test

Note: In the output below, you will see several additional interfaces, three bonds and a bridge, which were created as part of the Anvil! Tutorial 3 network setup. You can ignore those for now.

So now, reboot and we'll verify it is remapped.

First, call ip add and make sure the 'eth[05]' interfaces are gone and the new '{bcn,sn,ifn}_link{1,2}' interfaces are show.

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ifn_link1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master ifn_bond1 state UP qlen 1000
    link/ether 52:54:00:bd:13:07 brd ff:ff:ff:ff:ff:ff
3: ifn_link2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master ifn_bond1 state UP qlen 1000
    link/ether 52:54:00:bd:13:07 brd ff:ff:ff:ff:ff:ff
4: sn_link1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master sn_bond1 state UP qlen 1000
    link/ether 52:54:00:01:3f:c8 brd ff:ff:ff:ff:ff:ff
5: sn_link2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master sn_bond1 state UP qlen 1000
    link/ether 52:54:00:01:3f:c8 brd ff:ff:ff:ff:ff:ff
6: bcn_link1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bcn_bond1 state UP qlen 1000
    link/ether 52:54:00:b3:63:3e brd ff:ff:ff:ff:ff:ff
7: bcn_link2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bcn_bond1 state UP qlen 1000
    link/ether 52:54:00:b3:63:3e brd ff:ff:ff:ff:ff:ff
8: ifn_bridge1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 52:54:00:bd:13:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.31/24 brd 192.168.122.255 scope global ifn_bridge1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:febd:1307/64 scope link 
       valid_lft forever preferred_lft forever
9: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN 
    link/ether fa:77:7c:ce:b9:f0 brd ff:ff:ff:ff:ff:ff
10: bcn_bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 52:54:00:b3:63:3e brd ff:ff:ff:ff:ff:ff
    inet 10.20.30.1/16 brd 10.20.255.255 scope global bcn_bond1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:feb3:633e/64 scope link 
       valid_lft forever preferred_lft forever
11: ifn_bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue master ifn_bridge1 state UP 
    link/ether 52:54:00:bd:13:07 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::5054:ff:febd:1307/64 scope link 
       valid_lft forever preferred_lft forever
12: sn_bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 52:54:00:01:3f:c8 brd ff:ff:ff:ff:ff:ff
    inet 10.10.30.2/16 brd 10.10.255.255 scope global sn_bond1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe01:3fc8/64 scope link 
       valid_lft forever preferred_lft forever

Interfaces 2 through 7 are the newly named interfaces. So now lets watch the system log again while we pull our cables.

journalctl -f -n 0
-- Logs begin at Wed 2015-07-22 04:34:09 GMT. --

Pull the cable marked as 'bcn_link1';

Jul 22 04:46:00 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (bcn_link1): link disconnected

Plug it back in;

Jul 22 04:46:08 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (bcn_link1): link connected

Repeat for 'bcn_link2';

Jul 22 04:47:24 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (bcn_link2): link disconnected
Jul 22 04:47:27 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (bcn_link2): link connected

Repeat for the other four interfaces;

Jul 22 04:48:12 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (sn_link1): link disconnected
Jul 22 04:48:16 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (sn_link1): link connected
Jul 22 04:48:33 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (sn_link2): link disconnected
Jul 22 04:48:36 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (sn_link2): link connected
Jul 22 04:48:56 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (ifn_link1): link disconnected
Jul 22 04:49:00 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (ifn_link1): link connected
Jul 22 04:49:25 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (ifn_link2): link disconnected
Jul 22 04:49:29 an-a03n01.alteeve.ca NetworkManager[703]: <info>  (ifn_link2): link connected

That matches the order that we pulled the cables, so the remap worked perfectly.

We're done!

 

Any questions, feedback, advice, complaints or meanderings are welcome.
Alteeve's Niche! Enterprise Support:
Alteeve Support
Community Support
© Alteeve's Niche! Inc. 1997-2024   Anvil! "Intelligent Availability®" Platform
legal stuff: All info is provided "As-Is". Do not use anything here unless you are willing and able to take responsibility for your own actions.