網絡會話:通俗講就是不同的網絡模式模板,nmcli命令可以實現不同模板間的快速切換。
1、如何查看系統中已有的網絡會話
[root@pc2 network-scripts]# nmcli connection show ## 可見有一個網絡會話 NAME UUID TYPE DEVICE eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728 [root@pc2 network-scripts]# nmcli con show NAME UUID TYPE DEVICE eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728
## 以上網絡會話的名稱為eno16777728
## 以上兩條命令中的connection 和 con 實現的功能是一樣的
## 后經過測試以上兩條命令等價於nmcli connetion 和 nmcli con,也就是說省略show
2、如何增加網絡會話
[root@pc2 network-scripts]# nmcli con show ## 首先查看當前的網絡會話 NAME UUID TYPE DEVICE eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728 [root@pc2 network-scripts]# nmcli con add con-name net1 ifname eno16777728 type ethernet autoconnect yes ## 增加一個網絡會話,eno16777728為當前系統的網卡,可以使用ifconfig命令查看 ## add表示添加; con-name指定增加的網絡會話名稱; ifname指定使用網卡的名稱; type指定網絡類型,這里指定為以太網; autoconnect yes指定是否自動連接,這里選擇是
Connection 'net1' (378492e9-990e-4cd7-b189-40a9e13c3beb) successfully added. [root@pc2 network-scripts]# nmcli con show ## 再次列出所有的網絡會話,已經增加了名稱為net1的網絡會話 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet -- eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728
再添加一個網絡會話,設定不自動連接,手動指定IP地址,手動指定網關
[root@pc2 network-scripts]# nmcli con show ## 查看系統中所有的網絡會話 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet -- eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728 [root@pc2 network-scripts]# nmcli con add con-name net2 ifname eno16777728 type ethernet autoconnect no ip4 192.168.5.10/24 gw4 192.168.5.1 Connection 'net2' (da1ee70d-69c3-4243-a3d3-55f990e4d5b2) successfully added.
## 以上增加網絡會話的命令中設置了不自動連接,手動指定了IP地址,手動指定網關 [root@pc2 network-scripts]# nmcli con show ## 查看系統中所有的網絡會話,已經增加了名稱為net2的網絡會話 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet -- eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728 net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet --
3、如何切換網絡會話
[root@pc2 network-scripts]# nmcli con show ## 首先列出系統中已有的網絡會話,一共三個網絡會話 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet -- eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet eno16777728 net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet -- [root@pc2 network-scripts]# ifconfig | head -n 3 ## 查看系統當前的IP地址 eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link> [root@pc2 network-scripts]# nmcli con up net1 ## 切換至net1網絡會話 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/23) [root@pc2 network-scripts]# ifconfig | head -n 3 ## 查看此時的IP地址已經改變,net1設定為自動連接 eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.61.136 netmask 255.255.255.0 broadcast 192.168.61.255 inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link> [root@pc2 network-scripts]# nmcli con up net2 ## 再次切換至net2網絡會話 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/24) [root@pc2 network-scripts]# ifconfig | head -n 3 ## 查看此時的IP地址也已經改變,net2為手動設定 eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.5.10 netmask 255.255.255.0 broadcast 192.168.5.255 inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link>
4、如何查詢當前使用的哪一個網絡會話
[root@pc2 network-scripts]# nmcli con up net2 ## 首次使用命令切換到net2網絡會話 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/25) [root@pc2 network-scripts]# nmcli device status ## 查看此時的網絡會話,已經為net2 DEVICE TYPE STATE CONNECTION eno16777728 ethernet connected net2 lo loopback unmanaged -- [root@pc2 network-scripts]# nmcli con up net1 ## 切換到net1網絡會話 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/26) [root@pc2 network-scripts]# nmcli device status ## 查看此時的網絡會話,已經為net1 DEVICE TYPE STATE CONNECTION eno16777728 ethernet connected net1 lo loopback unmanaged --
5、如何刪除網絡會話
[root@pc2 network-scripts]# nmcli con show ## 首先列出系統中所有的網絡會話 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet eno16777728 eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet -- net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet -- [root@pc2 network-scripts]# nmcli con add con-name net3 ifname eno16777728 type ethernet autoconnect no ip4 192.168.7.10/24 gw4 192.168.7.1
## 上面的命令增加一個測試的網絡會話net3,手動指定IP地址和網關
Connection 'net3' (8c80ac7f-abb7-48c4-a978-c1fc4d3d4926) successfully added. [root@pc2 network-scripts]# nmcli con show ## 查看當前所有的網絡會話,已經有了net3 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet eno16777728 eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet -- net3 8c80ac7f-abb7-48c4-a978-c1fc4d3d4926 802-3-ethernet -- net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet -- [root@pc2 network-scripts]# nmcli con delete net3 ## 刪除網絡會話net3 [root@pc2 network-scripts]# nmcli con show ## 查看當前所有的網絡會話,已經沒有net3 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet eno16777728 eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet -- net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet --
6、如何查看網絡會話的詳細信息
[root@pc2 network-scripts]# nmcli con show ## 列出系統中所有的網絡會話,注意UUID列 NAME UUID TYPE DEVICE net1 378492e9-990e-4cd7-b189-40a9e13c3beb 802-3-ethernet eno16777728 eno16777728 22ca4bf3-febe-4fc8-9091-2817e2cd6f66 802-3-ethernet -- net2 da1ee70d-69c3-4243-a3d3-55f990e4d5b2 802-3-ethernet -- [root@pc2 network-scripts]# nmcli con show net1 | wc -l ## 統計net1網絡會話詳細信息的行數 91 [root@pc2 network-scripts]# nmcli con show net1 | head -n 3 ## 查看net1前三行 connection.id: net1 connection.uuid: 378492e9-990e-4cd7-b189-40a9e13c3beb connection.interface-name: eno16777728 [root@pc2 network-scripts]# nmcli con show net2 | wc -l 47 [root@pc2 network-scripts]# nmcli con show net2 | head -n 3 connection.id: net2 connection.uuid: da1ee70d-69c3-4243-a3d3-55f990e4d5b2 connection.interface-name: eno16777728 [root@pc2 network-scripts]# nmcli con show net2 | head -n 30 ## net2網絡會話前文中設定的手動指定IP地址、網關,可以在下面看到 connection.id: net2 connection.uuid: da1ee70d-69c3-4243-a3d3-55f990e4d5b2 connection.interface-name: eno16777728 connection.type: 802-3-ethernet connection.autoconnect: no connection.timestamp: 1604556127 connection.read-only: no connection.permissions: connection.zone: -- connection.master: -- connection.slave-type: -- connection.secondaries: connection.gateway-ping-timeout: 0
802-3-ethernet.port: --
802-3-ethernet.speed: 0
802-3-ethernet.duplex: --
802-3-ethernet.auto-negotiate: yes 802-3-ethernet.mac-address: --
802-3-ethernet.cloned-mac-address: --
802-3-ethernet.mac-address-blacklist: 802-3-ethernet.mtu: auto 802-3-ethernet.s390-subchannels: 802-3-ethernet.s390-nettype: --
802-3-ethernet.s390-options: ipv4.method: manual ipv4.dns: ipv4.dns-search: ipv4.addresses: { ip = 192.168.5.10/24, gw = 192.168.5.1 } ipv4.routes: ipv4.ignore-auto-routes: no
參考:https://www.cnblogs.com/rui517hua20/p/11280136.html
