https://www.jianshu.com/p/42bcbdcda617
nmcli [OBJECT参数] [显示参数]
OBJECT参数
g[eneral] NetworkManager's general status and operations
n[etworking] overall networking control
r[adio] NetworkManager radio switches
c[onnection] NetworkManager's connections
d[evice] devices managed by NetworkManager
a[gent] NetworkManager secret agent or polkit agent
m[onitor] monitor NetworkManager changes
显示参数
show 显示详细信息
status 显示简略信息
查看接口信息
#简略信息
nmcli device status
#详细信息
nmcli device show
#接口的详细信息
nmcli device show interface-name
查看连接信息
#详细信息
nmcli connection show
#某个接口的详细连接信息
nmcli connection show inteface-name
启动停止接口
#启动接口
nmcli connection up connection-name
nmcli device connect interface-name
#停止接口
nmcli connection down connection-name
nmcli device disconnect interface-name
P.S:建议使用 nmcli device disconnect(connect) interface-name
因为使用该命令,在启动某个连接前或发生外部事件时不会自动连接
创建连接
#创建动态获取ip地址的连接,con-name是创建连接的名字,ifname是物理设备,网络接口
nmcli connection add type eth con-name connection-name ifname interface-name
例子:nmcli connection add type eth con-name dhcp-ens33 ifname ens33
#创建静态ip地址连接
nmcli connection add type eth con-name connection-name ifname interface-name ip4 address gw4 address
例子:nmcli connection add type eth con-name static-ens33 ifname ens33 ip4 192.168.1.115/24 gw4 192.168.1.1
修改ip地址
#修改命令
nmcli connection modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+
例子:nmcli c mod static-eth1 ipv4.addr 172.16.60.20/24
nmcli c up static-eth1
修改连接是否自启
nmcli con mod ens33 connection.autoconnect no/on
配置连接的DNS
#设定单个DNS
nmcli connection modify connection-name ipv4.dns DNS1
#设定多个DNS
nmcli connection modify connection-name ipv4.dns DNS1 DNS2
例子:nmcli connection modify static-eth1 ipv4.dns 114.114.114.114
nmcli connection modify static-eth1 ipv4.dns 114.114.114.114 119.29.29.29
#添加某个连接的DNS,使用前缀 '+'
nmcli connection modify connection-name +ipv4.dns DNS3
例子:nmcli connection modify static-eth1 +ipv4.dns 192.168.1.1
nmcli c up static-eth1 //修改后需要重新激活连接
设置主机名
#查询当前主机名
nmcli general hostname
#修改主机名
nmcli general hostname new-hostname
#重启hostname(主机名)服务
systemctl restart systemd-hostnamed