Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the MAC address of the network card in linux

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to modify the MAC address of the network card in linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Check the MAC address of the network card

Use the ip command to view the MAC address:

Ip link show ${interface} 2: enp0s25: mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000 link/ether f0:de:f1:ad:1d:f0 brd ff:ff:ff:ff:ff:ff

The string after link/ether uses: the split 6-byte hexadecimal number is the MAC address of the network card, that is, f0:de:f1:ad:1d:f0.

Manually modify the MAC address

The MAC address can also be modified using the ip command, but root permission is required:

1. Disable the network card

Sudo ip link set dev ${interface} down

2. Modify the MAC address

Some network operators may refuse to assign IP addresses to incorrect MAC, so it is recommended that the first three bytes be prefixed with real MAC addresses, and the last three bytes can be changed randomly.

Sudo ip link set dev ${interface} address ${new_mac}

Let's check the MAC address of the network card again.

Ip link show ${interface} 2: enp0s25: mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000 link/ether f0:de:f1:ff:ff:ff brd ff:ff:ff:ff:ff:ff

You will find that the network card has been changed to the new MAC address.

3. Finally restart the network card

Sudo ip link set dev ${interface} up

Automatic change

The systemd-networkd service can set the MAC address through the link file (underlying physical network device configuration)

We match a network card through the [Match] section, and then configure the network card through the [Link] section.

Match section

If each entry in the "[Match]" section matches a network card, the Link file is considered to match that network card. If the "[Match]" section is empty, it matches any network card.

All the options available for matching are as follows:

MACAddress

Match the physical address of the Nic (MAC address)

OriginalName

Match the "INTERFACE" attribute value of the network card (the kernel name of the network card). Receives a space-separated list of matching patterns (using shell-style wildcards). Cannot be used to match a network card that has been renamed by user space. You should use this option carefully because the kernel name of the network card may be unstable.

Path

Match the "ID_PATH" attribute value of the network card (the bus path of the network card). Receives a space-separated list of matching patterns (using shell-style wildcards).

Driver

Match the "DRIVER" attribute value of the network card (the driver name of the network card). Receives a space-separated list of matching patterns (using shell-style wildcards). Note that if the "DRIVER" attribute of the network card does not exist, the driver name output in the "ethtool-I" command will be used.

Type

Match the "DEVTYPE" attribute value of the network card (the device type of the network card). Receives a space-separated list of matching patterns (using shell-style wildcards).

Host

Match the hostname or "machine ID" of the host, see the "ConditionHost=" option in systemd.unit (5).

Virtualization

To check whether it is running in a specific virtual environment, see the "ConditionVirtualization=" option in systemd.unit (5).

KernelCommandLine

Check if it is set (or with "!" The beginning indicates that specific kernel boot options are not set, see "ConditionKernelCommandLine=" option in systemd.unit (5).

Architecture

To check whether it is running on a specific hardware platform, see the "ConditionArchitecture=" option in systemd.unit (5).

Link section

You can configure the Nic in the Link section, among which the options related to modifying MAC are:

MACAddressPolicy

How to set the MAC address of the network card:

"persistent"

If the kernel uses the inherent MAC address of the Nic hardware (most NICs have it), do nothing but use the kernel's MAC address. Otherwise, a new MAC address (for a given motherboard and network card) will be randomly generated to ensure that it remains fixed between multiple boots. The feature of automatically generating MAC addresses requires that the network card must have an ID_NET_NAME_* attribute, otherwise the MAC address cannot be generated automatically.

"random"

If the kernel uses a randomly generated MAC address (instead of the inherent MAC address of the Nic hardware), do nothing but use the kernel's MAC address. Otherwise, a new MAC address will be randomly generated each time the network card appears (usually during startup). Regardless of the MAC address generated using either of the above methods, the "unicast" and "locally administered" bits are set.

"none"

Unconditionally and directly use the kernel's MAC address.

MACAddress

The MAC address used when "MACAddressPolicy=" is not set.

In addition, in the Link section, we must name a network card, so there must be NamePolicy or Name options

NamePolicy

How to set the name of the network card makes sense only if the "net.ifnames=0" kernel boot option is not used. Accept a space-separated list of policies, try each policy sequentially, and the first successful policy shall prevail. The resulting name will be used to set the "ID_NET_NAME" property of the network card. Note that the default udev rule sets the "NAME" property (that is, the name of the network card) with the value of "ID_NET_NAME". If the network card has been named by the empty user space, then no renaming operation will take place. The available policies are as follows:

"kernel"

If the kernel has set a fixed predictable name for this network card, no renaming is performed.

"database"

Sets the name of the network card based on the "ID_NET_NAME_FROM_DATABASE" attribute value of the network card (from the udev hardware database).

"onboard"

Sets the name of the network card based on the "ID_NET_NAME_ONBOARD" attribute value of the network card (from the on-board network card firmware).

"slot"

Sets the name of the network card based on the "ID_NET_NAME_SLOT" attribute value of the network card (from the pluggable network card firmware).

"path"

Sets the name of the network card based on the "ID_NET_NAME_PATH" attribute value of the network card (from the bus location of the network card).

"mac"

Sets the name of the network card based on the "ID_NET_NAME_MAC" attribute value of the network card (from the fixed MAC address of the network card).

Name

The name of the network card that should be used if the NamePolicy= is invalid. Invalid situations include: (1) NamePolicy= is not set; (2) all policies in NamePolicy= fail; (3) "net.ifnames=0" kernel boot option is used

Note that do not set names that may be used by the kernel for other network ports (such as "eth0"), which may cause udev to compete with the kernel in allocating names, resulting in unintended consequences. It is best to use names or prefixes that will never cause conflicts, such as "internal0", "external0" or "lan0", "lan1" / "lan3".

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, do you know how to change the MAC address of the network card in linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report