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.

Good morning one and all. In our network we have a single Linux machine that has aliased IP addresses. Each of these IP addresses sit on a network controlled by a remote router to an ISP.

We want to use our Linux box as the gateway for our internal network (10.0.0.x) and for the Linux box to then forward the outgoing traffic to one of our routers on another network.

All things i have read is about masquerading between two physical interface cards; however we have only one network card, listening to multiple IP addresses.

On the Linux box itself; it can ping and access the internet fine using one of the routers as the upstream gateway.

So our configuration is as follows:

: Linux Box 
  eth0:0 = 10.0.0.5
  eth0:1 = 192.168.137.5
  GW: 192.168.137.1


: Router#1
  IP: 192.168.137.1
  Connection to internet via ISP


: Network Machines
  IP: 10.0.0.x
  GW: 10.0.0.5

So the question is, what is my setup for iptables/nat on the Linux box to allow it to accept packets on the 10.0.0.x subnet and route them out to the specific IP address configured for the gateway. When i try to use eth0:1 as my -d then iptables complains of invalid characters and from what I read on the internet, this was disabled in iptables (http://lkml.indiana.edu/hypermail/linux/net/9705.1/0016.html).

Can someone assist please? I am sure I am missing something real obvious here; all my historical knowledge has been two separate ethX network interfaces.

thank you


Update; here is the "hackity" iptables script

WIFIBACKUP=192.168.137.1

iptables -F INPUT
iptables -F FORWARD
iptables -F OUTPUT
iptables -F -t nat

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -t nat -A PREROUTING -s 10.0.0.0/32 -j REDIRECT --to $WIFIBACKUP

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

-d is for --destination, and expects an address (or net). If destination is "any", then simply don't specify it.

The redirect does not do NAT.
As you are using aliases, using -i and -o does not work. The workaround is to use -d and -s. You can negate with "!". So, for all traffic from 10.0.0.0/24, that has a destination that is NOT 10.0.0.0/24, do masquerade:

iptables -t nat -A POSTROUTING -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j MASQUERADE

share|improve this answer
    
Thanks 3molo ... yes you are right, NAT is what we are looking for here. I've updated the original post –  Alan Williamson Apr 6 '11 at 9:24
    
mmm i don't think i can do that. 192.168.137.1 is a box configured/supplied by our ISP. So we can't touch that one. –  Alan Williamson Apr 6 '11 at 9:35
    
you are genius! that was the missing link. Thank you, that works perfectly. –  Alan Williamson Apr 6 '11 at 9:41
    
That doesn't work. I get "bad argument '10.0.0.0' –  Matt Jan 29 '13 at 5:56
1  
The correct format is iptables -t nat -A POSTROUTING -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j MASQUERADE –  Matt Jan 29 '13 at 6:07
show 1 more 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.