CentOS8下网络管理工具nmcli常用命令
简介
注:下面命令同样适用于RHEL7/CENTOS7
。
先做个对比,CENTOS7
和CENTOS8
下/etc/sysconfig/network-scripts
下文件:
CENTOS7
下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | [root@mysrs network-scripts]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) [root@mysrs network-scripts]# pwd /etc/sysconfig/network-scripts [root@mysrs network-scripts]# ll 总用量 252 -rw-r--r--. 1 root root 354 4月 24 2022 ifcfg-eth0 -rw-r--r--. 1 root root 254 8月 19 2019 ifcfg-lo lrwxrwxrwx. 1 root root 24 11月 6 2021 ifdown -> ../../../usr/sbin/ifdown -rwxr-xr-x. 1 root root 654 8月 19 2019 ifdown-bnep -rwxr-xr-x. 1 root root 6532 8月 19 2019 ifdown-eth -rwxr-xr-x. 1 root root 6190 4月 1 2020 ifdown-ib -rwxr-xr-x. 1 root root 781 8月 19 2019 ifdown-ippp -rwxr-xr-x. 1 root root 4540 8月 19 2019 ifdown-ipv6 lrwxrwxrwx. 1 root root 11 11月 6 2021 ifdown-isdn -> ifdown-ippp -rwxr-xr-x. 1 root root 2130 8月 19 2019 ifdown-post -rwxr-xr-x. 1 root root 1068 8月 19 2019 ifdown-ppp -rwxr-xr-x. 1 root root 870 8月 19 2019 ifdown-routes -rwxr-xr-x. 1 root root 1456 8月 19 2019 ifdown-sit -rwxr-xr-x. 1 root root 1621 12月 9 2018 ifdown-Team -rwxr-xr-x. 1 root root 1556 12月 9 2018 ifdown-TeamPort -rwxr-xr-x. 1 root root 1462 8月 19 2019 ifdown-tunnel lrwxrwxrwx. 1 root root 22 11月 6 2021 ifup -> ../../../usr/sbin/ifup -rwxr-xr-x. 1 root root 12415 8月 19 2019 ifup-aliases -rwxr-xr-x. 1 root root 910 8月 19 2019 ifup-bnep -rwxr-xr-x. 1 root root 13574 8月 19 2019 ifup-eth -rwxr-xr-x. 1 root root 10357 4月 1 2020 ifup-ib -rwxr-xr-x. 1 root root 12075 8月 19 2019 ifup-ippp -rwxr-xr-x. 1 root root 11893 8月 19 2019 ifup-ipv6 lrwxrwxrwx. 1 root root 9 11月 6 2021 ifup-isdn -> ifup-ippp -rwxr-xr-x. 1 root root 650 8月 19 2019 ifup-plip -rwxr-xr-x. 1 root root 1064 8月 19 2019 ifup-plusb -rwxr-xr-x. 1 root root 4997 8月 19 2019 ifup-post -rwxr-xr-x. 1 root root 4154 8月 19 2019 ifup-ppp -rwxr-xr-x. 1 root root 2001 8月 19 2019 ifup-routes -rwxr-xr-x. 1 root root 3303 8月 19 2019 ifup-sit -rwxr-xr-x. 1 root root 1755 12月 9 2018 ifup-Team -rwxr-xr-x. 1 root root 1876 12月 9 2018 ifup-TeamPort -rwxr-xr-x. 1 root root 2780 8月 19 2019 ifup-tunnel -rwxr-xr-x. 1 root root 1836 8月 19 2019 ifup-wireless -rwxr-xr-x. 1 root root 5419 8月 19 2019 init.ipv6-global -rw-r--r--. 1 root root 20678 8月 19 2019 network-functions -rw-r--r--. 1 root root 31027 8月 19 2019 network-functions-ipv6 [root@mysrs network-scripts]# |
CENTOS8
下:
1 2 3 4 5 6 7 8 9 | [root@ecs-2ae6 network-scripts]# cat /etc/redhat-release CentOS Stream release 8 [root@ecs-2ae6 network-scripts]# pwd /etc/sysconfig/network-scripts [root@ecs-2ae6 network-scripts]# ll total 4 -rw-r--r--. 1 root root 273 Dec 2 15:03 ifcfg-ens160 [root@ecs-2ae6 network-scripts]# |
1.NetworkManager管理
在rhel8
(含centos8
)上,在/etc/sysconfig/network-scripts/
里也看不到任何脚本文件,没有传统的network.service
。因此只能通过其它方法进行网络配置,包括NM
命令工具集。换言之,在rhel8
上,必须开启NM
,否则无法使用网络。
1.1 查看NM纳管状态:
1 2 3 | [root@ecs-2ae6 ~]# nmcli n enabled [root@ecs-2ae6 ~]# |
1.2 开启NM接管
1 2 | [root@ecs-2ae6 ~]# nmcli n on [root@ecs-2ae6 ~]# |
1.3 关闭NM纳管
1 2 | [root@ecs-2ae6 ~]# nmcli n off #最好不要用 [root@ecs-2ae6 ~]# |
2.网络管理命令行工具nmcli
在nmcli
中有2个
命令最为常用:
2.1 nmcli connection
表示连接,可理解为配置文件,相当于ifcfg-ethX
。可以简写为nmcli c
2.2 nmcli device
表示设备,可理解为实际存在的网卡(包括物理网卡和虚拟网卡)。可以简写为nmcli d
在NM
里,有2个
维度:连接(connection)和设备(device)。
这是多对一的关系。想给某个网卡配ip,首先NM要能纳管这个网卡。设备里存在的网卡(即nmcli d
可以看到的),就是NM纳管的。接着,可以为一个设备配置多个连接(即nmcli c
可以看到的),每个连接可以理解为一个ifcfg
配置文件。同一时刻,一个设备只能有一个连接活跃。可以通过nmcli c up
切换连接。
connection
有2种状态:
- 活跃(带颜色字体):表示当前该
connection
生效 - 非活跃(正常字体):表示当前该
connection
不生效
device
有4种常见状态:
- connected:已被NM纳管,并且当前有活跃的connection
- disconnected:已被NM纳管,但是当前没有活跃的connection
- unmanaged:未被NM纳管
- unavailable:不可用,NM无法纳管,通常出现于网卡link为down的时候(比如ip link set enp0s3 down)。
3. nmcli connection配置
下面是执行了nmcli c show
命令:
1 2 3 4 5 | [root@ecs-2ae6 ~]# nmcli c show NAME UUID TYPE DEVICE Wired connection 1 0e7955cc-fa7e-3a20-bc1d-0c9495f1f096 ethernet eth0 ens160 e61a9810-0e27-422c-ae97-cbb6bb862fce ethernet -- [root@ecs-2ae6 ~]# |
输出中:
- 第一列是
connection
名字,简称con-name
(注意con-name
不是网卡名) - 第二列是
connection
的UUID
- 最后一列才是网卡名(标准说法叫
device
名),可通过nmcil d
查看device
对connection
做操作时需要指定标识,标识可以是con-name
、UUID
、如果存在ifcfg
文件则也可以用ifcfg
的完整路径,即/etc/sysconfig/network-scripts/ifcfg-enp0s160
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@ecs-2ae6 ~]# nmcli c show 0e7955cc-fa7e-3a20-bc1d-0c9495f1f096 connection.id: Wired connection 1 connection.uuid: 0e7955cc-fa7e-3a20-bc1d-0c9495f1f096 connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: eth0 connection.autoconnect: yes connection.autoconnect-priority: -999 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0 (default) connection.auth-retries: -1 connection.timestamp: 1673585607 connection.read-only: no connection.permissions: -- connection.zone: -- connection.master: -- connection.slave-type: -- connection.autoconnect-slaves: -1 (default) connection.secondaries: -- connection.gateway-ping-timeout: 0 connection.metered: unknown …… |
或者用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@ecs-2ae6 ~]# nmcli c show /etc/sysconfig/network-scripts/ifcfg-ens160 connection.id: ens160 connection.uuid: e61a9810-0e27-422c-ae97-cbb6bb862fce connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: ens160 connection.autoconnect: yes connection.autoconnect-priority: 0 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0 (default) connection.auth-retries: -1 connection.timestamp: 1669972534 connection.read-only: no connection.permissions: -- connection.zone: -- connection.master: -- connection.slave-type: -- connection.autoconnect-slaves: -1 (default) connection.secondaries: -- connection.gateway-ping-timeout: 0 |
nmcli connecton
常见操作:
3.1 启用connection
1 | [root@ecs-2ae6 ~]# nmcli c up ens160 相当于ifup |
3.2 停止connection
1 | [root@ecs-2ae6 ~]# nmcli c down 相当于ifdown |
3.3 删除connection
1 | [root@ecs-2ae6 ~]# nmcli c delete ens160 相当于ifdown并删除ifcfg |
3.4 查看connection列表
1 2 3 4 | [root@ecs-2ae6 ~]# nmcli c show NAME UUID TYPE DEVICE Wired connection 1 0e7955cc-fa7e-3a20-bc1d-0c9495f1f096 ethernet eth0 ens160 e61a9810-0e27-422c-ae97-cbb6bb862fce ethernet -- |
3.5 查看connection详细信息
1 2 3 4 5 6 7 8 9 10 | [root@ecs-2ae6 ~]# nmcli c show ens160 connection.id: ens160 connection.uuid: e61a9810-0e27-422c-ae97-cbb6bb862fce connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: ens160 connection.autoconnect: yes connection.autoconnect-priority: 0 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0 (default) |
3.6 重载配置文件
1 | [root@ecs-2ae6 ~]# nmcli c reload #不会马上生效 |
4. nmcli device配置
4.1 查看device列表
1 2 3 4 | DEVICE TYPE STATE CONNECTION eth0 ethernet connected Wired connection 1 lo loopback unmanaged -- [root@ecs-2ae6 ~]# |
4.2 查看所有device详细信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [root@ecs-2ae6 ~]# nmcli d show GENERAL.DEVICE: eth0 GENERAL.TYPE: ethernet GENERAL.HWADDR: FA:16:3E:E3:87:3F GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: Wired connection 1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 192.168.0.88/24 IP4.GATEWAY: 192.168.0.1 IP4.ROUTE[1]: dst = 192.168.0.0/24, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.0.1, mt = 100 IP4.ROUTE[3]: dst = 169.254.169.254/32, nh = 192.168.0.254, mt = 100 IP4.DNS[1]: 100.125.108.250 IP4.DNS[2]: 114.114.114.114 IP4.DOMAIN[1]: openstacklocal IP6.ADDRESS[1]: fe80::23b7:5424:b509:a6f4/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 1024 GENERAL.DEVICE: lo GENERAL.TYPE: loopback GENERAL.HWADDR: 00:00:00:00:00:00 GENERAL.MTU: 65536 GENERAL.STATE: 10 (unmanaged) GENERAL.CONNECTION: -- GENERAL.CON-PATH: -- IP4.ADDRESS[1]: 127.0.0.1/8 IP4.GATEWAY: -- IP6.ADDRESS[1]: ::1/128 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ::1/128, nh = ::, mt = 256 [root@ecs-2ae6 ~]# |
4.3 查看指定device的详细信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@ecs-2ae6 ~]# nmcli d show eth0 GENERAL.DEVICE: eth0 GENERAL.TYPE: ethernet GENERAL.HWADDR: FA:16:3E:E3:87:3F GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: Wired connection 1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 192.168.0.88/24 IP4.GATEWAY: 192.168.0.1 IP4.ROUTE[1]: dst = 192.168.0.0/24, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.0.1, mt = 100 IP4.ROUTE[3]: dst = 169.254.169.254/32, nh = 192.168.0.254, mt = 100 IP4.DNS[1]: 100.125.108.250 IP4.DNS[2]: 114.114.114.114 IP4.DOMAIN[1]: openstacklocal IP6.ADDRESS[1]: fe80::23b7:5424:b509:a6f4/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 1024 [root@ecs-2ae6 ~]# |
4.4 激活网卡
1 2 3 | [root@ecs-2ae6 ~]# nmcli d connect eth0 Device 'eth0' successfully activated with '0e7955cc-fa7e-3a20-bc1d-0c9495f1f096'. [root@ecs-2ae6 ~]# |