- 状态
- 活跃(带颜色字体):表示当前该connection生效
- 非活跃(正常字体):表示当前该connection不生效
- 创建 connection,配置静态 ip
nmcli c add \
type ethernet \
con-name ethX \
ifname ethX \
ipv4.addr 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.method manual \
autoconnect yes
# type ethernet:创建连接时候必须指定类型,类型有很多,可通过 "nmcli c add type -h" 看到
# con-name ethX: 表示连接(connection)的名字,可任意定义,无需和网卡名相同
# ifname ethX: 表示网卡名,这个 ethX 必须是在 nmcli d里能看到的
# ipv4.addr: 指定一个或多个 ip 地址
# ipv4.gateway: 网关
# ipv4.method: 对应ifcfg文件内容的BOOTPROTO
# ipv4.method 默认为auto,对应为BOOTPROTO=dhcp,这种时候如果指定ip,就可能导致网卡同时有dhcp分配的ip和静态ip
# ipv4.method 设置为manual表示BOOTPROTO=none,即只有静态ip
# 如果这是为ethX创建的第一个连接,则自动生效
# 如果此时已有连接存在,则该连接不会自动生效,可以执行 nmcli c up ethX-test来切换生效
# autoconnect: 开机后自动连接
- 创建 connection,配置多个静态 ip
nmcli c add \
type ethernet \
con-name ethX-X \
ifname ethX \
ipv4.addr '192.168.1.100/24,192.10.0.3.100/25' \
ipv4.routes '192.168.0.0/16 192.168.1.10,10.0.0.0/8 10.0.3.1' \
ipv4.gateway 192.168.1.254 \
ipv4.dns '8.8.8.8,4.4.4.4' \
ipv4.method manual
# ipv4.routes: 设置自定义路由
# ipv4.dns: 设置 dns
- 创建 connection,配置动态 ip
nmcli c add \
type ethernet \
con-name ethX \
ifname ethX \
ipv4.method auto
- 修改 ip
# ethX 是 connection name,也可以指定 connection UUID
# 直接替换
nmcli c modify ethX \
ipv4.addr '192.168.1.200/24'
nmcli c up ethX
# 通过nmcli c modify修改con-name,只会对应修改ifcfg文件中的NAME,而不会更改ifcfg文件名
# 增加 ip
nmcli c modify ethX \
+ipv4.addr '192.168.2.200/24'
+ipv4.routes '10.1.0.0/16 192.168.2.1'
nmcli c up ethX
# 删除 ip
nmcli c modify ethX \
-ipv4.addr '192.168.2.200/24'
-ipv4.routes '10.1.0.0/16 192.168.2.1'
nmcli c up ethX
- 操作 connection
# ethX 是 connection name,也可以指定 connection UUID
# 启动
nmcli c up ethX
# 停止
nmcli c down ethX
# 删除
nmcli c delete ethX
# 删除当前连接后,会自动选择同一个设备的其他连接来顶替生效
- 查看 connection
# ethX 是 connection name,也可以指定 connection UUID
# 查看列表
nmcli c show
# 查看活动连接列表
nmcli c show -a
# 查看指定 connection 详细信息
nmcli c show ethX
- 重载配置但不立即生效
# 重载全部 connection 的所有 ifcfg-xxxx 和 route-xxxx
nmcli c reload
# 重载指定 ifcfg-ethX 和 route-ethX
nmcli c load /etc/sysconfig/network-scripts/ifcfg-ethX
nmcli c load /etc/sysconfig/network-scripts/route-ethX
- 重载配置并立即生效
# ethXX 是 connection name,也可以指定 connection UUID
nmcli c up ethXX
# ethX 是 device name
nmcli d reapply ethX
nmcli d connect ethX