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

Layer 3 IP Forwarding/NAT between two interfaces

· 52CE 我爱测

I have a strange configuration, let's see if you can give me some advice :) I have two networks (10.X/24 and 20.X/24) conected by a linux machine between them. This linux have two interfaces, each one atached to one of those netw…

问题原文

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

I have a strange configuration, let's see if you can give me some advice :)

I have two networks (10.X/24 and 20.X/24) conected by a linux machine between them. This linux have two interfaces, each one atached to one of those networks. Devices atached to those networks doesn't have gateway and I can't change that neither install aditional software like vpn's, etc... Also I can't change subnet mask.

What I want is to comunicate one device of each network, and I was thinking is to configure a something like IP FORWARDING but changing the SRC and DST, I think this is more like a NAT. The idea is to do that if I ping to 20.254 (doesnt exist) from 20.50(device from net A), it redirects this packet to 10.50 changing src to 10.254. It's something like virtual IP's.

The main idea is to do this translation:

input eth0 (network A) SRC 20.50 DST 20.254 --> output eth1 SRC 10.254 DST 10.50

input eth1 (network B) SRC 10.50 DST 10.254 --> output eth0 SRC 20.254 DST 20.50

Keep in mind that addreces 10.254 and 20.254 doesnt exists and I use them to contact with the other network BECAUSE I can't set a gateway to ask for unknown networks neither add static routes.

Let's see if someone can give me the best way to do that.

Thanks!

已采纳解答

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

You need to assign these IPs to your gateway box like:

ip addr add 192.168.10.254/24 dev eth0
ip addr add 192.168.20.254/24 dev eth1

Then, you can do the translation using NAT rules like:

iptables -t nat -A PREROUTING -i eth0 -s 192.168.20.50 -d 192.168.20.254 -j DNAT --to-destination 192.168.10.50
iptables -t nat -A POSTROUTING -o eth1 -d 192.168.10.50 -j MASQUERADE

The other translation can be done similarly. Also, the above rules are written based on your translation example. You can generalize the check for subnet using /24 notation.

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

资料来源与适用范围

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

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

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