I am building a router with a RPi (Raspbian). It has 3 network interfaces:

  • eth0: Connected to the Internet (IP/Gateway from DHCP)
  • wlan0, wlan1: local WLAN interfaces (each serving its own SSID as AP)

Moreover a have a VPN connection tun0 to a remote network, which is connected to the internet itself.

Now I want:

  • all traffic from wlan0 to be routed through tun0 and
  • all traffic from wlan1 to be routed through eth0

In the result I want to have two WLANs, one with direct internet access and one with internet access through the VPN connection.

This was very easy using two different devices, but how to do this with only one default gateway?

1 Answer 1


You need to create a second routing table and use policy based routing. Applied to your case you need to:

  1. Setup the first default route using the main routing table. This table will be used for the traffic generated locally and for the traffic from wlan1 :

    ip route add default via <gateway_reachable_by_eth0> table main
  2. Create a second routing table vpn:

    echo 200 vpn >> /etc/iproute2/rt_tables
  3. Add a default route to the new table:

    ip route add default via <gateway_reachable_by_tun0> table vpn
  4. Indicate that all traffic from wlan0 should use this new table:

    ip rule add from <wlan0_subnet> lookup vpn
  • Is 200 a freely chosen number?
    – Lukas
    Jun 26, 2017 at 12:00
  • 2
    @Lukas: Good question, answer is in man ip-route: "Route tables: Linux-2.x can pack routes into several routing tables identified by a number in the range from 1 to 2^31 or by name from the file /etc/iproute2/rt_tables .[...]. Values (0, 253, 254, and 255) are reserved for built-in use.
    – Gohu
    Jun 26, 2017 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .