路由在 Linux 中不起作用

Kev*_*len 0 linux routing

这是我的 ifconfig 文件:http : //pastebin.com/5ZUGYbnB

auto eth0
iface eth0 inet static
        address 180.xxx.xx.68
        netmask 255.255.255.248
        network 180.xxx.xx.64
        broadcast 180.xxx.xx.71
        gateway 180.xxx.xx.65
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 180.xxx.xx.65 8.8.8.8

auto eth1
iface eth1 inet static
        address 10.96.92.23
        netmask 255.255.255.0
        network 10.96.92.0
        broadcast 10.96.92.255
        gateway 10.96.92.1
        dns-nameservers 10.96.92.11
        up route add -net 10.86.82.0 netmask 255.255.255.0 gw 10.96.92.1
        down route del -net 10.86.82.0 netmask 255.255.255.0 gw 10.96.92.1
Run Code Online (Sandbox Code Playgroud)

这是我的路由表http://pastebin.com/VVwCe7ZF

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
180.xxx.xx.64   0.0.0.0         255.255.255.248 U     0      0        0 eth0
10.86.82.0      10.96.92.1      255.255.255.0   UG    0      0        0 eth1
10.96.92.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
0.0.0.0         180.xxx.xx.65   0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.96.92.1      0.0.0.0         UG    100    0        0 eth1
Run Code Online (Sandbox Code Playgroud)

但它不会将任何数据包转发到另一个网络。我的路由表有问题吗?

小智 6

对于初学者来说,您有两个默认路由。在某些情况下可能会出现这种情况,但这不太可能是您想要的。

通常会使用一个默认路由,对于您,我猜您想使用该 180.x 网络上的默认网关。

从 eth1 定义中删除网关行。

要检查的另一件事是/proc/sys/net/ipv4/ip_forward已启用:

cat /proc/sys/net/ipv4/ip_forward 
Run Code Online (Sandbox Code Playgroud)

并检查您得到的是 1,而不是 0。

如果没有,则需要启用它,最好的方法是编辑/etc/sysctl.conf和设置net.ipv4.ip_forward=1

这将使转发工作,但如果此时它仍然不起作用(如果没有更好地描述您要实现的目标,很难说出您的要求是什么),您可能需要使用 iptables 查看 netfilter SNAT 或 MASQ .