大网数据广告活动
服务器运维问答

How to set up two default routes in linux

· 52CE 我爱测

TL;DR How to set up two default routes in linux (corresponding to two interfaces), such that incoming traffic from both interfaces is accepted. Scenario I have a host having two NICs eth0 and eth1 . Its connected to two networks,…

问题原文

原帖链接 · Tanmay · CC BY-SA 3.0

TL;DR

How to set up two default routes in linux (corresponding to two interfaces), such that incoming traffic from both interfaces is accepted.

Scenario

I have a host having two NICs eth0 and eth1. Its connected to two networks, say:

  • Dev: eth0, IP: 1.1.1.4, Netmask: 255.255.255.0, Gateway: 1.1.1.1

  • Dev: eth1, IP: 2.2.2.4, Netmask: 255.255.255.0, Gateway: 2.2.2.1

(Both networks are connected to the internet.)

The routes are (ip route show):

default via 1.1.1.1 dev eth0 metric 1
default via 2.2.2.1 dev eth1 metric 2
1.1.1.0/24 dev eth0 src 1.1.1.4
2.2.2.0/24 dev eth1 src 2.2.2.4

(Lower metric means higher priority.)


Now when someone pings 2.2.2.4 from some external host (say 4.4.4.4), there is no reply.

The output of tcpdump -i eth1 on my host is:

22:41:27.431539 IP 4.4.4.4 > 2.2.2.4: ICMP echo request, id 8625, seq 4, length 64
22:41:28.439492 IP 4.4.4.4 > 2.2.2.4: ICMP echo request, id 8625, seq 5, length 64
22:41:29.447666 IP 4.4.4.4 > 2.2.2.4: ICMP echo request, id 8625, seq 6, length 64
22:41:30.455528 IP 4.4.4.4 > 2.2.2.4: ICMP echo request, id 8625, seq 7, length 64

After some research, it turns out that when linux network stack receives a packet from 4.4.4.4, it checks its routing table to see what interface should have been used for 4.4.4.4 (which is eth0 in the routing table). Now, since the packet came from eth1 interface, linux simply discards it (This policy is probably to prevent IP spoofing).


What I want is is:

  • Outgoing traffic (locally originated) should use eth0 interface by default.

  • Incoming traffic should be accepted from both interfaces. (So 4.4.4.4 should be able to ping both 1.1.1.4 and 2.2.2.4)

社区高分解答

原帖链接 · Jonas Bjork · CC BY-SA 4.0

This phenomena is called unicast rpf (reverse path forwarding) as a standard but reverse path filter in Linux. You may disable this without any important negative consequences (unless you're an isp.. and a router).

echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter

Then your example should work as you want it to.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-12-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。