I solved this problem for myself yesterday and couldn’t find the answer anywhere online (clearly stated), so here’s what I did.

This also works for Plex, games, etc it’s policy based routing so you can have your VPN and remote access at the same time.

This configuration is for your ssh server (ie your home PC) that has a VPN running like ProtonVPN etc and allows you to connect back to your home PC remotely either for SSH, plex, anything you’re serving.

This is changing the default gateway for particular ports, so that return traffic from connection attempts doesn’t go back out through the VPN.

I used this page to figure out how to do this:

http://linux-ip.net/html/tools-ip-rule.html

Add a default route to some table, arbitrarily table 10 but you can use whatever number (RTFM), also obviously needs to be your router IP

sudo ip route add default via 192.168.19.1 table 10

Add a rule for the return traffic from your ssh host

sudo ip rule add sport 22 table 10

sudo ip route flush cache

check out your fancy new rule

ip rule show

And to make this persistent across reboots, add to:

/etc/systemd/network/20-wired.network

(or whatever your file there is, add the following to the bottom. This is the same as above, just permanent)

[Route]
Gateway=192.168.19.1
Table=10

[RoutingPolicyRule]
SourcePort=22
Table=10
  • ∟⊔⊤∦∣≶OP
    link
    fedilink
    3
    edit-2
    1 year ago

    Ahead of you with the non-default port, I just didn’t use it in the example.

    But for the destination IP, I won’t always be connecting remotely from the same one. Or am I misunderstanding what you mean?

    • @[email protected]
      link
      fedilink
      11 year ago

      Aha, I think I misunderstood your situation then? I assumed you’re running these routing rules on your client machine, so you’re able to access your ssh server without it going over the VPN – not that your server is running a VPN active that blocks external connections…?

      But if I didn’t misunderstand, I’d mean the (assumingly static) ssh server’s IP.

      • ∟⊔⊤∦∣≶OP
        link
        fedilink
        11 year ago

        No it’s the second one; server (home pc) is running ProtonVPN and there is a default route that makes all (ie ssh replies) traffic go through the VPN.

        I added some clarification, but basically this sets up a port-based default route to the home router instead of the VPN, so that SSH etc works.