作用:用于控制 NetworkManager 的命令行工具.
1. 查看连接
罗列出所有的连接:
[root@localhost ming]# nmcli connection show
NAME UUID TYPE DEVICE
enp0s3 08e656c0-38ac-4cd1-b885-ce919221a530 ethernet enp0s3
virbr0 37b8848a-8e82-4428-b286-a48f237649d1 bridge virbr0
查看某个连接的具体信息:
[root@localhost ming]# nmcli connection show enp0s3
connection.id: enp0s3
connection.uuid: 08e656c0-38ac-4cd1-b885-ce919221a530
connection.stable-id: --
connection.type: 802-3-ethernet
connection.interface-name: enp0s3
connection.autoconnect: yes
...
2. 新建连接
为网卡 enp0s3 创建名为 conn1 的连接.
[root@localhost ming]# nmcli connection add type ethernet con-name conn1 ifname enp0s3
Connection 'conn1' (a64c5e48-eb89-41eb-81c5-6404f8379ee9) successfully added.
[root@localhost ming]# ls /etc/sysconfig/network-scripts/ | grep conn1
ifcfg-conn1
3. 启用、关闭连接
[root@localhost ming]# nmcli connection up conn1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ming]# nmcli connection down conn1
Connection 'conn1' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
4. 修改连接:IP地址
注意:修改之后需要重新加载连接,nmcli connection reload
.
设置 IP 地址:如果已有 IP 地址,则将 IP 地址修改为新的地址;且如果有多个的话,修改之后也只会剩下新指定的地址.
[root@localhost ming]# nmcli connection modify conn1 ipv4.addresses 20.0.0.11/24
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
IPADDR=20.0.0.11
PREFIX=24
添加 IP 地址:
[root@localhost ming]# nmcli connection modify conn1 +ipv4.addresses 30.0.0.10/24
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
IPADDR=20.0.0.11
PREFIX=24
IPADDR1=30.0.0.10
PREFIX1=24
删除 IP 地址:
[root@localhost ming]# nmcli connection modify conn1 -ipv4.addresses 30.0.0.10/24
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
IPADDR=20.0.0.11
PREFIX=24
5. 修改连接:DNS
设置 DNS:
[root@localhost ming]# nmcli connection modify conn1 ipv4.dns 8.8.8.8
增加 DNS:
[root@localhost ming]# nmcli connection modify conn1 +ipv4.dns 114.114.114.114
删除 DNS:
[root@localhost ming]# nmcli connection modify conn1 -ipv4.dns 114.114.114.114
6. 修改连接:路由
设置路由:
[root@localhost ming]# nmcli connection modify conn1 ipv4.routes "20.0.0.0/24 10.0.0.1"
[root@localhost ming]# cat /etc/sysconfig/network-scripts/route-conn1
ADDRESS0=20.0.0.0
NETMASK0=255.255.255.0
GATEWAY0=10.0.0.1
增加路由:
[root@localhost ming]# nmcli connection modify conn1 +ipv4.routes "30.0.0.0/24 10.0.0.1"
[root@localhost ming]# cat /etc/sysconfig/network-scripts/route-conn1
ADDRESS0=20.0.0.0
NETMASK0=255.255.255.0
GATEWAY0=10.0.0.1
ADDRESS1=30.0.0.0
NETMASK1=255.255.255.0
GATEWAY1=10.0.0.1
删除路由:
[root@localhost ming]# nmcli connection modify conn1 -ipv4.routes "30.0.0.0/24 10.0.0.1"
[root@localhost ming]# nmcli connection modify conn1 -ipv4.routes "20.0.0.0/24 10.0.0.1"
[root@localhost ming]# cat /etc/sysconfig/network-scripts/route-conn1
[root@localhost ming]#
7. 修改连接:网关
设置网关:
[root@localhost ming]# nmcli connection modify conn1 ipv4.gateway 20.0.0.1
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
GATEWAY=20.0.0.1
此处 +ipv4.gateway
的功能等同于 ipv4.gateway
. 而且 -ipv4.gateway
不会删除网关.
删除网关:
[root@localhost ming]# nmcli connection modify conn1 ipv4.gateway 0.0.0.0
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1 | grep GATEWAY
[root@localhost ming]#
8. 修改连接:自动连接
取消自动连接:
[root@localhost ming]# nmcli connection modify conn1 connection.autoconnect no
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
ONBOOT=no
...
设置自动连接:
[root@localhost ming]# nmcli connection modify conn1 connection.autoconnect yes
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
...
ONBOOT=yes
...
9. 删除连接
[root@localhost ming]# nmcli connection delete conn1
Connection 'conn1' (a64c5e48-eb89-41eb-81c5-6404f8379ee9) successfully deleted.
[root@localhost ming]# cat /etc/sysconfig/network-scripts/ifcfg-conn1
cat: /etc/sysconfig/network-scripts/ifcfg-conn1: No such file or directory
10. 重新加载连接
[root@localhost ming]# nmcli connection reload
11. 查看设备状态
[root@localhost ming]# nmcli device status
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
virbr0 bridge connected virbr0
lo loopback unmanaged --
virbr0-nic tun unmanaged --
12. 查看某个设备的详细信息
[root@localhost ming]# nmcli device show enp0s3
GENERAL.DEVICE: enp0s3
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 08:00:27:96:37:17
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: enp0s3
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnectio
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.0.2.15/24
IP4.GATEWAY: 10.0.2.2
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 10.0.2.2, mt = 100
IP4.ROUTE[2]: dst = 10.0.2.0/24, nh = 0.0.0.0, mt = 100
IP4.DNS[1]: 222.201.130.30
IP4.DNS[2]: 222.201.130.33
IP6.ADDRESS[1]: fe80::6a06:7351:c053:d277/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
13. 设置设备属性
新建一个网桥:
[root@localhost ming]# brctl addbr br0
[root@localhost ming]# nmcli device status
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
virbr0 bridge connected virbr0
br0 bridge unmanaged --
[root@localhost ming]# nmcli device set br0 managed yes
[root@localhost ming]# nmcli device status
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
virbr0 bridge connected virbr0
br0 bridge disconnected --
设置/取消自动连接:
[root@localhost ming]# nmcli device set br0 autoconnect yes
[root@localhost ming]# nmcli device set br0 autoconnect no
14. 连接设备
为 br0 新建一个连接:
[root@localhost ming]# nmcli connection add type bridge con-name conn-br0 ifname br0 ip4 30.0.0.1/24
Connection 'conn-br0' (b2dbe817-7986-4c8f-b421-ae6a42efd5fa) successfully added.
[root@localhost ming]# nmcli connection up conn-br0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/11)
然后连接设备:
[root@localhost ming]# nmcli device connect br0
Device 'br0' successfully activated with 'b2dbe817-7986-4c8f-b421-ae6a42efd5fa'.
[root@localhost ming]# nmcli device status
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
br0 bridge connected conn-br0
virbr0 bridge connected virbr0
15. 断开连接
[root@localhost ming]# nmcli device disconnect br0
Device 'br0' successfully disconnected.
[root@localhost ming]# nmcli device status
DEVICE TYPE STATE CONNECTION
enp0s3 ethernet connected enp0s3
virbr0 bridge connected virbr0
br0 bridge disconnected --
16. 删除设备
只能删除虚拟设备.
[root@localhost ming]# nmcli device delete br0
Device 'br0' successfully removed.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。