服务器使用的是ubuntu 20.04版本,出现的问题是我在192.168.40.43网段访问数据,时而通时而不通
我本地路由使用双lan网口,服务器是接入与本地不同的网口。

以下是服务器的网络配置:

vim /etc/netplan/00-installer-config.yaml


# This is the network config written by 'subiquity'
network:
  ethernets:
    ens3:
      dhcp4: false
      addresses:
      - 192.168.1.80/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
        - 192.168.1.1
      routes:
        - to: default
          via: 192.168.1.1
          metric: 100

    ens4:
      dhcp4: false
      addresses:
      - 192.168.8.80/24
      gateway4: 192.168.8.1
      nameservers:
        addresses:
        - 192.168.8.1
      #routes: []
      routes:
        - to: 192.168.8.0/24
          via: 192.168.8.1
          metric: 200
  version: 2

步骤:配置多路由表和路由规则

  1. 手动添加路由表
    为了使用多个路由表,需要编辑 /etc/iproute2/rt_tables 文件,为每个路由表添加一个条目。用以下命令编辑 rt_tables 文件并添加表项:

    sudo nano /etc/iproute2/rt_tables

    在文件的末尾添加如下内容:

    100    table1
    200    table2

    这些表名分别对应 ens3ens4

  2. 手动设置路由
    配置好路由表后,手动添加路由。首先,使用以下命令来为每个路由表添加默认路由:

    sudo ip route add default via 192.168.1.1 table table1
    sudo ip route add default via 192.168.8.1 table table2
  3. 配置路由规则
    接下来,需要为每个源地址添加路由规则,确保流量通过正确的路由表:

    sudo ip rule add from 192.168.1.80 table table1
    sudo ip rule add from 192.168.8.80 table table2

    这将确保从 192.168.1.80 发出的流量会使用 table1(即 ens3 的路由表),从 192.168.8.80 发出的流量会使用 table2(即 ens4 的路由表)。

  4. 保存路由规则
    为了确保每次重启后这些规则仍然有效,你可以将它们添加到系统启动脚本中。编辑 /etc/rc.local 文件(如果系统使用它)或创建一个新的启动脚本来执行这些命令。

    sudo nano /etc/rc.local

    exit 0 之前添加以下命令:

    ip route add default via 192.168.1.1 table table1
    ip route add default via 192.168.8.1 table table2
    ip rule add from 192.168.1.80 table table1
    ip rule add from 192.168.8.80 table table2
  5. 应用 netplan 配置

    使用 netplan 应用你的网络配置:

    sudo netplan apply
  6. 验证配置
    使用以下命令验证路由和路由规则是否正确:

    • 查看路由表:

      ip route show table table1
      ip route show table table2
    • 查看路由规则:

      ip rule show

总结

由于 netplan 不支持 routing-policy,我们采用了手动配置多路由表的方式,结合 ip rule 来为不同的源 IP 配置不同的路由表。这种方式能够解决路由冲突问题,并让你的服务器可以正确地通过不同的网卡与不同的网络进行通信。

在 Ubuntu 20.04 中,rc.local 文件默认是没有的,因为 Ubuntu 已经转向了 systemd 来管理系统服务和启动任务。不过,你可以通过创建一个 systemd 服务单元 来执行启动时的自定义命令。

创建一个 systemd 服务来代替 rc.local

  1. 创建一个自定义的 systemd 服务单元文件

    你需要为你的自定义命令创建一个 systemd 服务单元文件。使用以下命令创建一个新的 systemd 服务单元:

    sudo nano /etc/systemd/system/custom-routing.service
  2. 编写服务单元文件

    在文件中输入以下内容:

    [Unit]
    Description=Custom Routing Rules
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/bin/bash -c "ip route add default via 192.168.1.1 table table1; ip route add default via 192.168.8.1 table table2; ip rule add from 192.168.1.80 table table1; ip rule add from 192.168.8.80 table table2"
    RemainAfterExit=true
    
    [Install]
    WantedBy=multi-user.target

    解释:

    • [Unit] 部分定义了服务的描述和依赖关系,After=network.target 确保服务在网络配置完成后启动。
    • [Service] 部分定义了要执行的命令,ExecStart 中包含了你之前的路由和规则设置命令。
    • RemainAfterExit=true 表示服务执行完成后不会被停止。
    • [Install] 部分指定了服务将在系统启动时运行。
  3. 重新加载 systemd 配置

    创建服务单元文件后,你需要让 systemd 重新加载配置文件并启动服务。

    sudo systemctl daemon-reload
  4. 启用并启动服务

    启用服务,这样它会在每次启动时自动运行:

    sudo systemctl enable custom-routing.service

    然后,手动启动该服务来测试是否能正常工作:

    sudo systemctl start custom-routing.service
  5. 检查服务状态

    使用以下命令检查服务的状态,确保没有错误:

    sudo systemctl status custom-routing.service

    如果服务运行成功,你会看到类似 Active: active (exited) 的状态。

总结

在 Ubuntu 20.04 中,rc.local 文件已经被弃用,取而代之的是 systemd。你可以创建一个自定义的 systemd 服务单元 来执行启动时的命令。这种方式比 rc.local 更加灵活且符合现代的系统管理方法。

标签: none

添加新评论