centos8修改网卡名

修改网卡名为eth开头

NetworkName=`ls /etc/sysconfig/network-scripts/ifcfg-* | xargs -n1 | awk -F- '{print $NF}' | head -1`
# 检查网卡名: 如果不是eth0,修改为eth0
if [ "$NetworkName" = "eth0" ]; then
  echo "NetworkName:$NetworkName"
  exit 0
else
  sed -i 's/\(GRUB_CMDLINE_LINUX=".*\)"/\1 biosdevname=0 net.ifnames=0"/' /etc/default/grub
  grub2-mkconfig -o /etc/grub2.cfg
  sed -i 's/\(^NAME=\).*/\1eth0/' /etc/sysconfig/network-scripts/ifcfg-$NetworkName
  sed -i 's/\(^DEVICE=\).*/\1eth0/' /etc/sysconfig/network-scripts/ifcfg-$NetworkName
  mv /etc/sysconfig/network-scripts/ifcfg-$NetworkName /etc/sysconfig/network-scripts/ifcfg-eth0
  nmcli c reload
  reboot
fi

不行再使用udev修改网卡名为任意名称

cat<<EOF >> /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:ff:be:c2",NAME="eth0"
EOF && reboot

udev的作用是:

man udev
receives device uevents directly from the kernel whenever a device is added or removed from the system, or it changes its state.
当有设备从系统插入或者拔出,或者改变了状态的时候,内核会直接收到设备uevents。

udev rules文件存在位置:

system rules directory: /usr/lib/udev/rules.d
volatile runtime directory: /run/udev/rules.d
local administration directory: /etc/udev/rules.d

udev读取文件规则:

读取这些目录下以.rules为后缀的文件。
文件先后顺序和其所在目录无关,和文件的名称顺序有关。
若不同目录存在相同的文件,则根据目录名称来排优先级顺序。/etc>/run>/usr

udev进行网卡重命名

https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/sec-understanding_the_device_renaming_procedure?tdsourcetag=s_pctim_aiomsg

优先查询的是/usr/lib/udev/rules.d/60-net.rules配置文件。如果发现包含 HWADDR 条目的 ifcfg 文件与某个接口的 MAC 地址匹配,它会将该接口重命名为 ifcfg 文件中由 DEVICE 指令给出的名称。

合理修改centos7网卡名称流程

  1. 首先修改当前网卡名称
/sbin/ip link set eth1 down
/sbin/ip link set eth1 name eth123
/sbin/ip link set eth123 up
  1. 然后在/usr/lib/udev/rules.d/60-net.rules中加入配置策略
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:0c:29:ff:be:c2", NAME="eth123"
  1. 最后修改ifcfg-xxx里面的NAMEDEVICE字段值为eth123
  2. 重启网卡即可。

refer:

https://www.freedesktop.org/software/systemd/man/udev.html#
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/sec-understanding_the_device_renaming_procedure?tdsourcetag=s_pctim_aiomsg
https://unix.stackexchange.com/questions/205010/centos-7-rename-network-interface-without-rebooting

posted @ 2021-03-06 15:42  上善若水~小辉  阅读(833)  评论(0编辑  收藏  举报