route命令在多网卡下失效,因其仅操作main表且内核强制唯一默认路由,导致回程路径不一致而丢包;必须用ip rule+ip route table为每网卡建独立路由表并绑定源ip规则,同时注册表名、添加src直连路由。

route 和 ip route 都能改路由表,但直接用 route add default gw 在多网卡机器上一定会出问题——SSH 一交互就断、ping 通但服务连不上,根本原因是回程路径不一致,不是配置没生效。
为什么 route 命令在多网卡下会失效
route 只操作 main 路由表,而内核强制只允许一张默认路由存在。你执行两次 route add default gw,后一条会直接覆盖前一条;更隐蔽的问题是:即使你加了多条非默认路由(比如 route add -net 10.0.20.0/24 gw 192.168.10.1),回包仍走 main 表里那个唯一的默认网关,导致请求从 eth1 进、响应从 eth0 出,对方看到源 IP 和出口接口不匹配,直接丢包。
- 典型现象:能 SSH 连上
eth1的 IP,输入命令几秒后卡死或断开 -
route -n看起来“有两条默认路由”?那是假象——实际生效的只有一条,另一条已被覆盖 - 单网卡场景下
route没问题;多网卡必须绕过main表限制
如何用 ip rule + ip route table 实现策略路由
核心是为每张网卡建独立路由表 + 绑定源地址规则,让“从哪进、就从哪出”。顺序不能错,且必须先注册表名:
Zynq‑7000 / Zynq UltraScale+ 硬件调试技能。适用于 Xilinx Zynq(Cortex‑A9/A53 + FPGA)Linux 环境,调试 I2C、I2S、UART、GPIO 等外设。包括设备树配置、内核驱动、用户空间调试工具、交叉编译和常见问题排查,支持 PetaLinux 开发。
- 编辑
/etc/iproute2/rt_tables,追加两行(ID ≥ 100,避开系统保留值):100 tab_eth0<br>101 tab_eth1
名字只能含字母、数字、下划线,tab-eth0会失败 - 为
eth0(IP192.168.10.100/24,网关192.168.10.1)建表:ip route add 192.168.10.0/24 dev eth0 src 192.168.10.100 table tab_eth0<br>ip route add default via 192.168.10.1 dev eth0 table tab_eth0
- 为
eth1(IP10.0.20.100/24,网关10.0.20.1)建表:ip route add 10.0.20.0/24 dev eth1 src 10.0.20.100 table tab_eth1<br>ip route add default via 10.0.20.1 dev eth1 table tab_eth1
- 绑定规则:
ip rule add from 192.168.10.100/32 table tab_eth0<br>ip rule add from 10.0.20.100/32 table tab_eth1
临时路由 vs 永久路由怎么写
ip route 和 ip rule 命令本身是临时的,重启即丢。持久化要按发行版走不同路径:
- CentOS/RHEL:在
/etc/sysconfig/network-scripts/route-eth0里写192.168.10.0/24 dev eth0 src 192.168.10.100 table tab_eth0<br>default via 192.168.10.1 dev eth0 table tab_eth0
再在/etc/sysconfig/network-scripts/ifup-post或ifcfg-eth0里加POSTUP="ip rule add from 192.168.10.100/32 table tab_eth0" - Ubuntu/Debian:在
/etc/network/interfaces对应接口段落里加up ip route add 192.168.10.0/24 dev eth0 src 192.168.10.100 table tab_eth0<br>up ip route add default via 192.168.10.1 dev eth0 table tab_eth0<br>up ip rule add from 192.168.10.100/32 table tab_eth0
- 通用 fallback:把完整命令塞进
/etc/rc.local(确保它有执行权限且被 systemd 启用)
验证和排错关键点
别只信 ip route show,它默认只显示 main 表。查策略路由必须指定表或规则:
- 看某张表内容:
ip route show table tab_eth0 - 看所有规则:
ip rule show - 模拟数据包走向:
ip route get 8.8.8.8 from 192.168.10.100—— 输出应含dev eth0和table tab_eth0 - 常见漏项:忘了
src参数,导致直连路由不带源地址,回程选错接口 - 调试时先清掉旧规则:
ip rule del from 192.168.10.100/32,再重加,避免冲突
/etc/iproute2/rt_tables 里表名注册和 ip route add 中的 src 参数——缺一不可,否则规则匹配成功,但路由查表时找不到对应源地址的直连条目,照样走错路。










