在Rocky Linux 9中通过nmcli
配置路由表的步骤如下:
-
查看当前连接:
nmcli connection show
确认要修改的网络连接名称(如eth0、ens192)。
-
添加静态路由:
nmcli connection modify <连接名> +ipv4.routes "<目标网络>/<掩码> <网关>"
示例:添加目标网络
192.168.2.0/24
,网关192.168.1.1
:nmcli connection modify eth0 +ipv4.routes "192.168.2.0/24 192.168.1.1"
-
应用配置:
nmcli connection up <连接名>
-
验证路由表:
ip route # 或 route -n
-
高级配置:
- 指定Metric:添加
metric
值(如100):nmcli connection modify eth0 +ipv4.routes "192.168.3.0/24 192.168.1.2 100"
- 永久生效:配置会自动写入
/etc/sysconfig/network-scripts/route-<连接名>
文件。
- 指定Metric:添加
注意:
- 使用
+
符号保留原有路由,用=
会覆盖现有配置。 - 若需删除路由,将
+
替换为-
。 - 确保NetworkManager服务已启用:
systemctl enable --now NetworkManager