Rename network interface linux

Linux Rename Eth0 Network Interface Card Name [ Udev ]

A wireless NIC is showing as wlan0 but I need to be appear as eth1. How can I rename wlan0 devices through udev as eth1? How do I change or rename eth0 as wan0 under Linux operating systems?

The best way to rename Ethernet devices is through udev. It is the device manager for the Linux kernel. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles /dev directory and all user space actions when adding/removing devices, including firmware load.

Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements Linux + udev
Reboot required
Est. reading time N/A

The order of the network interfaces may be unpredictable under certain configurations. Between reboots it usually stays the same, but often after an upgrade to a new kernel or the addition or replacement of a network card (NIC) the order of all network interfaces changes. For example, what used to be rl0 now becomes wlan0 or what used to be eth0 now becoems eth2 or visa versa.

Step #1: Find out the MAC address of the Ethernet device

Type the following command:
# ifconfig -a | grep -i —color hwaddr
Sample outputs:

Note down the MAC address.

Step #2: Rename eth0 as wan0

To rename eth0 as wan0, edit a file called 70-persistent-net.rules in /etc/udev/rules.d/ directory, enter:
# vi /etc/udev/rules.d/70-persistent-net.rules
The names of the Ethernet devices are listed in this file as follows:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Locate and identify the line with the NIC from step 1 (look for the MAC address). It may look like above. In this example, the interface eth0 will be renamed to wan0 (change NAME=» eth0 » to NAME=» wan0 » ):

Save and close the file. Reboot the system to test changes:
# reboot
Verify new settings:
# ifconfig -a
# ifconfig wan0
# ifconfig -a | less
# ip addr show

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Can i change the MAC address of a using udev rule. Actually i am getting random MAC for a Ethernet interface on my device. So i can’t rename that interface as in this rule MAC address is comparing.

Try using “macchanger”

Hi Rahul,
Probably you are looking for this. On my ubuntu (10.04-LTS) system. Its like.

root@ubuntu:/etc/udev/rules.d#cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x10ec:0x8139 (8139cp)
SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR

==”64:63:62:61:10:02″, ATTR==”0x0″, ATTR==”1″, KERNEL==”eth*”, NAME=”eth0″

Change here what you want.

Thanks for your reply. 🙂
In my case ATTR

==”xx:xx:xx:xx:xx:xx″ is random, it changed on every reboot, so i can’t compare this.
I need to make it static first, is there any way to do this?

looks like you’re using some virtualization software, In that case, its the Virtualization software whether its Xen, KVM, Hyper-V, VMWare or any other, has the option to put the static MAC address. You’ll have to look out for the options..

Thanks Balvinder,
I am working on a embedded self designed card and i have only one option to put the static MAC by using kernel. But i don’t want to change the kernel again. So looking for udev alternate, if it is possible. Like i can change the device name, can i change the MAC as well?

Add this entry into your rc.local file

ifconfig eth0 down hw ether 00:C1:26:10:30:C5 up

Источник

rigacci.org

User Tools

Site Tools

Indice

Eventi

Energia

Rigacci.Org usa energia elettrica da fonti rinnovabili, grazie al gruppo di acquisto Merci Dolci.

Software libero!

Table of Contents

Renaming network interfaces

With Debian 10 Buster

According some sources, there is at least two methods to rename a network interface e.g. from enx00e04c885302 to something more human like lan0 or the plain old eth0.

The simplest method should be to add a line into /etc/network/interfaces (or to create a new file /etc/network/interfaces.d/local) with the following:

Another methos should be to create a file /etc/systemd/network/10-persistent-net.link with something like

Both seems to work, unfortunately on my XFCE environment, the NetworkManager stepped into the play, reverting back the name:

To keep it simple, I choosed the kernel override option in /etc/default/grub:

Followed by update-grub and reboot.

If you want to manage the interface using NetworkManager from the user session, be sure that the interface is not referred by /etc/network/interfaces or any snippet file into /etc/network/interfaces.d/ . If the interface is configured by /etc/network/interfaces , NetworkManger will ignore it, labelling as unmanaged.

How it worked in the old days of Debian 4 Etch

In a modern Linux system, network interface drivers are compiled as modules. Generally there is a subsytem that provides loading kernel modules automatically. Debian GNU/Linux Etch uses udev for that.

Interface names (eth0, eth1, …) are assigned as they are discovered and kernel modules are loaded, unfortunately the order of discovering can sligtly vary across reboots. So you can find that after a reboot your system has swapped eth0 and eth1, with all the nasty consequences you can imagine.

I suggest two ways to assign interface names in a consisten way.

In a Debian Etch box, check the /etc/udev/rules.d/z25_persistent-net.rules . This file is updated at bootstrap when new network interfaces are found. It contains a persistent mapping of MAC addresses to interface names.

In general, add a custom configuration file /etc/udev/rules.d/010_netinterfaces.rules with lines like this:

In the above example we used the SYSFS

as a match for the rule, use this command to print all sysfs properties of the specified device that can be used in udev rules to match the specified device:

ifrename

You configure this userspace program writing the configuration file /etc/iftab :

The good thing of this program is that you can select intefaces by the name of the kernel module, by MAC address, by bus position and other methods. But beware that the standard Debian startup of ifrename runs the program once, when all the interfaces are already discovered and named. In this case it is not possible to swap two interface names, because when you try to rename the first interface, an error occurr: Error: cannot change name of eth1 to eth0: File exists .

Debian 8 Jessie

The udev cache file is named now /etc/udev/rules.d/70-persistent-net.rules , sometimes only one Ethernet card is automatically added to this file, some other times the cache file is not created at all.

To force the creation of that file just execute the command:

Debian 9 predictable network interface names

Esempio: enp3s0f1

Per disabilitare questa funzione, chiamata Predictable Network Interface Names è possibile aggiungere l’opzione kernel net.ifnames=0 ad esempio in /etc/default/grub , opzione GRUB_CMDLINE_LINUX.

Se è installato la helper utility di udev chiamata biosdevname, è opportuno passare al kernel anche il parametro biosdevname=0.

Источник

Change Network Interface Name: eth0,eth1,eth2+

The best way to rename a network interface is through udev .

Edit the file /etc/udev/rules.d/70-persistent-net.rules to change the interface name of a network device.

The names of the network devices are listed in this file as follows:

Rename network interface from eth0 to wan0

To rename interface eth0 to wan0 , edit /etc/udev/rules.d/70-persistent-net.rules file and change NAME=»eth0″ to NAME=»wan0″ .

For Centos/RHEL etc.

Rename the network interface configuration file:

Edit the network interface configuration file and replace all occurrences of the old name eth0 with the new one wan0 :

For Ubuntu etc.

Edit the /etc/network/interfaces file and replace all occurrences of the old name eth0 with the new one wan0 :

Test changes

Reboot the system to test changes:

Verify new settings:

Rename network interface from eth1 back to eth0

Q: Why does my network interface name change?
A: The interface name of a network device increases if the MAC address of a network card changes.

Edit the file /etc/udev/rules.d/70-persistent-net.rules .

Copy the new MAC address from eth1 to the line of your eth0 rule.

Delete the rule for eth1 . Save and close the file.

For Centos/RHEL etc.

Check the network interface configuration located under:

Don’t forget to replace the old MAC address with the new one.

For Ubuntu etc.

Make sure /etc/network/interfaces file has correct configuration:

Источник

Rename Network Interface using Udev in Linux

udev is a daemon which dynamically creates and removes device nodes from /dev/, handles hotplug events and loads drivers at boot time. It replaces the hotplug package and requires a kernel not older than 2.6.12.udev – /dev/ and hotplug management daemon

Install udev in Debian

#apt-get install udev

After installing this you need to reboot your machine.

Upgrade from devfs to udev on Debian Sarge

