Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two network adapters that use the same ip range, but are different networks.

I tried this but it didn't work:

iptables -t nat -A OUTPUT -p all -d 1.2.3.0/8 -j DNAT --to-destination 192.168.0.0/8 -o eth0
iptables -t nat -A OUTPUT -p all -d 3.2.1.0/8 -j DNAT --to-destination 192.168.0.0/8 -o eth1

Example:

eth0 has the ip of 192.168.0.1 on a network with the range 192.168.0.*

eth1 has the ip of 192.168.0.8 on a separate network with the range 192.168.0.*

1.2.3.* = 192.168.0.* through eth1

3.2.1.* = 192.168.0.* through eth0

Is this something I can do with iptables?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Your question is not clear, but assuming you want to route your 1.2.3.4 traffic to one NIC and 4.3.2.1 to another than you need to update you routing table using route command.

Example :

route add -net 1.2.3.4 netmask 255.0.0.0 dev eth1
route add -net 4.3.2.1 netmask 255.0.0.0 dev eth0
share|improve this answer
    
This won't do what the OP seems to want. He wants a way to use NAT so that he can reach two different machines, both themselves numbered 192.168.0.50, with different IP addresses as seen from his machine. –  David Schwartz Nov 5 '11 at 18:59
    
"both themselves numbered 192.168.0.50" where does it say that? –  Andrey Nov 5 '11 at 19:02
    
That was just an example. I picked 50 arbitrarily to try to explain what I mean. He has two networks that both use the same IP address range, and he wants to remap those addresses into distinct IP ranges as seen from his machine. That's why he's using NAT. –  David Schwartz Nov 5 '11 at 19:05
    
What I understand is that he has two physical LANs that are connected to his machine via two separate NICs. Each LAN has 192.168.0.0/8 IP addressing. If this correct? –  Andrey Nov 5 '11 at 19:05
    
Yes. So that means that 192.168.0.50 could mean two different machines. He wants to be able to access both of them, one as 1.2.3.50 and the other as 4.3.2.50 -- that's why he's using DNAT. –  David Schwartz Nov 5 '11 at 19:09
show 2 more comments

You would either have to write code specifically to do this or add 512 individual NAT rules. The DNAT module treats multiple IPs as a round-robin, not a mapping.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.