- 所有 nmcli 命令调用完成后, 会自动更新
ifcfg-eth0
route-eth0
等等相关配置文件 - 部分参数修改完之后,调用
nmcli connection up eth0
使配置生效
查看设备
[root@aikaiyuan ~]# yum install bash-completion -y
[root@aikaiyuan ~]# nmcli device show | grep DEV
GENERAL.DEVICE: eth0
GENERAL.DEVICE: eth1
GENERAL.DEVICE: lo
安装 bash-completion , 可以多使用 tab 补全功能
eth0 和 eth1 , 关于设备名可以 查看下面文章
接口和设备映射
[root@aikaiyuan ~]# nmcli connection add type ethernet con-name eth0 ifname eth0
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=8a7935e9-4b6e-41e5-856f-51bea112f672
DEVICE=eth0
ONBOOT=yes
新创建一个 eth0 接口, 绑定到 eth0 设备上
完成之后会 会自动创建 /etc/sysconfig/network-scripts/ifcfg-eth0 文件
开机自启动
[root@aikaiyuan ~]# nmcli connection modify eth0 connection.autoconnect yes
配置IP/MASK/GATEWAY/DHCP
- 关闭DHCP, 默认是开启DHCP的
BOOTPROTO=dhcp
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.method manual
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep BOOTPRO
BOOTPROTO=none
或禁用
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.method disabled
- 配置IPv4 地址
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.add 192.168.1.144/24
[root@aikaiyuan ~]# nmcli connection show eth0 | grep ipv4.addresses
ipv4.addresses: 192.168.1.144/24
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -E 'IPADDR|PREFIX'
IPADDR=192.168.1.144
PREFIX=24
- 添加DNS
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.dns "192.168.1.10 192.168.1.20"
[root@aikaiyuan ~]# nmcli connection modify eth0 +ipv4.dns 192.168.1.30
[root@aikaiyuan ~]# nmcli connection modify eth0 -ipv4.dns 192.168.1.10
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.dns-search "aikaiyuan.com"
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.dns-options "timeout:1"
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep DNS
DNS1=192.168.1.20
DNS2=192.168.1.30
[root@aikaiyuan ~]# nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)
[root@aikaiyuan ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search aikaiyuan.com
nameserver 192.168.1.20
nameserver 192.168.1.30
options timeout:1
注意: 参数前面符号,
没有符号: 全量覆盖
“+” 加号: 添加配置
“-” 减号: 删除配置最后
up eth0
配置生效,并写入配置文件
- 添加默认网关
[root@aikaiyuan ~]# nmcli connection modify eth0 ipv4.gateway 192.168.1.1
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -E "GATEWAY|^DEFROUTE"
DEFROUTE=yes
GATEWAY=192.168.1.1
删除网关
[root@aikaiyuan ~]# nmcli c modify eth0 -ipv4.gateway ""
这里要注意
DEFROUTE=yes
配置
这里 eth0 连接公网,需要走默认路由, 也就是ip route
看到的 default via 192.168.1.1 dev eth0` 默认网关
如果有多个设备, 另外设备要 关闭DEFROUTE=no
, 否则会出现多个 默认 网关
[root@aikaiyuan ~]# nmcli connection modify eth1 ipv4.never-default yes
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep "^DEFROUTE"
DEFROUTE=no
[root@aikaiyuan ~]# nmcli connection modify eth1 +ipv4.routes "10.0.0.0/8 172.16.1.1"
[root@aikaiyuan ~]# nmcli connection modify eth1 +ipv4.routes "172.16.0.0/12 172.16.1.1"
[root@aikaiyuan ~]# cat /etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 172.16.1.1
172.16.0.0/12 via 172.16.1.1
这里是对 第二个设备
eth1
配置
添加 内网路由
修改MTU
[root@aikaiyuan ~]# nmcli connection show eth0 | grep mtu
802-3-ethernet.mtu: auto
[root@aikaiyuan ~]# nmcli connection modify eth0 802-3-ethernet.mtu 1480
[root@aikaiyuan ~]# nmcli connection show eth0 | grep mtu
802-3-ethernet.mtu: 1480
[root@aikaiyuan ~]# nmcli connection up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
MTU 一般默认 auto 即可, 如果有特许需求在修改