You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Ubuntu双网卡配置固定IP实现外部访问及静态路由永久生效求助

Ubuntu双网卡配置固定IP实现外部访问及静态路由永久生效求助

Hey there, let's sort out this annoying issue where your dual NIC routing settings vanish after a reboot on Ubuntu. You've already nailed the temporary static routing setup that fixes the single-NIC response problem—great job! Now we just need to make those settings stick permanently.

First, let's recap the working temporary configuration you've got right now, which correctly routes traffic through each NIC based on its assigned IP:

# 添加自定义路由表
echo 700 adsl1 >> /etc/iproute2/rt_tables
echo 600 adsl2 >> /etc/iproute2/rt_tables

# 配置adsl1(enp1s0)的路由规则
ip route add 192.168.1.0/24 dev enp1s0 src 192.168.1.130 table adsl1
ip route add default via 192.168.1.1 table adsl1

# 配置adsl2(wlp2s0)的路由规则
ip route add 192.168.0.0/24 dev wlp2s0 src 192.168.0.189 table adsl2
ip route add default via 192.168.0.1 table adsl2

# 向主路由表添加直连路由
ip route add 192.168.1.0/24 dev enp1s0 src 192.168.1.130
ip route add 192.168.0.0/24 dev wlp2s0 src 192.168.0.189

# 添加策略路由规则
ip rule add from 192.168.1.130 table adsl1
ip rule add from 192.168.0.189 table adsl2

Now here are two reliable methods to make these settings persist through reboots or network restarts:

方法一:使用Netplan(Ubuntu 18.04+推荐)

Netplan是现代Ubuntu版本默认的网络配置工具,我们可以直接在它的YAML配置文件中定义所有路由规则:

  1. 找到你的Netplan配置文件,通常在/etc/netplan/目录下,文件名类似00-installer-config.yaml50-cloud-init.yaml
  2. 用文本编辑器打开它(比如sudo nano /etc/netplan/00-installer-config.yaml),替换或添加以下内容(注意匹配你的网卡名称和IP信息):
network:
  ethernets:
    enp1s0:
      addresses: [192.168.1.130/24]
      routes:
        - to: 192.168.1.0/24
          via: 192.168.1.1
          table: 700
        - to: 0.0.0.0/0
          via: 192.168.1.1
          table: 700
      routing-policy:
        - from: 192.168.1.130
          table: 700
    wlp2s0:
      addresses: [192.168.0.189/24]
      routes:
        - to: 192.168.0.0/24
          via: 192.168.0.1
          table: 600
        - to: 0.0.0.0/0
          via: 192.168.0.1
          table: 600
      routing-policy:
        - from: 192.168.0.189
          table: 600
  version: 2
  1. 保存文件后,无需重启即可应用配置:
sudo netplan generate
sudo netplan apply
  1. 验证配置是否生效:
ip route show table adsl1
ip route show table adsl2
ip rule list

方法二:使用iproute2持久化脚本

如果你更习惯用iproute2命令的方式,可以创建一个启动脚本,让系统在网络启动时自动执行路由配置:

  1. 首先确保自定义路由表已永久添加到/etc/iproute2/rt_tables,打开文件sudo nano /etc/iproute2/rt_tables,确认以下两行存在:
700 adsl1
600 adsl2

保存并退出。

  1. 在网络钩子目录创建自定义脚本:
sudo nano /etc/network/if-up.d/custom-routes

粘贴你的路由配置命令:

#!/bin/sh

# 配置adsl1路由规则
ip route add 192.168.1.0/24 dev enp1s0 src 192.168.1.130 table adsl1
ip route add default via 192.168.1.1 table adsl1

# 配置adsl2路由规则
ip route add 192.168.0.0/24 dev wlp2s0 src 192.168.0.189 table adsl2
ip route add default via 192.168.0.1 table adsl2

# 向主路由表添加直连路由
ip route add 192.168.1.0/24 dev enp1s0 src 192.168.1.130
ip route add 192.168.0.0/24 dev wlp2s0 src 192.168.0.189

# 添加策略路由规则
ip rule add from 192.168.1.130 table adsl1
ip rule add from 192.168.0.189 table adsl2
  1. 给脚本添加可执行权限:
sudo chmod +x /etc/network/if-up.d/custom-routes

验证持久化效果

完成任一方法的配置后,重启服务器或重启网络服务,然后执行以下命令确认路由和规则是否仍然存在:

ip route show table adsl1
ip rule list

如果能看到正确的条目,说明你的双网卡路由配置已经可以永久生效了!

备注:内容来源于stack exchange,提问作者saeid hemati

火山引擎 最新活动