Route all traffic by OpenVPN

Posted by erwin on Tuesday, December 11, 2018

In October i posted a blog about setting up your OpenVPN server in 2 minutes.

This blog is a addon to your existing configuration to route all traffic over the VPN. 

Change server configuration

go to the config file (/etc/openvpn/server.conf) and add the following lines:

    push "redirect-gateway def1"
    push "dhcp-option DNS "
    push "dhcp-option DNS"

Restart your OpenVPN daemon

    sudo /etc/init.d/openvpn restart

Change client configuration

Change your client config, and add the following line

    redirect-gateway def1

Change IP Tables

Last thing to do is change your iptables to NAT traffic to the internet. (make sure you have the right to do this or run this as root)

    iptables -I FORWARD -i tun0 -o wlan0 \
            -s -m conntrack --ctstate NEW -j ACCEPT
    iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED \
            -j ACCEPT
    iptables -t nat -I POSTROUTING -o wlan0 \
            -s -j MASQUERADE


  • tun0: your virtual VPN network interface
  • eth0: your normal network interface (to make sure you will use the right interface, check with “ifconfig”)
  • your VPN network IP range

comments powered by Disqus