OpenVPN : routing all traffic through the VPN tunnel

I’m really into OpenVPN these days, see my two previous posts about it :

Setting up OpenVPN for your road warriors :

Setting up a VPN between two sites :

Today : how to route all traffic through the OpenVPN tunnel

On the server side :

First of all, if you want to route all your traffic through the VPN tunnel, you need to turn on IP forwarding (also called routing) and add a masquerading rule on the server (where eth0 is the device connecting you to the internet) :

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

iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE

To make routing persistent, see

Then, here’s the OpenVPN configuration :

port 10000
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem
push "dhcp-option DOMAIN"
push "dhcp-option DNS"
push "redirect-gateway def1"
keepalive 10 120
user nobody
group nogroup
log vpn.log
verb 1
chroot /tmp

You can see the option redirect-gateway that is responsible for creating all the routes on the client computer when the connection is set up.

The two other push options are only taken into account by Windows clients (to my knowledge).
If you want to change the DNS resolution of your linux clients, you need to use the up and down options on the client (see below).

Client configuration :

vpn.conf :

dev tun
proto udp
port 10000
ca ./ca.crt
cert ./user.crt
key ./user.key 
verb 5
up ./
down ./
ping 60
ping-restart 120 :

mv /etc/resolv.conf /etc/resolv.conf.bak
echo "search" > /etc/resolv.conf
echo "nameserver" >> /etc/resolv.conf :

mv /etc/resolv.conf.bak /etc/resolv.conf

When connecting to the server (with verbose option set to 5), we can see the server pushing the route settings to the client.

Fri Jul 18 23:22:19 2008 us=838005 ifconfig tun0 pointopoint mtu 1500
Fri Jul 18 23:22:19 2008 us=843211 route add -net 72.x.x.x netmask gw
Fri Jul 18 23:22:19 2008 us=845178 route add -net netmask gw
Fri Jul 18 23:22:19 2008 us=848568 route add -net netmask gw
Fri Jul 18 23:22:19 2008 us=850460 route add -net netmask gw

On the client, the routes :

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
72.x.x.x UGH   0      0        0 wlan0 UH    0      0        0 tun0   U     0      0        0 wlan0   UG    0      0        0 tun0       UG    0      0        0 tun0       UG    0      0        0 tun0         UG    0      0        0 wlan0

