linux 策略路由参考

ip link set eth1 up
ip addr add 192.168.11.118/30 dev eth1
ip route add default via 192.168.11.117 table 100
ip rule add from 183.232.148.156/30 lookup 100

ubuntu 20.0 策略路由

root@i-CAF8EB6F:~# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens3:
      dhcp4: no
      addresses: [172.18.248.8/24]
      routes:
        - to: 0.0.0.0/0
          via: 172.18.248.1
          table: 101
      routing-policy:
        - from: 172.18.248.0/24
          table: 101

    ens10:
      addresses: [45.199.148.30/30]
      routes:
      - to: default
        via: 45.199.148.29
      - to: 0.0.0.0/0
        via: 45.199.148.29
        table: 102
      routing-policy:
      - from: 45.199.148.28/30
        table: 102
  version: 2

centos 策略路由配置文件 (我测试好像没成功)

创建路由表

在 CentOS 中,你可以使用 nmcli 或者手动编辑网络配置文件来实现策略路由。下面是手动编辑网络配置文件的方法。

假设你的网络接口是 eth0,你可以创建一个类似于以下内容的配置文件(比如 /etc/sysconfig/network-scripts/route-eth0):

cat /etc/sysconfig/network-scripts/route-eth0
default via 172.18.248.1 dev eth0 table 101
172.18.248.0/24 dev eth0 table 101

创建策略文件

cat /etc/sysconfig/network-scripts/rule-eth0
from 172.18.248.0/24 table 101

centos 通过/dispatcher 创建策略路由

cat /etc/NetworkManager/dispatcher.d/99-custom-routes
#!/bin/bash

IFACE=$1
ACTION=$2

case $ACTION in
    up)
        # 这里执行你的路由规则命令
        /sbin/ip rule add from 10.201.0.0/24 table 101
        /sbin/ip route add default via 10.201.0.1 table 101
        ;;
    down)
        # 这里执行你的撤销路由规则命令
        /sbin/ip rule del from 10.201.0.0/24 table 101
        /sbin/ip route del default via 10.201.0.1 table 101
        ;;
esac

exit 0

posted @ 2022-06-23 21:31  lifei888  阅读(93)  评论(0)    收藏  举报