Some of you used devfs under Woody, or under their previous Sarge install. When going kernel 2.6, it may be useful to revert or upgrade to udev instead. The problem is that their nomenclatura is different, so your system may be rendered unbootable. Let’s see what to care about to switch from devfs to udev.

First, the bootloader will no longer need the devfs=mount parameter on the kernel command-line. So, wheter you use GRUB or LILO, remove or comment the reference to devfs=mount.

Second, check in your /etc/fstab that you have nothing in devfs-nomenclatura, like /dev/ide/host0/bus0/target0/lun0/part3, but everything in the standard nomenclatura like /dev/hda3.

Third, install the udev package, it will probably bring some dependencies too. That’s ok.

Last, check the links in /etc/udev/rules.d/. There are probably two links to the upper directory: compat-full.rules and devfs.rules. Remove them and create a new link to udev.rules and to cd-aliases.rules like this

# cd /etc/udev/rules.d/
# rm compat-full.rules devfs.rules
# ln -s ../udev.rules .
# ln -s ../cd-aliases.rules .

Now, reboot, and if you want, remove package devfsd.

Note: It may also be necessary to reconfigure some special packages which store device information in their configuration file, like XFree86.

If you want rename a network interface’s device name when using udev. This can be practical to do since the automatically assigned device name may actually change around a bit due to random variation in the initialisation process of them.

For instance, eth0 can on one bootup refer to network interface A and eth1 to network interface B, but on another bootup can interface A be assigned eth1 and interface B be assigned eth0, which can cause problems if only one of them has a network cable attached and is configured to obtain an address over DHCP — the interface without the cable attached will then attempt to do that instead!

udev Overview

Unlike the traditional Linux system, where the device nodes in the /dev directory have been a static set of files, udev dynamically provides only the nodes for the devices actually present on a system. Although devfs provided a similar functionality, advocates for udev cited a number of reasons for preferring its implementation over devfs.

udev supports persistent device naming, which does not depend on, for example, the order in which the devices are plugged into the system. The default udev setup provides persistent names for storage devices. Any hard disk is recognized by its unique filesystem id, the name of the disk and the physical location on the hardware it is connected to.

udev executes entirely in user space, as opposed to devfs’ kernel space. One consequence is that udev moved the naming policy out of the kernel and can run arbitrary programs to compose a name for the device from the device’s properties, before the node is created.

udev operation

Udev is a generic kernel device manager. It runs as a daemon on a Linux system and listens to events the kernel sends out if a new device is initialized or a device is removed from the system. The system provides a set of rules that match against exported values of the event and properties of the discovered device. A matching rule will possibly name and create a device node and run configured programs to set-up and configure the device. Udev rules can match on properties like the kernel subsystem, the kernel device name, the physical location of the device, or properties like the device’s serial number. Rules can also request information from external programs to name a device or specify a custom name that will always be the same, regardless of the order devices are discovered by the system.

Obtaining the MAC addresses from your Debian machine

If you want to obtain MAC address of any linux machine you need to run the following command

This will give you all the existing network interfaces in your machine

# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:AB:CD:12:34:56
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:3
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:180 (180.0 b)
Interrupt:185 Base address:0xc000

In the above example HWaddr is your MAC address 00:AB:CD:12:34:56

Using udev

Basic Rules

udev provides several different match keys which can be used to write rules which match devices very precisely. Some of the most common keys are introduced below, others will be introduced later in this document. For a complete list, see the udev man page.

KERNEL – match against the kernel name for the device

SUBSYSTEM – match against the subsystem of the device

DRIVER – match against the driver name for the device

After you have used a series of match keys to precisely match a device, udev gives you fine control over what happens next, through a range of assignment keys. For a complete list of possible assignment keys, see the udev man page. The most basic assignment keys are introduced below. Others will be introduced later in this document.

NAME – the name that shall be used for the device node

SYMLINK – a list of symbolic links which act as alternative names for the device node

Create a new file in the udev rules directory, e.g. /etc/udev/rules.d/010_netinterfaces.rules

In it specify the renaming in the following way for each interface on its own line

KERNEL=”oldnameprefix*”, SYSFS

==”MACaddress”, NAME=”newname”

where the oldnameprefix is typically eth. Note that in the MAC address, the hexadecimal digits should be in lowercase, otherwise udev fails to match them properly with the network interface.

You have quite a bit of freedom in choosing the new name, We recommend to keep it short and without any spaces or weird characters though. You can e.g. specify a fixed eth0, eth1, eth2 for specific MAC addresses, or you can name them after their use, or anything really. Remember that some applications that poke on a low level may dislike them not being called in the normal fashion of eth0, eth1..etc

Examples using udev

Example: Three network interfaces being present on a computer, setting a fixed eth0, eth1 and eth2 as their names.

KERNEL==”eth*”, SYSFS

==”00:12:34:fe:dc:ba”, NAME=”eth0″
KERNEL==”eth*”, SYSFS
==”00:56:78:98:76:54″, NAME=”eth1″
KERNEL==”eth*”, SYSFS
==”00:90:ab:32:10:fe”, NAME=”eth2″Example: Three network interfaces (one Intel, one NVIDIA, and one 3Com) being present on a computer, naming them after the manufacturer of the interfaces.

KERNEL==”eth*”, SYSFS

==”00:12:34:fe:dc:ba”, NAME=”eth-intel”
KERNEL==”eth*”, SYSFS
==”00:56:78:98:76:54″, NAME=”eth-nv”
KERNEL==”eth*”, SYSFS
==”00:90:ab:32:10:fe”, NAME=”eth-3com”

Updating network configuration

If you named the interfaces in a different fashion as they were named before, the network configuration needs to be updated for the new interface device names to be used.

Edit the /etc/network/interfaces file, and change all instances of the old names to the new names.

E.g. if you previously used eth0 and have renamed it newname, you’d replace all instances of eth0 in that file with newname

But if you just put a fixed eth0, eth1, … as their names, you just need to make sure the one you want to have as the primary network interface is set to the one you want in the file.

Example

Having renamed the existing eth0, eth1, and eth2 to eth-intel, eth-nv and eth-3com, choosing to use the eth-intel one as the primary interface

The /etc/network/interfaces file before changes

# The primary network interface
auto eth0
iface eth0 inet dhcp

# Currently unused network interfaces
iface eth1 inet dhcp
iface eth2 inet dhcpThe file after changes:

# The primary network interface
auto eth-intel
iface eth-intel inet dhcp

# Currently unused network interfaces
iface eth-nv inet dhcp
iface eth-3com inet dhcp

Now you need to Reboot the computer and verify that the new network interface names are in use with e.g. ifconfig

Where newname is the new interface name you specified. Repeat procedure for each one you renamed.

Rename CD/DVD drives

If you want to change the names of a DVD reader (hdf), and a DVD rewriter (hdg) to view like /dev/dvd for convenience.

As we know the KERNEL names for these devices, rule writing is simple. Here are some examples for my system:

BUS==”ide”, KERNEL==”hdf”, SYMLINK+=”dvd”, GROUP=”cdrom”
BUS==”ide”, KERNEL==”hdg”, SYMLINK+=”dvdrw”, GROUP=”cdrom”

Rename Partitions USB Hard Disk

If you have a 200GB USB hard disk, you might want to partition it, in which case we can take advantage of udev’s string substitutions:

BUS==”usb”, KERNEL==”sd*”, SYSFS==”USB 2.0 Storage Device”, NAME=”%k”, SYMLINK+=”usbhd%n”This rule creates symlinks such as:

/dev/usbhd – The fdiskable node
/dev/usbhd1 – The first partition (mountable)
/dev/usbhd2 – The second partition (mountable)

Источник

Читайте также:  Как удалить копию старого windows
Оцените статью