二、nmcli管理设备与连接情况
- 查看设备状态
[root@localhost ~]# nmcli device
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
eno16777736 ethernet connected eno16777736
virbr0-nic ethernet connected virbr0-nic
lo loopback unmanaged --
[root@localhost ~]#
- 查看指定设备状态
[root@localhost ~]# nmcli device show eno16777736
GENERAL.DEVICE: eno16777736
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:69:1F:D8
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: eno16777736
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 192.168.1.4/24
IP4.GATEWAY: 192.168.1.1
IP4.DNS[1]: 202.101.172.35
IP4.DNS[2]: 8.8.8.8
IP6.ADDRESS[1]: fe80::20c:29ff:fe69:1fd8/64
IP6.GATEWAY: fe80::1
[root@localhost ~]#
- 查看链接状态
[root@localhost ~]# nmcli connection
add delete down edit help load modify reload show up
[root@localhost ~]# nmcli connection
NAME UUID TYPE DEVICE
virbr0-nic 7fb8f43e-926a-42a9-a205-cd6ef7033f3c 802-3-ethernet virbr0-nic
virbr0 72125c4c-417e-47ba-bd21-45d7162fb40e bridge virbr0
eno16777736 be3b17c4-48df-4513-aa1d-a653b7a57cc2 802-3-ethernet eno16777736
[root@localhost ~]#
- 查询所有活动的连接
[root@localhost ~]# nmcli connection show --active
NAME UUID TYPE DEVICE
virbr0-nic 7fb8f43e-926a-42a9-a205-cd6ef7033f3c 802-3-ethernet virbr0-nic
virbr0 72125c4c-417e-47ba-bd21-45d7162fb40e bridge virbr0
eno16777736 be3b17c4-48df-4513-aa1d-a653b7a57cc2 802-3-ethernet eno16777736
[root@localhost ~]#
- 查看指定ID连接的详细情况
[root@localhost ~]# nmcli connection show eno16777736
使用nmcli创建新的连接(不是物理设备)
- 定义一个名为ens33-dhcp的连接,配置动态IP地址,对应的设备是eno16777736,设置开机自启动,连接类型是以太网,iPv4地址自动获取
// 其实就是在/etc/sysconfig/network-scripts/下新增了一个ifcfg-ens33-dhcp的配置文件
[root@localhost ~]# nmcli connection add con-name ens33-dhcp ifname eno16777736 autoconnect yes type ethernet
Connection 'ens33-dhcp' (e5edbcf2-b4b4-4922-9390-b8d67409e52c) successfully added.
[root@localhost ~]#
// 当想要将连接切换到ens33-dhcp时:
[root@localhost ~]# nmcli connection up ens33-dhcp
// 删除连接eno16777736
[root@localhost ~]# nmcli connection delete ens33-dhcp
- 定义一个名为static的连接,配置静态IP,掩码,网关等
// 我们先添加一个网卡设备:ens38
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:69:1f:e2 txqueuelen 1000 (Ethernet)
// 设置名为ens34-static的静态连接(BOOTPROTO=sataic需要在配置文件中改),
[root@localhost ~]# nmcli connection add con-name ens34-static ifname ens38 autoconnect yes type ethernet gw4 192.168.1.1 ip4 192.168.1.7
Connection 'ens34-static' (02b15e6c-33d6-475f-b854-2a4fe2f39a60) successfully added.
[root@localhost ~]#
- 激活static连接(默认连接不自动激活,处于断开状态)
一般配置完成后就是自动激活的,如果没有激活可以手动激活
[root@localhost ~]# nmcli connection up ens34-static
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
virbr0-nic 7fb8f43e-926a-42a9-a205-cd6ef7033f3c 802-3-ethernet virbr0-nic
virbr0 72125c4c-417e-47ba-bd21-45d7162fb40e bridge virbr0
eno16777736 be3b17c4-48df-4513-aa1d-a653b7a57cc2 802-3-ethernet eno16777736
ens34-static 02b15e6c-33d6-475f-b854-2a4fe2f39a60 802-3-ethernet ens38
ens33-dhcp e5edbcf2-b4b4-4922-9390-b8d67409e52c 802-3-ethernet --
[root@localhost ~]#
- 使用++nmcli++修改已有的网络连接属性(这种方式修改后不用重启网卡)
// 可以修改很多参数,下面只是显示了,并没有改。
[root@localhost ~]# nmcli connection modify ens34-static ipv
ipv4.addresses ipv4.ignore-auto-dns ipv6.addresses ipv6.ignore-auto-routes
ipv4.dhcp-client-id ipv4.ignore-auto-routes ipv6.dhcp-hostname ipv6.ip6-privacy
ipv4.dhcp-hostname ipv4.may-fail ipv6.dhcp-send-hostname ipv6.may-fail
ipv4.dhcp-send-hostname ipv4.method ipv6.dns ipv6.method
ipv4.dns ipv4.never-default ipv6.dns-search ipv6.never-default
ipv4.dns-search ipv4.route-metric ipv6.gateway ipv6.route-metric
ipv4.gateway ipv4.routes ipv6.ignore-auto-dns ipv6.routes
[root@localhost ~]#
//修改连接后重新加载下连接
[root@localhost ~]# nmcli connection down ens34-static && nmcli connection up ens34-static
Connection 'ens34-static' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)
[root@localhost ~]#
- 添加连接方法总结
- 添加物理网卡
- 拷贝原有的配置文件名称就是新的连接名
- 删除配置文件里的UUID,NAME名称,DEVCE设备名称,修改ip等等
- 在使用nmcli connection reload 重新加载配置文件
- 启用连接nmcli connection up [连接名]
- 当然我们可以不用拷贝原有连接的配置文件这么low的方式,你可以直接用上面教的nmcli connetion add 来实现添加新的连接
- 最后我们需要查看修改的ip是否生效:ifconfig