Configuring routes so that vpn is only used for local resources

5,959

I managed to solve it.

First I increased the metric of the default gateway route to 1 so that all my traffic goes trough that.

sudo ifmetric enp4s0 1

After that I added a new route for the 10.x.x.x subnetwork

sudo ip route add 10.0.0.0/8 dev ppp0 metric 25

this now allows me to access all vpn network resources and use my local internet connection for everything else.

I also added a script into /etc/network/if-up.d/ that automatically does this every time I connect to the vpn.

#!/bin/sh

if [ "$IFACE" = "ppp0" ]; then
     ifmetric enp4s0 1
     ip route add 10.0.0.0/8 dev ppp0 metric 25
fi

Where ppp0 is the interface name of my vpn connection. And the routing table in the end looks like this:

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    1      0        0 enp4s0
0.0.0.0         0.0.0.0         0.0.0.0         U     50     0        0 ppp0
10.0.0.0        0.0.0.0         255.0.0.0       U     25     0        0 ppp0
10.150.1.254    0.0.0.0         255.255.255.255 UH    50     0        0 ppp0
169.254.0.0     0.0.0.0         255.255.0.0     U     1      0        0 enp4s0
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 enp4s0
195.230.180.187 192.168.0.1     255.255.255.255 UGH   1      0        0 enp4s0
Share:
5,959

Related videos on Youtube

nixpix
Author by

nixpix

Updated on September 18, 2022

Comments

  • nixpix
    nixpix over 1 year

    After searching far and wide for a comprehensive answer and trying out multiple solutions which didn't work I am turning to the community.

    I am using a VPN connection to access certain network resources (mainly testbeds) and they are all on the 10.0.0.0 network range. However, when I connect to the VPN all of my traffic is routed trough the VPN and not just the requests to those ip addresses. My goal is to route traffic like Youtube, Google Play and so on over my internet and not over the VPN internet. Mainly because the speed is very bad going over the VPN.

    I have tried checking the options in the network manager setting to use the VPN only for the resources on the network but this does not work.

    I have also tried playing around with my routing tables but to no success.

    I would appreciate any help in this regard. I am using Ubuntu 16.04 with Gnome. Here are some network stats:

    netstat -rn
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    0.0.0.0         0.0.0.0         0.0.0.0         U         0 0          0 ppp0
    0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 enp4s0
    10.150.1.254    0.0.0.0         255.255.255.255 UH        0 0          0 ppp0
    169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 enp4s0
    192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 enp4s0
    195.230.180.187 192.168.0.1     255.255.255.255 UGH       0 0          0 enp4s0
    
  • Gewure
    Gewure almost 6 years
    isn't there a UI-way of doing this?
  • Йордан Рамчев
    Йордан Рамчев over 5 years
    Works perfect :)
  • pLumo
    pLumo about 4 years
    Although everything looks great, it works well only after running sudo ip route add 0.0.0.0/1 via 192.168.178.1. How could I add that to the if-up.d script, when the gateway address is always different ?