导读 nmcli是用于控制NetworkManager和报告网络状态的行工具。nmcli用于创建、显示、编辑、删除、激活和停用网络连接,以及控制和显示网络设备状态。
基本用法
nmcli [OPTIONS...] {help | general | networking | radio | connection | device | agent | monitor} [COMMAND] [ARGUMENTS...]

可以使用TAB键来补全,忘记了语法,可以按TAB键查看可用选项。如果按TAB键没有反应,也许是没有安装bash-completion

#安装bash-completion
[root@localhost ~]# yum -y install bash-completion
#退出重新登录
[root@localhost ~]# exit

tab键查看可用选项

查看NetworkManager的状态:

[root@localhost ~]# nmcli general status 
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
connected  full          enabled  enabled  enabled  enabled

列出连接配置文件信息:

[root@localhost ~]# nmcli connection show 
NAME    UUID                                  TYPE      DEVICE 
enp0s3  5005942f-a7fd-4e55-b8e7-77928d8da72d  ethernet  enp0s3

显示网卡的状态:

[root@localhost ~]# nmcli device status 
DEVICE  TYPE      STATE      CONNECTION 
enp0s3  ethernet  connected  enp0s3     
lo      loopback  unmanaged  --         

可以使用nmcli命令行启动或停止网卡,相当于ifup/ifdown命令。

停止网卡:
[root@localhost ~]# nmcli device disconnect enp0s3

开启网卡:
[root@localhost ~]# nmcli device connect enp0s3

修改网卡信息,由原来的dhcp自动获取改为手动添加地址:
[root@localhost ~]# nmcli connection modify enp0s3 ipv4.method manual ipv4.addresses 192.168.0.110/24 ipv4.gateway 192.168.0.1 ipv4.dns 202.102.128.68
[root@localhost ~]# nmcli connection up enp0s3

查看网卡配置文件信息

添加额外IP
[root@localhost ~]# nmcli connection modify enp0s3 +ipv4.addresses 192.168.0.108/24
# 启用配置文件
[root@localhost ~]# nmcli connection up enp0s3
# 查看ip地址
[root@localhost ~]# ip add show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:7b:d3:32 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.107/24 brd 192.168.0.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.0.108/24 brd 192.168.0.255 scope global secondary noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::d4d9:9043:1056:9e1a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
删除额外IP
[root@localhost ~]# nmcli connection modify enp0s3 -ipv4.addresses 192.168.0.108/24
# 启用配置文件
[root@localhost ~]# nmcli connection up enp0s3
# 查看ip地址
[root@localhost ~]# ip ad show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:7b:d3:32 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.107/24 brd 192.168.0.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::d4d9:9043:1056:9e1a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
总结

在命令行界面,对于直接修改配置文件,用nmcli修改网卡信息还是非常方便的。

本文原创地址://q13zd.cn/centos7-nmcli-tools.html编辑:逄增宝,审核员:逄增宝