ubuntu - ubuntu中基于源IP的网络
问题描述
我在 ubuntu 机器上有 3 个接口。eth0/eth1/eth2。
- eth0 是 ip (10.102.xx) 的管理接口
- 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 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
有人可以回答为什么第一个案例不起作用吗?
解决方案
此类配置的用例:
基本上这对我来说很好。内容/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
推荐阅读
- javascript - 制作基于滑块的图像集合
- android - 一起滚动 CalendarView 和 ListView
- c - SDL2 png图像不会加载
- php - 如何在 SQL PHP 的结果中包含变量?
- vb.net - vb.net 读取正在运行的 Windows 应用程序并删除 Windows 应用程序
- android - 收到通知问题并打开一个片段
- amazon-web-services - 用于上传/putObject 图像的 AWS Lambda 函数仅适用于本地计算机
- javascript - 如何删除除最后 5 个之外的所有 html 标签?
- c# - C# 中的西蒙游戏
- c# - 基于串行输入 C# 的动态 GUI