我使用网络管理器设置自定义 IP 地址,同时保持 DHCP 处于激活状态,以防我将无头设备连接到另一个网络,我会自动设置默认网关和所有内容。但是,我希望将自定义 IP 优先于 DHCP 生成的 IP。
这是我的配置:
ipv4.method: auto
ipv4.dns: 1.1.1.1,8.8.8.8
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.1.33/24
ipv4.gateway: --
ipv4.routes: --
ipv4.route-metric: -1
ipv4.route-table: 0 (unspec)
ipv4.routing-rules: --
ipv4.ignore-auto-routes: no
ipv4.ignore-auto-dns: yes
ipv4.dhcp-client-id: --
ipv4.dhcp-iaid: --
ipv4.dhcp-timeout: 0 (default)
ipv4.dhcp-send-hostname: yes
ipv4.dhcp-hostname: --
ipv4.dhcp-fqdn: --
ipv4.dhcp-hostname-flags: 0x0 (none)
ipv4.never-default: no
ipv4.may-fail: yes
ipv4.dad-timeout: -1 (default)
ipv4.dhcp-vendor-class-identifier: --
ipv4.dhcp-reject-servers: --
这将创建以下路由表:
default via 192.168.1.1 dev enp0s25 proto dhcp metric 100
192.168.1.0/24 dev enp0s25 proto kernel scope link src 192.168.1.33 metric 100
192.168.1.0/24 dev enp0s25 proto kernel scope link src 192.168.1.107 metric 100
但我希望源 192.168.1.33 路由具有较低的度量,而另一个路由用作后备。使用 nmcli (NetworkManager) 可以实现吗?
答案1
无需删除其他路由。只需使用ipv4.routes
配置选项添加新路由,如下所示:set ipv4.routes 192.168.1.0/24 50 src=192.168.1.33
。
这将生成以下配置:
ipv4.method: auto
ipv4.dns: 192.168.1.11,192.168.1.12
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.1.33/24
ipv4.gateway: --
ipv4.routes: { ip = 192.168.1.0/24, mt = 50 src=192.168.1.33 }
ipv4.route-metric: 99
ipv4.route-table: 0 (unspec)
ipv4.routing-rules: --
ipv4.replace-local-rule: -1 (default)
ipv4.ignore-auto-routes: no
ipv4.ignore-auto-dns: yes
ipv4.dhcp-client-id: --
ipv4.dhcp-iaid: --
ipv4.dhcp-timeout: 0 (default)
ipv4.dhcp-send-hostname: yes
ipv4.dhcp-hostname: --
ipv4.dhcp-fqdn: --
ipv4.dhcp-hostname-flags: 0x0 (none)
ipv4.never-default: no
ipv4.may-fail: yes
ipv4.required-timeout: -1 (default)
ipv4.dad-timeout: -1 (default)
ipv4.dhcp-vendor-class-identifier: --
ipv4.link-local: 0 (default)
ipv4.dhcp-reject-servers: --
ipv4.auto-route-ext-gw: -1 (default)
这将创建以下路由表:
default via 192.168.1.1 dev enp0s25 proto dhcp src 192.168.1.107 metric 99
192.168.1.0/24 dev enp0s25 proto static scope link src 192.168.1.33 metric 50
192.168.1.0/24 dev enp0s25 proto kernel scope link src 192.168.1.33 metric 99
192.168.1.0/24 dev enp0s25 proto kernel scope link src 192.168.1.107 metric 99
注意:该
99
指标用于检查配置发生了哪些变化以及哪些没有变化