Skip to content

Instantly share code, notes, and snippets.

@windwiny
Last active October 24, 2023 05:55
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save windwiny/c85dd8c2571b4374f874 to your computer and use it in GitHub Desktop.
Save windwiny/c85dd8c2571b4374f874 to your computer and use it in GitHub Desktop.
Linux 基于策略的路由(Linux Policy Routing)(Linux 多个网卡使用相同网段的IP地址设置)
----
Linux 基于策略的路由(Linux Policy Routing)
Linux 有传统的基于数据包目的地址的路由算法,和新的基于策略的路由算法
新算法优点:支持多个路由表,支持按数据报属性(源地址、目的地址、协议、端口、数据包大小、内容等)选择不同路由表
# 查看规则命令,后面可跟其它参数,默认为 show(list) 显示全部
ip rule
系统默认有3条记录
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
各部分解释
xx: 第一列数字是优先级,小的数字优先级高
lookup [xxx] : 表示搜索xxx路由表,1-252之间的数字或名称
中间部分内容:如 from all, 这是规则
整行的意思就是,如果一个数据包符合规则(源地址、目的地址、协议、端口、数据包大小、内容等),则使用指定路由表
# 系统最多支持255个路由表。文件 /etc/iproute2/rt_tables
# 可以看到系统保留的表及对应名称,253:default 254:main 255:local
# 可以自由添加自定义名称
# 查看路由表命令,参数可用数字或名称
ip route list table 101
ip route list table main
ip route list cache
# 清除表或内存缓存
ip route flush table 101
ip route flush cache
# 示例
##清空一个路由表
##添加一条路由
##添加这个表的默认路由
##添加规则使用这个表,如果没有指定优先级,则使用比现在规则最小数字
ip route flush table 100
ip route add 192.168.1.0/24 dev eth0 src 192.168.1.240 table 100
ip route add default dev eth0 table 100
ip rule add from 192.168.1.242 table 100 [pre 12345]
# 示例2 (Linux 多个网卡使用相同网段的IP地址设置)
## Linux 系统带4个网卡,连接同一交换机,设置同一网段的IP,
## eth0-eth3 IP分别是 192.168.1.240-243
## 默认情况下 route -n 的结果是按 eth up 的顺序决定的,
## IP 实际上都全部指向第一块 UP 的网卡,可以在其它机器上 ping 这4个IP,使用 arp -a 查看到
## 全部 IP 都关连到相同的 MAC 地址
## 这样的结果就是保留第一条网线,拔掉其它网线,其它机器还是能连通这4个IP
## 意味着4个网卡,设置了4个相同网段的IP,但是数据流全部通过第一个网卡传输
## 执行以下命令
ip route flush table 100
ip route flush table 101
ip route flush table 102
ip route flush table 103
ip route add default dev eth0 table 100
ip route add default dev eth1 table 101
ip route add default dev eth2 table 102
ip route add default dev eth3 table 103
ip rule add from 192.168.1.240 table 100
ip rule add from 192.168.1.241 table 101
ip rule add from 192.168.1.242 table 102
ip rule add from 192.168.1.243 table 103
ip route flush cache
## 在其它机器上 ping 这4个IP ,使用 arp -a 查看到
## IP 关连的 MAC 已经不相同了。
## 拔掉任意一条网线,其它机器就会连不上对应的 IP
## 这就表示不同 IP 使用各自的网卡传输数据了。
----------------
@yysalad
Copy link

yysalad commented Aug 2, 2017

#!/bin/bash

#编辑rt_tables
#echo "252 net_eth0" >> /etc/iproute2/rt_tables
#echo "251 net_eth1" >> /etc/iproute2/rt_tables
#echo "250 net_eth2" >> /etc/iproute2/rt_tables
#echo "249 net_eth3" >> /etc/iproute2/rt_tables

#清空net_eth0路由表
echo "ip route flush table net_eth0" >> /etc/rc.local
#添加一个路由规则到net_eth0表,这条规则是net_eth0这个路由表中数据包默认使用源IP17.58.0.57通过 eth0 走网关 17.58.0.254
echo "ip route add default via 17.58.0.254 dev eth0 src 17.58.0.57 table net_eth0" >> /etc/rc.local
#来自 117.58.0.57 的数据包,使用 net_192 路由表的路由规则
echo "ip rule add from 17.58.0.57 table net_eth0" >> /etc/rc.local

echo "ip route flush table net_eth1" >> /etc/rc.local
echo "ip route add default via 17.58.0.254 dev eth1 src 17.58.0.58 table net_eth1" >> /etc/rc.local
echo "ip rule add from 17.58.0.58 table net_eth1" >> /etc/rc.local

echo "ip route flush table net_eth2" >> /etc/rc.local
echo "ip route add default via 17.58.0.254 dev eth2 src 17.58.0.59 table net_eth2" >> /etc/rc.local
echo "ip rule add from 17.58.0.59 table net_eth2" >> /etc/rc.local

echo "ip route flush table net_eth3" >> /etc/rc.local
echo "ip route add default via 17.58.0.254 dev eth3 src 17.58.0.60 table net_eth3" >> /etc/rc.local
echo "ip rule add from 17.58.0.60 table net_eth3" >> /etc/rc.local

您好,我在参考了示例2后,自己做了一些改动,目前接受数据正常,各个网卡都可以独立接受数据,但是存在一个问题,我的服务器在往外面发数据的时候还是走的一个网卡eth0,请您帮忙指点一下,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment