什么是网络会话?
网络会话,可以看作是一种网络模板。可以让我们在不同的工作场景中快速的切换网络环境,以适应不同的工作地点(类似于firewalld防火墙服务中的区域技术,允许用户在多个配置文件中快速切换)。
在RHEL和CentOS系统默认使用NetworkManager来提供网络服务,这是一种动态管理网络配置的守护进程,能够让网络设备保持连接状态。(https://blog.csdn.net/weixin_36522099/article/details/104770911)
可以使用nmcli命令来管理NetwoekManager服务。它可以轻松地查看网络信息和网络状态:
用法及参数(https://blog.csdn.net/weixin_36522099/article/details/104770911)
1、使用nmcli查看网络会话
[root@linuxprobe network-scripts]# nmcli connection show
NAME UUID TYPE DEVICE
eno16777728 e72a626f-2946-4285-9edf-2f5a6b172712 802-3-ethernet eno16777728
[root@linuxprobe network-scripts]# nmcli con show ## 效果一样
NAME UUID TYPE DEVICE
eno16777728 e72a626f-2946-4285-9edf-2f5a6b172712 802-3-ethernet eno16777728
2、显示指定网卡详细的网络会话
[root@linuxprobe network-scripts]# nmcli connection show eno16777728 | wc -l
61
[root@linuxprobe network-scripts]# nmcli connection show eno16777728 | head
connection.id: eno16777728
connection.uuid: e72a626f-2946-4285-9edf-2f5a6b172712
connection.interface-name: --
connection.type: 802-3-ethernet
connection.autoconnect: yes
connection.timestamp: 1604238903
connection.read-only: no
connection.permissions:
connection.zone: --
connection.master: --
[root@linuxprobe network-scripts]# nmcli connection show eno16777728 | tail
GENERAL.DEFAULT: no
GENERAL.DEFAULT6: no
GENERAL.VPN: no
GENERAL.ZONE: --
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/0
GENERAL.SPEC-OBJECT: --
GENERAL.MASTER-PATH: --
IP4.ADDRESS[1]: ip = 192.168.10.10/24, gw = 0.0.0.0
IP6.ADDRESS[1]: ip = fe80::20c:29ff:fee4:f7b9/64, gw = ::
以公司和家庭为例增加网络会话
3、增加公司网络会话
[root@linuxprobe Desktop]# nmcli con show ## 查看所有的网络会话
NAME UUID TYPE DEVICE
eno16777728 e72a626f-2946-4285-9edf-2f5a6b172712 802-3-ethernet eno16777728
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看网卡名称
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 2 bytes 120 (120.0 B)
[root@linuxprobe Desktop]# nmcli con add con-name company ifname eno16777728 autoconnect no type ethernet ip4 192.168.10.10/24 gw4 192.168.10.1 ## 增加公司网络会话
## add 表示添加网络会话,ifname指定本机的网卡名称,autoconnect 设置是否自动连接,这里手动指定IP地址, type指定网络类型
Connection 'company' (f3824b94-5bdf-4c1d-97d1-18721b010016) successfully added.
[root@linuxprobe Desktop]# nmcli con show ## 列出所有的网络会话
NAME UUID TYPE DEVICE
company f3824b94-5bdf-4c1d-97d1-18721b010016 802-3-ethernet --
eno16777728 e72a626f-2946-4285-9edf-2f5a6b172712 802-3-ethernet eno16777728
4、增加家庭网络会话
[root@linuxprobe Desktop]# nmcli con add con-name home ifname eno16777728 autoconnect yes type ethernet ## 增加家庭网络会话
## add表示增加网络会话,con-name指定增加网络会话的名字,ifname指定本机网卡名称,autoconnect指定是否自动连接(这里使用DHCP服务器自动获取IP地址),type指定网络类型
Connection 'home' (86494d2a-8971-4c28-829e-1619d66d2bc4) successfully added.
[root@linuxprobe Desktop]# nmcli con show
NAME UUID TYPE DEVICE
home 86494d2a-8971-4c28-829e-1619d66d2bc4 802-3-ethernet --
company f3824b94-5bdf-4c1d-97d1-18721b010016 802-3-ethernet --
eno16777728 e72a626f-2946-4285-9edf-2f5a6b172712 802-3-ethernet eno16777728
5、测试效果,切换至家庭网络会话和公司网络会话
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看当前IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 21 bytes 2240 (2.1 KiB)
[root@linuxprobe Desktop]# nmcli con up home ## 切换至家庭网络会话
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.7 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 46 bytes 5353 (5.2 KiB)
[root@linuxprobe Desktop]# systemctl restart network ## 重置网卡
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 52 bytes 5803 (5.6 KiB)
切换公司网络会话:
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看当前IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 66 bytes 7264 (7.0 KiB)
[root@linuxprobe Desktop]# nmcli con up company ## 切换至公司网络会话
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@linuxprobe Desktop]# ifconfig | head -n 5 ## 查看IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:f7:b9 txqueuelen 1000 (Ethernet)
RX packets 68 bytes 7384 (7.2 KiB)