Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CentOS 7 更改网卡名 #47

Open
johnnian opened this issue Feb 9, 2018 · 2 comments
Open

CentOS 7 更改网卡名 #47

johnnian opened this issue Feb 9, 2018 · 2 comments
Labels

Comments

@johnnian
Copy link
Owner

johnnian commented Feb 9, 2018

安装CentOS 7后,默认的网卡名字不是 「eth0」,由于在安装阿里云的Logtail客户端,其默认读取 「eth0」网卡的IP地址,因此需要更改下网卡名。

设置步骤

下面的具体步骤:(假设网卡名字是:eth123456)

步骤1: 配置创建并配置 ifcfg-eth0 文件

[root@localhost ~]# ifconfig
eth123456: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1280
        inet 192.168.0.102  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::77a1:9964:3a77:8ab9  prefixlen 64  scopeid 0x20<link>
        ether d8:9e:f3:10:7a:11  txqueuelen 1000  (Ethernet)
        RX packets 34180  bytes 4149267 (3.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32703  bytes 20801400 (19.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  memory 0xf7e80000-f7ea0000

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# mv ifcfg-eth123456 ifcfg-eth0
[root@localhost network-scripts]# vi ifcfg-eth0
#修改下面两个Key
NAME="eth0"
DEVICE="eth0"

步骤2: 修改grub配置并更新内核参数

#修改配置
[root@localhost ~]# vi /etc/sysconfig/grub
#修改这个Key:增加 net.ifnames=0 biosdevname=0
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/swap rhgb quiet"

#更新内核参数
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

步骤3: 添加udev的规则

[root@localhost ~]# vi /usr/lib/udev/rules.d/60-net.rules
#将第一行注释掉
#ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", PROGRAM="/lib/udev/rename_device", RESULT=="?*", NAME="$result"
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", ATTR{address}=="d8:9e:*:10:*11", NAME="eth0"

配置完成之后,重启服务器即可。

备注:

  • ATTR{address}=="网卡Mac地址"(可以通过 ifconfig命令查看 )
  • NAME="新网卡名字"

参考链接

@gotnix
Copy link

gotnix commented Oct 29, 2019

根据 RHEL 7 《联网指南》中的描述,并不需要这么麻烦,只需要修改 ifcfg 配置文件就行了,修改或者新增 HWADDRDEVICE 这 2 个选项,HWADDR 的值是你需要修改名称的网卡 MAC 地址, DEVICE 的值是你想要修改的新名称。注意,ifcfg 的文件名和 NAME 和网卡名称都没有关系,都不用改。参考链接如下(中英只是章节不同,内容一样):
RHEL 7 Networking Guide :
3.5. CONFIGURING IP NETWORKING WITH IFCFG FILES
11.8. CONTROLLING THE SELECTION OF NETWORK DEVICE NAMES
RHEL 7 联网指南:
2.4.1. 使用 ifcfg 文件配置网络接口
8.2. 了解设备重命名过程

关于 ifcfg 文件中的配置项,RHEL 7 中并没有很详细的文档,只能看 RHEL 6 的 《Deployment Guide》11.2. INTERFACE CONFIGURATION FILES

警告:DEVICE 的值一定要正确,否则配置不生效,网卡不会上电。

@johnnian
Copy link
Owner Author

@gotnix 感谢分享!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants