关于linux:Linux命令nmcli

78次阅读

共计 5883 个字符,预计需要花费 15 分钟才能阅读完成。

作用 :用于管制 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.

正文完
 0