linux系统中nmcli命令、查看、添加、删除、编辑网络会话


网络会话:通俗讲就是不同的网络模式模板,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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM