Ubuntu中基于源IP的网络

时间:2018-11-05 05:05:47

标签: ubuntu networking routing

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

  • eth0是具有IP(10.102.x.x)的管理接口
  • eth1是IP 192.168.1.x的专用接口
  • eth2是另一个具有IP 192.168.2.x的专用接口

我想达到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路由和ip路由显示表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

有人可以回答为什么第一种情况不起作用吗?

2 个答案:

答案 0 :(得分:0)

您的配置没有意义。

  

从192.168.1.x表1添加IP规则

这表示,如果我们知道来自192.168.1.x的数据包,则可以使用表1进行路由。

  

ip路由通过192.168.1.x dev eth1表1添加默认值

这表示如果我们知道我们正在使用表1路由数据包,则默认路由是通过192.168.1.x。

看到问题了吗?只有表1知道您的默认路由可以通过192.168.1.x路由器访问。而且只有知道默认路由可以通过192.168.1.x到达,才能告诉您使用表1。因此,不应期望此配置能像您期望的那样起作用。

我不明白您为什么尝试使用基于源的路由。您只想将特定的路线路由到特定的目的地。很难在不理解为什么使事情变得比看起来需要复杂的情况下提出更改建议。

答案 1 :(得分:0)

这种配置的用例:

基本上,这对我来说很好。 /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