Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is related to a previous one I asked.

I want to set up an OpenVPN dual client box such that a computer whose gateway is set to eth0:0 gets all their traffic routed through one OpenVPN tunnel, and a computer whose gateway is set to eth0:1 gets all their traffic routed through a different OpenVPN tunnel. Eventually, I want to be able to have multiple computers connecting to my dual client box, sharing that OpenVPN tunnel (but unaware of each other).


I've set up the ethernet aliases in my dual client box and I've set my gateway on a connecting host to an ethernet alias on the dual client box. However, the routing doesn't appear to be happening properly. Any pings from my connecting host go directly to the default gateway of the dual client box instead of being routed through the tunnel.

These are the route commands I'm using once the tunnel is set up:

ip route add default dev $1 table us_table
ip rule add from [ip address of my ethernet alias] table us_table
ip route flush cache
iptables -t nat -A POSTROUTING -o $1 -j MASQUERADE[/code]

This is the routing table after the tunnel's set:

root@ahost:/etc/openvpn# ip route show via dev tun0 dev eth0  proto kernel  scope link  src dev tun0  proto kernel  scope link  src 
default via dev eth0  metric 100

This is the us_table routing table:

root@ahost:/etc/openvpn# ip route show table us_table
default dev tun0  scope link 

My rules:

root@ahost:/etc/openvpn# ip rule show
0:  from all lookup local 
32765:  from lookup us_table 
32766:  from all lookup main 
32767:  from all lookup default 

Any suggestions on what I'm missing or doing wrong? I'm stumped here...

share|improve this question
Could you please provide a simple network diagram for this, just to avoid confusion. – wolfgangsz Jun 13 '11 at 11:12
Added a link to a simple network diagram – serpah Jun 14 '11 at 5:00
Mhm, I believe I already advised you to drop the idea of interface aliases (as this won't work) and simply use the ip rule from <client> lookup <table> syntax? – the-wabbit Jun 14 '11 at 13:12
Yep, you did. I wanted to know if there was a way to set the gateway on the connecting laptops without having to add routing rules to the Gateway server. – serpah Jun 15 '11 at 2:14
There will be a lot of computers connecting and disconnecting from this Gateway server and I would like to have a way to set up the routing without having to add & delete rules for every client – serpah Jun 15 '11 at 10:22

You have set the rule to match the ethernet alias address:

ip rule add from [ip address of my ethernet alias] table us_table

However, this will never match, as the masquerading is in the POSTROUTING chain, i.e. it happens after the routing decisions have already been made.

I have never used policy based routing, so I can only guess what you might have to do to get this working. The man page says that you can perform NAT before other rules, this might be able to sort you out. Alternatively you could use the actual IP address of the laptops or workstations as the source addresses (or ranges).

share|improve this answer
up vote 0 down vote accepted

Based on @syneticon-dj's suggestion, I assigned different subnets for the different OpenVPN clients and set up routing so that all IP addresses in a specific subnet get routed to a specific tun device.

This means that all connecting clients have to set their gateway to the OpenVPN Gateway and their IP address has to be in the correct subnet.

My commands once the OpenVPN tun is up are:

ip route add default dev tun0 table us_table
ip rule add from table us_table
ip route flush cache
iptables -t nat -A POSTROUTING -o $1 -j MASQUERADE
share|improve this answer

Your Answer


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.