CentOS 7.2更改网卡名称

Posted 处女座打字员

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7.2更改网卡名称相关的知识,希望对你有一定的参考价值。

背景

没啥背景,就是VMWare装的CentOS虚拟机的自带网卡名有点乱,想重新定义一下。

 

环境

1、VMWare虚拟机

6张网卡

 

2、系统

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)

并且是最小化安装。

 

网卡信息

 

网卡配置文件

 

配置内容

[root@localhost network-scripts]# cat ifcfg-eno16777736 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=1160a71e-e5b5-4378-b742-c1bf0cba6f00
DEVICE=eno16777736
ONBOOT=no

 

这里启动下网卡,获得IP,好连接shell

[root@localhost network-scripts]# sed -i \'/ONBOOT/s/no/yes/\' ifcfg-eno*
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.128/24 brd 192.168.140.255 scope global dynamic eno16777736
       valid_lft 1799sec preferred_lft 1799sec
    inet6 fe80::20c:29ff:fea3:b15c/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:66 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.129/24 brd 192.168.140.255 scope global dynamic eno33554960
       valid_lft 1800sec preferred_lft 1800sec
    inet6 fe80::20c:29ff:fea3:b166/64 scope link 
       valid_lft forever preferred_lft forever
4: eno50332184: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.130/24 brd 192.168.140.255 scope global dynamic eno50332184
       valid_lft 1800sec preferred_lft 1800sec
    inet6 fe80::20c:29ff:fea3:b170/64 scope link 
       valid_lft forever preferred_lft forever
5: eno67109408: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.131/24 brd 192.168.140.255 scope global dynamic eno67109408
       valid_lft 1800sec preferred_lft 1800sec
    inet6 fe80::20c:29ff:fea3:b17a/64 scope link 
       valid_lft forever preferred_lft forever
6: eno83886632: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:84 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.132/24 brd 192.168.140.255 scope global dynamic eno83886632
       valid_lft 1800sec preferred_lft 1800sec
    inet6 fe80::20c:29ff:fea3:b184/64 scope link 
       valid_lft forever preferred_lft forever
7: eno100663856: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.133/24 brd 192.168.140.255 scope global dynamic eno100663856
       valid_lft 1801sec preferred_lft 1801sec
    inet6 fe80::20c:29ff:fea3:b18e/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]# 

默认使用DHCP,都是自动获取的IP

 

配置

CentOS7使用了“一致性网络命名方法”,这里举例,将网卡名截断为前5个字符,如eno16777736改为eno16。

1、更改网卡配置文件名

[root@localhost network-scripts]# ll
total 252
-rw-r--r--. 1 root root   290 Jan 11 22:41 ifcfg-eno10
-rw-r--r--. 1 root root   288 Jan 11 22:41 ifcfg-eno16
-rw-r--r--. 1 root root   288 Jan 11 22:41 ifcfg-eno33
-rw-r--r--. 1 root root   288 Jan 11 22:41 ifcfg-eno50
-rw-r--r--. 1 root root   288 Jan 11 22:41 ifcfg-eno67
-rw-r--r--. 1 root root   288 Jan 11 22:41 ifcfg-eno83
-rw-r--r--. 1 root root   254 Sep 16  2015 ifcfg-lo
lrwxrwxrwx. 1 root root    24 Jan 11 22:31 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root   627 Sep 16  2015 ifdown-bnep
[root@localhost network-scripts]# 

 

并更改配置文件内容,举例

[root@localhost network-scripts]# cat ifcfg-eno16 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16
UUID=803d2d05-493b-489b-8201-1d3fc67878aa
DEVICE=eno16
ONBOOT=yes
[root@localhost network-scripts]# 

  

2、关闭“一致性网络设备命名法”

更改前

[root@localhost ~]# cat /etc/sysconfig/grub 
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed \'s, release .*$,,g\' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
[root@localhost ~]# 

 

添加net.ifnames=0 biosdevname=0

更改后

[root@localhost ~]# cat /etc/sysconfig/grub 
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed \'s, release .*$,,g\' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
[root@localhost ~]# 

 

更新GRUB、内核配置

[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-f034e6e19df748b7a27fad77b06e3179
Found initrd image: /boot/initramfs-0-rescue-f034e6e19df748b7a27fad77b06e3179.img
done
[root@localhost ~]# 

 

3、添加设备规则配置文件

修改/etc/udev/rules.d/70-persistent-net.rules,没有该文件则新建文件

内容如下

[root@localhost ~]# cat /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:5c", ATTR{type}=="1", KERNEL=="eth*", NAME="eno16"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:8e", ATTR{type}=="1", KERNEL=="eth*", NAME="eno10"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:66", ATTR{type}=="1", KERNEL=="eth*", NAME="eno33"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:70", ATTR{type}=="1", KERNEL=="eth*", NAME="eno50"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:7a", ATTR{type}=="1", KERNEL=="eth*", NAME="eno67"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:a3:b1:84", ATTR{type}=="1", KERNEL=="eth*", NAME="eno83"
[root@localhost ~]# 

注意MAC地址与NAME相匹配(可以用ip addr查看)

 

4、重启

[root@localhost ~]# reboot

 

使用

[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.128/24 brd 192.168.140.255 scope global dynamic eno16
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b15c/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:66 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.129/24 brd 192.168.140.255 scope global dynamic eno33
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b166/64 scope link 
       valid_lft forever preferred_lft forever
4: eno50: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.130/24 brd 192.168.140.255 scope global dynamic eno50
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b170/64 scope link 
       valid_lft forever preferred_lft forever
5: eno67: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.131/24 brd 192.168.140.255 scope global dynamic eno67
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b17a/64 scope link 
       valid_lft forever preferred_lft forever
6: eno83: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:84 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.132/24 brd 192.168.140.255 scope global dynamic eno83
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b184/64 scope link 
       valid_lft forever preferred_lft forever
7: eno10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.133/24 brd 192.168.140.255 scope global dynamic eno10
       valid_lft 1768sec preferred_lft 1768sec
    inet6 fe80::20c:29ff:fea3:b18e/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

看见网卡名称都已经更改了

 

再来设置个静态ip,以eno83为例

[root@localhost network-scripts]# cat ifcfg-eno83 
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno83
UUID=919f599a-c262-4868-be7e-0646b1e98dee
DEVICE=eno83
IPADDR=192.168.140.140
PREFIX=24
ONBOOT=yes
[root@localhost network-scripts]# 

 

重启网络

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# 
[root@localhost network-scripts]# 
[root@localhost network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.128/24 brd 192.168.140.255 scope global dynamic eno16
       valid_lft 1802sec preferred_lft 1802sec
    inet6 fe80::20c:29ff:fea3:b15c/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:66 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.129/24 brd 192.168.140.255 scope global dynamic eno33
       valid_lft 1802sec preferred_lft 1802sec
    inet6 fe80::20c:29ff:fea3:b166/64 scope link 
       valid_lft forever preferred_lft forever
4: eno50: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.130/24 brd 192.168.140.255 scope global dynamic eno50
       valid_lft 1803sec preferred_lft 1803sec
    inet6 fe80::20c:29ff:fea3:b170/64 scope link 
       valid_lft forever preferred_lft forever
5: eno67: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:7a brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.131/24 brd 192.168.140.255 scope global dynamic eno67
       valid_lft 1803sec preferred_lft 1803sec
    inet6 fe80::20c:29ff:fea3:b17a/64 scope link 
       valid_lft forever preferred_lft forever
6: eno83: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:84 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.140/24 brd 192.168.140.255 scope global eno83
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fea3:b184/64 scope link 
       valid_lft forever preferred_lft forever
7: eno10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a3:b1:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.133/24 brd 192.168.140.255 scope global dynamic eno10
       valid_lft 1802sec preferred_lft 1802sec
    inet6 fe80::20c:29ff:fea3:b18e/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost network-scripts]# 

 

改好了~

 

ps:

 

 

1、如果没有“配置-3添加设备规则配置文件“这步,其他不变,系统重启后将使用eth0、eth1、eth2、...这样的网卡名,此时系统会尝试启动配置文件(eno16等)描述的网卡,但网卡不存在,这些网卡将启动失败,并且开机后无法重启网络,提示failed to start lsb:bring up/down巴拉巴拉~

2、/etc/sysconfig/network-scripts这里目录里的网卡配置文件不要有多余的,每个ifcfg-xxxx对应且仅对应一个网卡,否则容易造成重启网络失败,提示failed to start lsb:bring up/down巴拉巴拉~

3、按这个顺序更改,可以避免更改网卡名称并重启之后,无法重启network的情况。

 

以上是关于CentOS 7.2更改网卡名称的主要内容,如果未能解决你的问题,请参考以下文章

教你更改 RHEL7 或者 CentOS 7 的网卡名称

CentOS7更改网卡名称

centos7更改网卡名称

CentOS7更改网卡名称

CentOS更改网卡名称eth0

CentOS更改网卡名称eth0