首頁 > 軟體

CentOS 7.2修改網絡卡名稱

2020-06-16 17:20:24

背景

沒啥背景,就是VMWare裝的CentOS 7.2虛擬機器的自帶網絡卡名有點亂,想重新定義修改一下。

環境

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的情況。

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-03/141384.htm


IT145.com E-mail:sddin#qq.com