linux manual automatic change of network card MAC address method

  • 2020-11-26 19:03:47
  • OfStack

preface

The mac address of 1 network card has been written in the factory, and sometimes in rom which can be erased, but it needs to have the corresponding professional technology or professional equipment.

For those who are bound to mac address but do not want to change mac after changing the device, the mac address needs to be modified.

View the NETWORK card MAC address

Use the ip command to see the MAC address:


ip link show ${interface}

2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> 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

Including 1 link/ether behind the string with: segmentation of 6 bytes hexadecimal number 106 is the nic MAC address, namely f0: de: f1: ad: 1 d: f0

Manually modify the MAC address

You can also modify the MAC address using the ip command, but you need the root permission:

1. Disable network card


sudo ip link set dev ${interface} down

2. Modify the MAC address

Some network operators may refuse to assign an IP address to an incorrect MAC, so it is recommended that the first three bytes be prefixed with the real MAC address, while the last three bytes can be changed at random.


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

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


ip link show ${interface}

2: enp0s25: <BROADCAST,MULTICAST> 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 notice 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

Automatically change

The ES68en-ES69en service can set the MAC address via the link file (the underlying physical network device configuration)

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

Match section

If each item in the "[Match]" section matches a network card, the Link file is considered to match the 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 network card (MAC address)

OriginalName

Matches the value of the "INTERFACE" attribute of the network card (the kernel name of the network card). Receives a list of matching patterns separated by a space (using an ES100en-style wildcard). Cannot be used to match a network card whose name has been changed by userspace. You should use this option with care because the kernel name of the network card can be unstable.

Path

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

Driver

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

Type

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

Host

Match the host's hostname or "machine ID", see the "ConditionHost=" option in ES138en.unit (5).

Virtualization

To check if you are running in a specific virtual environment, see the "ConditionVirtualization=" option in ES147en.unit (5).

KernelCommandLine

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

Architecture

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

Link section

The Link section allows for a variety of configurations for network CARDS, 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 MAC address inherent in the network card hardware (which most network CARDS do), then do nothing but use the kernel's MAC address. Otherwise, an MAC address (for a given motherboard and network card) will be randomly generated to ensure that it remains fixed between launches. The MAC address generation feature requires the network card to have the ID_NET_NAME_* attribute, otherwise the MAC address cannot be generated automatically.

"random"

If the kernel USES the randomly generated MAC address (rather than the MAC address inherent in the network card hardware), then do nothing and use the kernel's MAC address. Otherwise, one MAC address will be randomly generated each time the network card appears (usually during boot). The "unicast" and "locally administered" bits are set regardless of the MAC address generated using either of the above methods.

"none"

Use the kernel's MAC address directly and unconditionally.

MACAddress

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

Also, in the Link section, we have to name a network card, so there must be NamePolicy or Name options

NamePolicy

How you should set the name of the network card only makes sense if you do not use the "net.ifnames =0" kernel boot option. Accept a list of policies separated by a space, and try each policy sequentially, depending on the first successful policy. The resulting name will be used to set the "ID_NET_NAME" attribute of the network card. Note that the default udev rule sets the "NAME" attribute (that is, the name of the network card) with the value "ID_NET_NAME". If the network card is already named by an empty household space, no renaming will take place. The available policies are as follows:

"kernel"

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

"database"

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

"onboard"

The "ID_NET_NAME_ONBOARD" attribute value based on the network card (from the onboard network card firmware) sets the name of the network card.

"slot"

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

"path"

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

"mac"

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

Name

The name of the network card that should be used when NamePolicy= is invalid. Invalid cases include: (1) NamePolicy= is not set; (2) All the strategies in NamePolicy= failed; (3) "ES314en. ifnames=0" kernel boot option is used

Be careful not to set a name that could be used by the kernel for other network ports (such as "eth0"), which could cause udev to compete with the kernel for a name assignment, with unintended consequences. The best practice is to use a name or prefix that will never cause a conflict, such as "internal0" "external0" or "lan0" "lan1"/" lan3"

conclusion


Related articles: