Linux 8 修改网卡名称

联系:手机/微信(+86 17813235971) QQ(107644445)QQ咨询惜分飞

标题:Linux 8 修改网卡名称

作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]

在某些情况下,我们希望通过修改网卡的名称,从而避免应用层面的一些配置(比如oracle rac 网络从千兆换成万兆,我们直接把万兆网卡的名称修改和之前使用的网卡名称一致,集群层面不用做任何配置),这里做一个测试,验证在linux 8(redhat/oracle linux/Rocky/AlmaLinux/centos等)修改网卡名称可行性:
查看当前主机网卡名称情况(有两个网卡ens160和ens192)

[root@xifenfeidg network-scripts]# cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.9 (Ootpa)
[root@xifenfeidg network-scripts]# cat /etc/oracle-release 
Oracle Linux Server release 8.9
[root@xifenfeidg network-scripts]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.118.141  netmask 255.255.255.0  broadcast 192.168.118.255
        inet6 fe80::20c:29ff:fe2d:1c85  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2d:1c:85  txqueuelen 1000  (Ethernet)
        RX packets 265118  bytes 392933574 (374.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24828  bytes 1374591 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.222.18  netmask 255.255.255.0  broadcast 192.168.222.255
        inet6 fe80::20c:29ff:fe2d:1c8f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2d:1c:8f  txqueuelen 1000  (Ethernet)
        RX packets 165  bytes 18748 (18.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 127  bytes 21755 (21.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6403  bytes 648853 (633.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6403  bytes 648853 (633.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:ab:b4:34  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@xifenfeidg ~]# nmcli d s
DEVICE  TYPE      STATE      CONNECTION 
ens160  ethernet  connected  ens160     
ens192  ethernet  connected  ens192     
virbr0  bridge    unmanaged  --         
lo      loopback  unmanaged  --   

[root@xifenfeidg ~]# cd /etc/sysconfig/network-scripts/
[root@xifenfeidg network-scripts]# ls
ifcfg-ens160  ifcfg-ens192

尝试把ens160修改为ens191,删除ens160设备

[root@xifenfeidg network-scripts]# nmcli connection delete ens160
Connection 'ens160' (ae9669d8-3db3-424e-8a0e-41d8b05197d0) successfully deleted.
[root@xifenfeidg network-scripts]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:2d:1c:85  txqueuelen 1000  (Ethernet)
        RX packets 291009  bytes 431437623 (411.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27085  bytes 1499570 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.222.18  netmask 255.255.255.0  broadcast 192.168.222.255
        inet6 fe80::20c:29ff:fe2d:1c8f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2d:1c:8f  txqueuelen 1000  (Ethernet)
        RX packets 193  bytes 21530 (21.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156  bytes 27505 (26.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6427  bytes 650593 (635.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6427  bytes 650593 (635.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:ab:b4:34  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@xifenfeidg network-scripts]# nmcli d s
DEVICE  TYPE      STATE         CONNECTION 
ens192  ethernet  connected     ens192     
ens160  ethernet  disconnected  --         
virbr0  bridge    unmanaged     --         
lo      loopback  unmanaged     --         

增加ens191设备(注意mac地址使用之前ens160)

[root@xifenfeidg network-scripts]# nmcli connection add type ethernet con-name ens191 ifname ens191  mac 00:0c:29:2d:1c:85
Connection 'ens191' (62a5fd83-8dc9-407d-83cc-1327d400fa0b) successfully added.
[root@xifenfeidg network-scripts]# 
[root@xifenfeidg network-scripts]# nmcli d s
DEVICE  TYPE      STATE         CONNECTION 
ens192  ethernet  connected     ens192     
ens160  ethernet  disconnected  --         
virbr0  bridge    unmanaged     --         
lo      loopback  unmanaged     --         

重启主机

[root@xifenfeidg network-scripts]# reboot

Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue Sep  3 14:12:20 2024 from 192.168.222.1

重启之后登录主机查看网络信息(确认网卡名称从ens160修改为ens191)

[root@xifenfeidg ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens191: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:1c:85 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 192.168.118.141/24 brd 192.168.118.255 scope global dynamic noprefixroute ens191
       valid_lft 1788sec preferred_lft 1788sec
    inet6 fe80::30e4:c5bf:692a:2753/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2d:1c:8f brd ff:ff:ff:ff:ff:ff
    altname enp11s0
    inet 192.168.222.18/24 brd 192.168.222.255 scope global noprefixroute ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe2d:1c8f/64 scope link 
       valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:ab:b4:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
[root@xifenfeidg ~]# cd /etc/sysconfig/network-scripts/
[root@xifenfeidg network-scripts]# ls -ltr
total 8
-rw-r--r--. 1 root root 308 Jul 31 05:04 ifcfg-ens192
-rw-r--r--. 1 root root 300 Sep  3 14:16 ifcfg-ens191
[root@xifenfeidg network-scripts]# nmcli d s
DEVICE  TYPE      STATE                   CONNECTION 
ens191  ethernet  connected               ens191     
ens192  ethernet  connected               ens192     
virbr0  bridge    connected (externally)  virbr0     
lo      loopback  unmanaged               --