主要目的是方便在不同的网络环境中快速的切换网络配置。
操作步骤:
1、查看当前系统已有的连接
[root@localhost ~]# nmcli connection show
名称 UUID 类型 设备
virbr0 fa307ff7-3562-448f-a362-b8f760201ca4 bridge virbr0
ens32 0e27f4c6-04ed-491f-a73f-48fe0f8dc834 802-3-ethernet --
2、创建company连接
[root@localhost ~]# nmcli connection add con-name company ifname ens32 autoconnect no type ethernet ip4 192.168.200.111/24 gw4 192.168.200.2 ipv4.dns 114.114.114.114
连接“company”(74c65219-9bed-4927-aa44-c73c83c44c40) 已成功添加。
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-company
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.200.111
PREFIX=24
GATEWAY=192.168.200.2
DNS1=114.114.114.114
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=company
UUID=74c65219-9bed-4927-aa44-c73c83c44c40
DEVICE=ens32
ONBOOT=no
3、创建home连接
[root@localhost ~]# nmcli connection add con-name home ifname ens32 autoconnect no type ethernet ip4 192.168.200.222/24 gw4 192.168.200.2 ipv4.dns 114.114.114.114
连接“home”(3112b3ce-8d52-4a2d-865d-0d55bd64d937) 已成功添加。
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-home
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.200.222
PREFIX=24
GATEWAY=192.168.200.2
DNS1=114.114.114.114
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=home
UUID=3112b3ce-8d52-4a2d-865d-0d55bd64d937
DEVICE=ens32
ONBOOT=no
4、使能company连接并验证连通性
[root@localhost ~]# nmcli connection up company
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.111 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::97be:576c:d124:f781 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:bd:2f:10 txqueuelen 1000 (Ethernet)
RX packets 11 bytes 1428 (1.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 31 bytes 4237 (4.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
[root@localhost ~]# nmcli connection show
名称 UUID 类型 设备
company 74c65219-9bed-4927-aa44-c73c83c44c40 802-3-ethernet ens32
virbr0 fa307ff7-3562-448f-a362-b8f760201ca4 bridge virbr0
ens32 0e27f4c6-04ed-491f-a73f-48fe0f8dc834 802-3-ethernet --
home 3112b3ce-8d52-4a2d-865d-0d55bd64d937 802-3-ethernet --
[root@localhost ~]# ping -c1 baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66 (110.242.68.66): icmp_seq=1 ttl=128 time=34.1 ms
--- baidu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 34.115/34.115/34.115/0.000 ms
5、使能home连接并验证连通性
[root@localhost ~]# nmcli connection down company
成功取消激活连接 'company'(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost ~]# nmcli connection up home
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.222 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::e7ea:7ec0:a369:20fd prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:bd:2f:10 txqueuelen 1000 (Ethernet)
RX packets 17 bytes 2192 (2.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 9058 (8.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
[root@localhost ~]# nmcli connection show
名称 UUID 类型 设备
home 3112b3ce-8d52-4a2d-865d-0d55bd64d937 802-3-ethernet ens32
virbr0 fa307ff7-3562-448f-a362-b8f760201ca4 bridge virbr0
company 74c65219-9bed-4927-aa44-c73c83c44c40 802-3-ethernet --
ens32 0e27f4c6-04ed-491f-a73f-48fe0f8dc834 802-3-ethernet --
[root@localhost ~]# ping -c1 baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=128 time=25.8 ms
--- baidu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 25.877/25.877/25.877/0.000 ms
6、down掉home连接并清理配置
[root@localhost ~]# nmcli connection down home
成功取消激活连接 'home'(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ~]# nmcli connection delete home
[root@localhost ~]# nmcli connection delete company
[root@localhost ~]# nmcli connection show
名称 UUID 类型 设备
virbr0 fa307ff7-3562-448f-a362-b8f760201ca4 bridge virbr0
ens32 0e27f4c6-04ed-491f-a73f-48fe0f8dc834 802-3-ethernet -- ---------------------------------------------------------