首页 > 解决方案 > ubuntu中基于源IP的网络

问题描述

我在 ubuntu 机器上有 3 个接口。eth0/eth1/eth2。

我想访问 25.25.25.x(这是其他网络中的框)。这是拓扑:-

Ubuntu machine <----------------> Router <------------> End Machine
192.168.1.x                 192.168.1.x   25.25.25.x     25.25.25.x

我想使用 eth1 和 eth2 接口到达 25.25.25.x。所以为此我正在尝试做基于源 ip 的路由表。这是应用的配置:

ifconfig eth1 up
ifconfig eth1 192.168.1.x netmask 255.255.255.0
ip rule add from 192.168.1.x table 1 
ip route add 192.168.1.0/24 dev eth1 scope link table 1
ip route add default via 192.168.1.x dev eth1 table 1

但是,这并不成功,因为 ping 25.25.25.x -I eth1 不起作用。但是直接路线有效:

ifconfig eth1 up
ifconfig eth1 192.168.1.x netmask 255.255.255.0 
route add -net 25.25.25.0/24 gw 192.168.1.x

在非工作情况下,linux 客户端本身正在为 25.25.25.x ip 进行广播,这不应该发生。

这是 ip route 和 ip route show table 1 输出:-

root@ubuntu:~# ip rule show
0:  from all lookup local 
32765:  from 192.168.1.x lookup 1 
32766:  from all lookup main 
32767:  from all lookup default 
root@ubuntu:~# ip route show table 1
default via 192.168.1.x dev eth1 
192.168.1.0/24 dev eth1 scope link

有人可以回答为什么第一个案例不起作用吗?

标签: ubuntunetworkingrouting

解决方案


此类配置的用例:

  • 确保传入流量通过相同的接口回复,例如
  • 多宿主协议和应用程序,例如 SCTP

基本上这对我来说很好。内容/etc/network/interfaces

auto eth1
iface eth1 inet static
  address 192.168.1.19
  netmask 255.255.255.0
  gateway 192.168.1.254 # default gateway if no source address
  post-up ip link set eth1 promisc on
  post-up ip route add 192.168.1.0/24 dev eth1 table 122
  post-up ip route add default via 192.168.1.254 table 122
  post-up ip rule add from 192.168.1.19/32 table 122 priority 122
  post-up ip route flush cache
  pre-down ip rule del from 192.168.1.19/32 table 122 priority 122
  pre-down ip route flush table 122
  pre-down ip route flush cache

auto eth2
iface eth2 inet static
  address 192.168.2.19
  netmask 255.255.255.0
  post-up ip link set eth2 promisc on
  post-up ip route add 192.168.2.0/24 dev eth2 table 222
  post-up ip route add default via 192.168.2.254 table 222
  post-up ip rule add from 192.168.2.19/32 table 222 priority 222
  post-up ip route flush cache
  pre-down ip rule del from 192.168.2.19/32 table 222 priority 222
  pre-down ip route flush table 222
  pre-down ip route flush cache 

然后:

$ ip route get 8.8.8.8
8.8.8.8 via 192.168.1.254 dev eth1  src 192.168.1.19 
    cache 
$ ip route get 8.8.8.8 from 192.168.1.19
8.8.8.8 from 192.168.1.19 via 192.168.1.254 dev eth1 
    cache 
$ ip route get 8.8.8.8 from 192.168.2.19
8.8.8.8 from 192.168.2.19 via 192.168.2.254 dev eth2 
    cache 

推荐阅读