使用nmcli来管理Linux网络
nmcli
nmcli命令是CentOS8新增的,nm是服务NetworkManager的缩写,cli表示命令行界面。
systemctl status network 命令在centos8已经被官方淘汰,不再使用
查看NetworkManager服务状态,默认是开启的
systemctl status NetworkManager
Abbreviate
Parameter | Abbreviate |
---|---|
connection | c |
show | s |
modify | m |
device | d |
networking | n |
general | g |
nmcli可以查看当前主机上的网络设置
$ nmcli
ens192: connected to ens192
"VMware VMXNET3"
ethernet (vmxnet3), 00:50:56:A8:BA:09, hw, mtu 1500
ip4 default
inet4 192.168.111.170/24
route4 192.168.111.0/24 metric 100
route4 default via 192.168.111.1 metric 100
inet6 fe80::250:56ff:fea8:ba09/64
route6 fe80::/64 metric 1024
virbr0: connected (externally) to virbr0
"virbr0"
bridge, 52:54:00:B5:8B:43, sw, mtu 1500
inet4 192.168.122.1/24
route4 192.168.122.0/24 metric 0
lo: unmanaged
"lo"
loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536
DNS configuration:
servers: 8.8.8.8
interface: ens192
connection
查看网卡信息
nmcli connection
与nmcli connection show
皆可
$ nmcli c s
NAME UUID TYPE DEVICE
ens192 ab155b3e-d274-4ad7-87bb-9e4034746dcd ethernet ens192
virbr0 bdb04bd3-66f5-4129-86a1-401a99270644 bridge virbr0
显示具体的网络接口信息
nmcli connection show ens192
显示所有活动连接
nmcli connection show --active
设置IP(IPADDR)
nmcli connection modify ens192 ipv4.addresses 192.168.0.58
添加一个ipv4地址
nmcli connection modify ens192 +ipv4.addresses 192.168.0.59/24
添加子网掩码(NETMASK)
nmcli connection modify ens192 ipv4.addresses 192.168.0.58/24
删除一个ipv4
nmcli connection modify ens192 -ipv4.addresses 192.168.0.59/24
IP获取方式设置成手动(BOOTPROTO=static/none)
nmcli connection modify ens192 ipv4.method manual
添加DNS
nmcli connection modify ens192 ipv4.dns 114.114.114.114
删除DNS
nmcli connection modify ens192 -ipv4.dns 114.114.114.114
设置网关(GATEWAY)
nmcli connection modify ens192 ipv4.gateway 192.168.0.2
使用nmcli重新回载网络配置
nmcli connection reload
如果之前没有xxx的connection,则上一步reload后就已经自动生效了,否则需要使用下面命令启用新的IP
nmcli connection up ens192
设备开机自动加载(ONBOOT=yes)
nmcli connection modify connection.autoconnect yes
删除一个网卡连接
nmcli connection delete ens192
设置IP地址、网关及DNS:
nmcli connection modify ens192 ipv4.addresses 192.168.0.58/24 ipv4.gateway 192.168.0.2 ipv4.dns 114.114.114.114
仅显示IPV4信息
nmcli connection show ens192 |grep ipv4|egrep ".method|addresses|gateway|\.dns:"
device
查看设备状态:nmcli device
或nmcli device show
$ nmcli device
DEVICE TYPE STATE CONNECTION
ens192 ethernet connected ens192
virbr0 bridge connected (externally) virbr0
general
general status and operations
$ nmcli g
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full missing enabled missing enabled
networking
overall networking control
$ nmcli n
enabled