31 thoughts on “OpenVPN : routing all traffic through the VPN tunnel

  1. Hello Sébastien,

    I have this openvpn starting string on the server side:

    openvpn –float –port 1196 –cipher AES-256-CBC –dev tun –dev-node /dev/tun –ifconfig –persist-tun –persist-local-ip –persist-remote-ip –comp-lzo –push “dhcp-option DOMAIN” –push “dhcp-option DNS″ –push “dhcp-option DNS″ –push “redirect-gateway def1″ –ping 15 –ping-restart 300 –verb 5 –secret /etc/openvpn/vpn.key –persist-key –chroot /home/nobody –user nobody –group nobody &> /root/vpn.txt&

    And this one on the client side:

    openvpn –float –remote –port 1196 –cipher AES-256-CBC –dev tun –dev-node /dev/tun –ifconfig –persist-tun –persist-local-ip –persist-remote-ip –comp-lzo –ping 15 –ping-restart 300 –verb 5 –secret /etc/openvpn/vpn.key –persist-key –chroot /home/nobody –user nobody –group nobody &> /root/vpn.txt&

    Starting it up, it works wonderfully well, both machines communicate perfectly, but my client isn’t changing its own routes as you claim. Do you see something wrong I might be doing for it not work?

    Thank you,
    Tiago from Brazil

  2. Hi,

    This has been tested on Debien Etch, openvpn version 2.0.

    Which version of openvpn are you using ?

  3. Hi Sébastien,

    Thx for the HowTo.
    I got it working finally, but I can not use my server as DNS for the VPN.
    push “dhcp-option DNS [external-dns-server-address]”
    it works
    but with
    push “dhcp-option DNS [my-Server-address]”
    it won’t.
    In my LAN I use the Server as DNS and have DNS-forwarding enabled on it.

    Do you have any Idea?


  4. Taraman,

    When you say it’s not working.. I assume your VPN client correctly gets the local DNS in /etc/resolv.conf but the DNS is not resolving ?

    If it is your problem, you just need to allow queries for the VPN range in your DNS.


  5. Hi Sébastien,
    great howto!I would like to ask you something…i configured tun device, and i pushed 2 routes of internal network succesfully, and internal dns.
    When i connect from outside to the server, i get the routing to the internal routes through virtual adapter, while the other destinations routed through my gateway.But i got all dns lookups through virtual adapter.
    Is it possible to split dns resolution?

    • Hi,

      There must be a way but I’m not aware of anything easy to implement.

      Since I assume you are a residential user, the quickest way I can think of is :
      – don’t make the vpn send the DNS information to the client
      – store hostnames of remote machine in your hosts file

  6. Hi,
    Having a heck of a time w/ this – 2 linux boxes doing site2site. Tunnel works great but I can’t get to the Internet from a laptop on the “client” side of the tunnel. I see the DNS query traverse the tunnel but then nothing. Linux boxes are multi-homed – any special procedures in this case?


  7. Hi Sébastien,

    What if I want to use my another public IP address, like eth0:1 ? I can’t do:
    iptables -t nat -A POSTROUTING -s -o eth0:1 -j MASQUERADE
    cause I’m getting error:
    weird character in interface `eth0:1′ (No aliases, :, ! or *)

    any idea what shall i do ?

  8. Are you sure it’s not working ?
    You can get this error message and it could work actually.

  9. Hi please Can you help me, as I’m not sure where the problem is, server or client ?
    I want to route all internet traffic through the openvpn server.
    I can log into the openvpn, and ping from the client

    Server routing
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface * UH 0 0 0 tun0 * UH 0 0 0 eth0 * UH 0 0 0 eth0 * UH 0 0 0 eth0 UG 0 0 0 tun0 * U 0 0 0 eth0 UG 0 0 0 tun0 * U 0 0 0 eth0 * U 0 0 0 eth0
    default UG 0 0 0 eth0

    Client routing

    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface * UH 0 0 0 tun0 UGH 0 0 0 eth1 UG 0 0 0 tun0 * U 2 0 0 eth1
    loopback * U 0 0 0 lo
    default UG 0 0 0 tun0

    # Which local IP address should OpenVPN
    # listen on? (optional)

    # Which TCP/UDP port should OpenVPN listen on?
    # If you want to run multiple OpenVPN instances
    # on the same machine, use a different port
    # number for each one. You will need to
    # open up this port on your firewall.
    port 1194

    # TCP or UDP server?
    ;proto tcp
    proto udp

    # “dev tun” will create a routed IP tunnel,
    # “dev tap” will create an ethernet tunnel.
    # Use “dev tap0″ if you are ethernet bridging
    # and have precreated a tap0 virtual interface
    # and bridged it with your ethernet interface.
    # If you want to control access policies
    # over the VPN, you must create firewall
    # rules for the the TUN/TAP interface.
    # On non-Windows systems, you can give
    # an explicit unit number, such as tun0.
    # On Windows, use “dev-node” for this.
    # On most systems, the VPN will not function
    # unless you partially or fully disable
    # the firewall for the TUN/TAP interface.
    ;dev tap
    dev tun

    # Windows needs the TAP-Win32 adapter name
    # from the Network Connections panel if you
    # have more than one. On XP SP2 or higher,
    # you may need to selectively disable the
    # Windows firewall for the TAP adapter.
    # Non-Windows systems usually don’t need this.
    ;dev-node MyTap

    # SSL/TLS root certificate (ca), certificate
    # (cert), and private key (key). Each client
    # and the server must have their own cert and
    # key file. The server and all clients will
    # use the same ca file.
    # See the “easy-rsa” directory for a series
    # of scripts for generating RSA certificates
    # and private keys. Remember to use
    # a unique Common Name for the server
    # and each of the client certificates.
    # Any X509 key management system can be used.
    # OpenVPN can also use a PKCS #12 formatted key file
    # (see “pkcs12″ directive in man page).
    ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt
    cert /etc/openvpn/easy-rsa/2.0/keys/server.crt
    key /etc/openvpn/easy-rsa/2.0/keys/server.key # This file should be kept secret

    # Diffie hellman parameters.
    # Generate your own with:
    # openssl dhparam -out dh1024.pem 1024
    # Substitute 2048 for 1024 if you are using
    # 2048 bit keys.
    dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem

    # Configure server mode and supply a VPN subnet
    # for OpenVPN to draw client addresses from.
    # The server will take for itself,
    # the rest will be made available to clients.
    # Each client will be able to reach the server
    # on Comment this line out if you are
    # ethernet bridging. See the man page for more info.

    # Maintain a record of client virtual IP address
    # associations in this file. If OpenVPN goes down or
    # is restarted, reconnecting clients can be assigned
    # the same virtual IP address from the pool that was
    # previously assigned.
    ifconfig-pool-persist ipp.txt

    # Configure server mode for ethernet bridging.
    # You must first use your OS’s bridging capability
    # to bridge the TAP interface with the ethernet
    # NIC interface. Then you must manually set the
    # IP/netmask on the bridge interface, here we
    # assume Finally we
    # must set aside an IP range in this subnet
    # (start= end= to allocate
    # to connecting clients. Leave this line commented
    # out unless you are ethernet bridging.

    # Configure server mode for ethernet bridging
    # using a DHCP-proxy, where clients talk
    # to the OpenVPN server-side DHCP server
    # to receive their IP address allocation
    # and DNS server addresses. You must first use
    # your OS’s bridging capability to bridge the TAP
    # interface with the ethernet NIC interface.
    # Note: this mode only works on clients (such as
    # Windows), where the client-side TAP adapter is
    # bound to a DHCP client.

    # Push routes to the client to allow it
    # to reach other private subnets behind
    # the server. Remember that these
    # private subnets will also need
    # to know to route the OpenVPN client
    # address pool (
    # back to the OpenVPN server.
    push “route″
    ;push “route″

    # To assign specific IP addresses to specific
    # clients or if a connecting client has a private
    # subnet behind it that should also have VPN access,
    # use the subdirectory “ccd” for client-specific
    # configuration files (see man page for more info).

    # EXAMPLE: Suppose the client
    # having the certificate common name “Thelonious”
    # also has a small subnet behind his connecting
    # machine, such as
    # First, uncomment out these lines:
    # client-config-dir ccd
    # route
    # Then create a file ccd/Thelonious with this line:
    # iroute
    # This will allow Thelonious’ private subnet to
    # access the VPN. This example will only work
    # if you are routing, not bridging, i.e. you are
    # using “dev tun” and “server” directives.

    # EXAMPLE: Suppose you want to give
    # Thelonious a fixed VPN IP address of
    # First uncomment out these lines:
    client-config-dir ccd
    # Then add this line to ccd/Thelonious:
    # ifconfig-push

    # Suppose that you want to enable different
    # firewall access policies for different groups
    # of clients. There are two methods:
    # (1) Run multiple OpenVPN daemons, one for each
    # group, and firewall the TUN/TAP interface
    # for each group/daemon appropriately.
    # (2) (Advanced) Create a script to dynamically
    # modify the firewall in response to access
    # from different clients. See man
    # page for more info on learn-address script.
    ;learn-address ./script

    # If enabled, this directive will configure
    # all clients to redirect their default
    # network gateway through the VPN, causing
    # all IP traffic such as web browsing and
    # and DNS lookups to go through the VPN
    # (The OpenVPN server machine may need to NAT
    # or bridge the TUN/TAP interface to the internet
    # in order for this to work properly).
    push “redirect-gateway def1 bypass-dhcp”

    # Certain Windows-specific network settings
    # can be pushed to clients, such as DNS
    # or WINS server addresses. CAVEAT:
    # The addresses below refer to the public
    # DNS servers provided by
    push “dhcp-option DNS″
    ;push “dhcp-option DNS″

    # Uncomment this directive to allow different
    # clients to be able to “see” each other.
    # By default, clients will only see the server.
    # To force clients to only see the server, you
    # will also need to appropriately firewall the
    # server’s TUN/TAP interface.

    # Uncomment this directive if multiple clients
    # might connect with the same certificate/key
    # files or common names. This is recommended
    # only for testing purposes. For production use,
    # each client should have its own certificate/key
    # pair.

    # The keepalive directive causes ping-like
    # messages to be sent back and forth over
    # the link so that each side knows when
    # the other side has gone down.
    # Ping every 10 seconds, assume that remote
    # peer is down if no ping received during
    # a 120 second time period.
    keepalive 10 120

    # For extra security beyond that provided
    # by SSL/TLS, create an “HMAC firewall”
    # to help block DoS attacks and UDP port flooding.
    # Generate with:
    # openvpn –genkey –secret ta.key
    # The server and each client must have
    # a copy of this key.
    # The second parameter should be ‘0’
    # on the server and ‘1’ on the clients.
    ;tls-auth ta.key 0 # This file is secret

    # Select a cryptographic cipher.
    # This config item must be copied to
    # the client config file as well.
    ;cipher BF-CBC # Blowfish (default)
    ;cipher AES-128-CBC # AES
    ;cipher DES-EDE3-CBC # Triple-DES

    # Enable compression on the VPN link.
    # If you enable it here, you must also
    # enable it in the client config file.

    # The maximum number of concurrently connected
    # clients we want to allow.
    ;max-clients 100

    # It’s a good idea to reduce the OpenVPN
    # daemon’s privileges after initialization.
    # You can uncomment this out on
    # non-Windows systems.
    ;user nobody
    ;group nobody

    # The persist options will try to avoid
    # accessing certain resources on restart
    # that may no longer be accessible because
    # of the privilege downgrade.

    # Output a short status file showing
    # current connections, truncated
    # and rewritten every minute.
    status openvpn-status.log

    # By default, log messages will go to the syslog (or
    # on Windows, if running as a service, they will go to
    # the “Program FilesOpenVPNlog” directory).
    # Use log or log-append to override this default.
    # “log” will truncate the log file on OpenVPN startup,
    # while “log-append” will append to it. Use one
    # or the other (but not both).
    ;log openvpn.log
    ;log-append openvpn.log

    # Set the appropriate level of log
    # file verbosity.
    # 0 is silent, except for fatal errors
    # 4 is reasonable for general usage
    # 5 and 6 can help to debug connection problems
    # 9 is extremely verbose
    verb 3

    # Silence repeating messages. At most 20
    # sequential messages of the same message
    # category will be output to the log.
    ;mute 20

  10. If you use the config in the post, it should work flawlessly.
    I’m not sure about some configuration you’ve used.. (bypass-dhcp in push redirect ?)

  11. Hello,

    Thanks for posting this, but i get problem with testing this.
    How do you keep the openvpn connection alive when you try to route all traffic throu it?
    dont you need to exclude the ip address of the openvpn server from that route?

    When the connection is established and routing setup i get “write UDPv4: No route to host (code=65)”.

  12. @ R :
    please post your routes and what you get in the openvpn logs..
    i’ve been using this particular configuration for the past 2 years now, and it works like a charm.


  13. Hello, Thanks for the quick replay.
    I am posting the end parts of the logfiles.
    To me it seems like i am missing a route to exclude the openvpn-server from the default route.

    Ok logfile from client side:

    Wed Aug 11 19:22:22 2010 /sbin/ifconfig tun0 mtu 1500 netmask up
    Wed Aug 11 19:22:22 2010 /sbin/route add -net
    add net gateway
    Wed Aug 11 19:22:22 2010 /sbin/route add -net
    add net gateway
    Wed Aug 11 19:22:22 2010 /sbin/route add -net
    add net gateway
    Wed Aug 11 19:22:22 2010 /sbin/route add -net
    add net gateway
    Wed Aug 11 19:22:22 2010 Initialization Sequence Completed
    Wed Aug 11 19:22:22 2010 write UDPv4: No route to host (code=65)
    Wed Aug 11 19:22:23 2010 write UDPv4: No route to host (code=65)
    …. (keeps repeating the same line)

    Server openvpn logfile:

    Wed Aug 11 17:24:45 2010 MULTI: Learn: -> clinet1/
    Wed Aug 11 17:24:45 2010 MULTI: primary virtual IP for clinet1/
    Wed Aug 11 17:24:47 2010 clinet1/ PUSH: Received control message:
    Wed Aug 11 17:24:47 2010 clinet1/ SENT CONTROL [clinet1]: ‘PUSH_R
    EPLY,redirect-gateway def1,route,topology net30,ping 10,ping-
    restart 60,ifconfig′ (status=1)

    routes on client side:

    > netstat -rn
    Routing tables

    Destination Gateway Flags Refs Use Netif Expire
    0/1 UGSc 1 0 tun0
    default UGSc 4 11 en1
    10.0.13/24 UGSc 0 0 tun0 UH 5 0 tun0 UGSc 1 0 en1

  14. there’s indeed something wrong

    here are my routes when going through openvpn :

    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface UGH 0 0 0 eth0 UH 0 0 0 tun0 U 1 0 0 eth0 UG 0 0 0 tun0 UG 0 0 0 tun0 UG 0 0 0 tun0 UG 0 0 0 eth0 is my LAN gateway is the OpenVPN server is the tunnel PtP IP

    This is my route when not using OpenVPN :

    Kernel IP routing table
    Destination Gateway Genmask Flags MSS Window irtt Iface U 0 0 0 eth0 UG 0 0 0 eth0

    So yes, you’re definitely missing some routes needed for this to work.
    What kind of distribution are you using ? Which version of OpenVPN ?

  15. Hi, i am using MacOSX 10.6 and “OpenVPN 2.1.1 i386-apple-darwin10.4.0 [SSL] [LZO2] built on Aug 6 2010″ on the client side and “OpenVPN 2.1_rc11 x86_64-pc-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11]” on the server side.

    Are you using a Linux distro?


  16. I’m using Viscosity under Mac OS X, it comes with OpenVPN 2.1 and works perfectly. On the server side, OpenVPN 2.1 rc11. Viscosity is not free but there’s a trial period, you should probably give it a try..

    Here’s my current server config :

    port 21523
    proto udp
    dev-type tun
    dev vpn-gw-udp
    ca ca.crt
    cert server.crt
    key server.key
    dh dh1024.pem
    push “dhcp-option DOMAIN”
    push “dhcp-option DNS″
    push “redirect-gateway def1″
    keepalive 10 120
    user nobody
    group nogroup
    log /var/log/openvpn.log
    verb 3
    chroot /tmp

    My server NAT table :

    # iptables -t nat -L -nv
    Chain PREROUTING (policy ACCEPT 164K packets, 14M bytes)
    pkts bytes target prot opt in out source destination

    Chain POSTROUTING (policy ACCEPT 6084K packets, 415M bytes)
    pkts bytes target prot opt in out source destination
    16 1054 MASQUERADE all — * eth0 !

    Chain OUTPUT (policy ACCEPT 6084K packets, 415M bytes)
    pkts bytes target prot opt in out source destination

    Client config :

    dev tun
    proto udp
    port 21523
    ca ./conf/ca.crt
    cert ./conf/client.crt
    key ./conf/client.key
    ns-cert-type server
    verb 5
    up ./conf/
    down ./conf/
    ping 60
    ping-restart 120
    script-security 3

  17. Seb, all I can say is a big thank you for being so persistent in assisting those of us who weren’t as fortunate as you to get it working with no fuss! :)

    I am using openvpn 2.2 on a linux debian server and openvpn 2.2 on OSX as a client and was trying to first of all use the ‘redirect-gateway def1′ directive and then various other means (manually adding routes especially) to try to get it working. I was getting the ‘no route to host’ error when I used redirect-gateway, and then ‘no space left in buffer’ on the client side at other times, suggesting to me that there was possibly a routing problem (some sort of loop perhaps).

    Not being at all familiar with routing past the basic concepts I found it hard to know which routes to add to make it work.

    However, the posting of your ‘before and after’ routes allowed me to reverse engineer it for my own situation and write a script to add the routes manually. Now it’s working!

    Thanks so much. It had caused me many a long night up trying to work this out.

    No idea why using the openvpn redirect-gateway didn’t work for me, but it definitely didn’t create the necessary routes in my situation.

    • Hi Jason,

      Glad it finally worked out ! Hope this will help others in figuring out their issues.

      Which version of Debian do you use on the server side ? I’m on Debian Lenny (current stable), OpenVPN 2.1~rc11-1. This version pushes the routes fine.

      On the client side, I’ve used various version of OpenVPN 2.1 on Ubuntu 9.x and 10.x, and Viscosity on Mac OS X.

      Viscosity has this version built-in :

      $ ./openvpn –version
      OpenVPN 2.0.9 i386-apple-darwin9.0.0 [SSL] [LZO] built on Oct 29 2007
      Developed by James Yonan
      Copyright (C) 2002-2005 OpenVPN Solutions LLC

      Did the log say anything about route addition while connecting ? Or maybe failure to use the route command for some reason ?

      Anyway, glad you found the issues, seems like it’s always a DNS problem, or more commonly a route problem.


  18. Hi,
    I have a tp-wr1043nd with dd-wrt and a openvpn conected with a vpn server, my firewall rule is:
    iptables -A POSTROUTING -t nat -o tun0 -j MASQUERADE
    at momento every ip is behind the vpn.
    How can I set for example from with openvpn and without ?

    Best regards

  19. Hi
    Thanks for the interest, now I’ m at work so I have a little problem to give you other information.
    wich kind of information do you need ifconfig, route -n, or….?
    I will as soon as possible.

    Best regards

  20. Dear Wains

    i have the following scenario,
    openvpn server: eth0: (internet), eth1: /24 (private), VPN subnet:
    i created an openvpn server so i can access all my devices on the private LAN from anywhere on the internet.
    the VPN connection is working well, however i am not able to access any device on the private subnet unless i add
    a route in the device as following:
    ip route 255.555.255.0
    question 1: what is the rule i should put in iptables in order to NAT the VPN subnet to, like this i avoid each time adding a route to each device i wanna access.
    question2: what is the rule i should put in iptables so i route all my internet traffic via the vpn tunnel and get assigned the source ip: to the outside world ( i already added for this case “redirect-gateway” in the client config file on my PC).
    thx in advance

    • 1) you don’t have to NAT, just use a route statement to your LAN in your client vpn config

      2) iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE

  21. Sorry if that’s a wrong place I’m writting under, but after few hours googling couldn’t find better place with lots of info about route traffic for vpn tunnels.

    This is how my network looks like:
    My home router/gateway IP –
    My home server IP –
    My home laptop IP –
    My home public IP –

    My work router/gateway IP –
    My work laptop IP –
    My work public IP –

    My home server is a VPN server with IP address
    My home laptop is a VPN client with IP address
    My work laptop is a VPN client with IP address

    I use OpenVPN with client to client option and everything is working fine, all clients are connected to my home public IP address.

    Here is my question:
    Is there any way that I could route all traffic from my home laptop through my work laptop ? To be visible in the world as IP instead of ?

    PS. Please don’t tell that it’s not possible because there’s no way I can have IP address ;)

  22. How about this, can you figure out the server config for this client config?
    dev tun
    route-method exe
    route-delay 2
    lport %local2%
    mssfix 1400
    verb 1
    reneg-sec 0
    inactive 1200 50000
    remote random
    remote %rmte1%
    remote %rmte2%
    hand-window 120
    auth-retry nointeract
    script-security 2 system
    dhcp-option DISABLE-NBT

    blah blah

Comments are closed.