nmcli是用于控制NetworkManager和报告网络状态的命令行工具。nmcli用于创建、显示、编辑、删除、激活和停用网络连接,以及控制和显示网络设备状态。 |
---|
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命令。
显示具体的网络接口信息
# nmcli connection show eth0
停止网卡:
[root@localhost ~]# nmcli device disconnect enp0s3
开启网卡:
[root@localhost ~]# nmcli device connect enp0s3
重启网卡:
nmcli connection reload ens18
修改网卡信息,由原来的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: 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: 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 connection reload
总结
在命令行界面,对于直接修改配置文件,用nmcli修改网卡信息还是非常方便的
来源: https://blog.csdn.net/weixin_39859055/article/details/111213711