Prevent SSH connection lost after logging into VPN on server machine

I encountered an issue that I can't deal with. When I'm logged onto an VPS over SSH and try to estabilish VPN connection on that VPS, the SSH connection between VPS and my machine get lost. I assume that's because routing got changed by VPN settings. How to prevent that?


Solution 1:

Let's consider following scenario:

  1. your VPS has a single ethernet interface, configured with IP address 4.3.2.1/24;
  2. your VPS can access the Internet via a default-gateway 4.3.2.254
  3. your VPS has not yet activated any OpenVPN connection; hence there are no tun interface active

In such a scenario, from your machine (let's suppose your machine is 9.8.7.6/24 with def-gw 9.8.7.254) you can successfully establish an SSH connection to 4.3.2.1. Hence both hosts 4.3.2.1 and 9.8.7.6 can succesfully reach each other.

Now, with such an SSH connection established, let's suppose:

  1. you launch an OpenVPN connection from your VPS 4.3.2.1;
  2. as such, a new tun0 interface will be dinamically configured (let's suppose it will be assigned a 10.10.10.2 IP, with a 10.10.10.1 PTP).

At this stage:

  • IF no route will be pushed from remote OpenVPN server to your local VPS, then nothing will change in term of routing, and your SSH connection will survive with no problems at all. In this case, the only traffic traversing the VPN is the one directed towards the remote OpenVPN Server (10.10.10.1);

  • IF remote OpenVPN server will push back some route, and expecially if VPS default-gateway will be replaced with 10.10.10.1 (remote OpenVPN endpoint), THEN you're having problems. In this case you're tunneling ALL the outgoing IP traffic (with the exception of OpenVPN itself) within the VPN.

In this second case (replacing def-gw right after establishing VPN connection), your previous SSH connection will "hang", due to asymmetric routing:

  • Traffic from your machine (9.8.7.6) to VPS (4.3.2.1) will flow trough the previous, never changed, path;
  • Traffic from VPS (4.3.2.1) to your machine (9.8.7.6):
    • without the VPN (hence, initially) was routed through the 4.3.2.254 gateway;
    • after the establishment of the VPN link, with related def-gw replacement, is routed through the VPN (10.10.10.1).

In other words: as soon as the VPN link is established, your return route from VPS to your machine is going to change and... this is not a good thing (several network devices, along the return-path, might recognize such asymmetric path and simply drop packets).

Furthermore, chances are high that your remote OpenVPN server is acting as a NAT-box: all the traffic coming from the VPN will be NATted with the public IP-Address of the remote OpenVPN Server. If this is true, than things are no more... "not good", but definitely "bad", as for your SSH connection: return traffic, in addition to get back along a different route, is coming back to your machine with a different source IP (the one of the public interface of the VPN server).

How to solve this problem?

Quite easily, indeed.

Simply instructing your VPS server to not route traffic to your machine along the VPN, but, instead, relying on previous route. It should be as easy as adding, before starting OpenVPN:

     route add -host 9.8.7.6 gw 4.3.2.254

where:

  • 9.8.7.6 is your machine public IP address
  • 4.3.2.254 is the original default gateway of your VPS.

P.S.: by providing a much more detailed question, you would have gotten a much quicker answer :-)

Solution 2:

You need to add route-nopull option (and remove redirect-gateway if it exists) to your OpenVPN client's configuration file on your VPS.

That way connecting to a VPN server won't modify any routes on your VPS, so you would be able to set those you need by yourself.