基于:http : //kernelpanik.net/rename-a-linux-network-interface-without-udev/
我们可以使用以下方法轻松更改接口的名称:
ifconfig peth0 down
ip link set peth0 name eth0
ifconfig eth0 up
Run Code Online (Sandbox Code Playgroud)
我们如何使用上述方式在启动时做到这一点?
在 /etc/network/interfaces 或任何其他文件中?
多亏了 netplan(ubuntu 18.04 中的默认设置),这现在特别容易。您可以根据 macaddress 或驱动程序设置接口名称:
编辑/etc/ netplan / 中现有的 .yaml配置文件或创建一个新配置文件:
sudo nano /etc/netplan/config.yaml
Run Code Online (Sandbox Code Playgroud)
下面是一个 MAC 地址匹配的例子。名称使用“set-name”设置并与接口的 MAC 地址匹配:
network:
ethernets:
wan:
match:
macaddress: 00:ab:cd:ef:12:34
addresses:
- 10.5.1.2/16
dhcp4: true
optional: true
set-name: wan0
lan:
match:
macaddress: 00:ab:cd:ef:12:45
addresses:
- 10.6.1.1/16
optional: true
set-name: eth0
version: 2
Run Code Online (Sandbox Code Playgroud)
保存 .yaml 文件并应用以下配置:
sudo netplan apply
Run Code Online (Sandbox Code Playgroud)
可能需要重新启动才能应用名称更改。
知道该ip
命令很好,但是有一些持久的方法可以使用现有脚本进行配置,是的,udev。
您可以做的一件事是将特定 MAC 地址的 NIC 映射到名称。将类似的内容附加到 /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="eth0"
Run Code Online (Sandbox Code Playgroud)
从 2021 年开始,我们将不再使用 udev 规则(尽管它们仍然可以正常工作),而是转向在 systemd 中进行,以将所有网络配置保留在一个地方。
要设置持久网络设备,请/etc/systemd/network/10-eth0.link
使用以下内容创建。文件名必须以数字开头并以数字结尾.link
,但其余的并不重要,由您决定。对于要重命名的每个网络接口,您都需要一个文件。
[Match]
# This is the MAC address of the card to change. You can also
# match against other properties like PCI/USB slot.
MACAddress=00:11:22:33:44:55
[Link]
# This is the name to assign. It must not conflict with any existing
# device, so be careful if you use a name like eth0 which can fail
# unexpectedly if you plug in another device that ends up on eth0 first.
Name=lan0
# You can also change the MAC address at the same time if you like.
# Make sure you follow the MAC address numbering rules or you can run
# into problems, e.g. if you assign a broadcast MAC address by mistake.
MACAddress=02:00:00:00:00:10
Run Code Online (Sandbox Code Playgroud)
在撰写本文时,.link
一旦系统启动并运行(即使重新启动systemd-networkd
),systemd 就不会重新应用文件,因此您需要重新启动才能确认它可以成功应用更改。