this post was submitted on 14 Nov 2023
89 points (100.0% liked)

Linux

48062 readers
694 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

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
all 23 comments
sorted by: hot top controversial new old
[–] TCB13@lemmy.world 23 points 1 year ago (1 children)

Finally someone who learnt how to use systemd.

[–] luthis@lemmy.nz 11 points 1 year ago (3 children)

~~learnt~~

learning... it's extensive!

[–] TCB13@lemmy.world 6 points 11 months ago (1 children)
[–] luthis@lemmy.nz 2 points 11 months ago

Woooah awesome thanks!

There goes my whole day again on systemd...

[–] skullgiver@popplesburger.hilciferous.nl 1 points 11 months ago* (last edited 11 months ago) (2 children)

[This comment has been deleted by an automated system]

[–] luthis@lemmy.nz 5 points 11 months ago

Yes but.. I am learnING??

[–] CmdrShepard@lemmy.one 0 points 11 months ago

This (and "tyre") is why we won the war.

[–] EinfachUnersetzlich@lemm.ee 0 points 11 months ago

We really need a ConfidentlyIncorrect community on Lemmy.

[–] kungen@feddit.nu 11 points 1 year ago (1 children)

If you're looking for more improvements, I'd recommend using a non-default SSH port and to include the destination IP in the rules.

[–] luthis@lemmy.nz 3 points 1 year ago* (last edited 1 year ago) (1 children)

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?

[–] kungen@feddit.nu 1 points 11 months ago (1 children)

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.

[–] luthis@lemmy.nz 1 points 11 months 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.

[–] Mikelius@lemmy.ml 10 points 11 months ago (2 children)

Out of curiosity, why not just leave ssh access to the local network so you can only reach it by VPN in the first place? Note I might be misunderstanding what the goal of this was, so feel free to lmk if I'm off the field with my question lol

[–] luthis@lemmy.nz 2 points 11 months ago (2 children)

I have protonVPN, therefore no way to forward packets from their endpoint to my pc.

[–] Mikelius@lemmy.ml 2 points 11 months ago

Oh gotcha, I misunderstood this post as talking about a self hosted VPN, not external provider. That explains it! :D

[–] WeAreAllOne@lemm.ee 1 points 11 months ago (1 children)

Proton has port forwarding anyhow..

[–] luthis@lemmy.nz 1 points 11 months ago* (last edited 11 months ago)

I was surprised to learn this was a thing, impressive, however;

'the VPN app sends a request to the VPN server to open a random port'

'the active port number will change when you disconnect and reconnect the VPN.'

This will not work OOTB with Plex for example, you would need to change the port in the app every time. It becomes difficult to serve anything statically, like a XMPP server or anything that doesn't let you configure the port.

You also would need to be at home to check which port you've been assigned eg if the connection drops and you get assigned a new port, defeating the whole 'remote access' thing.

[–] oldGregg@lemm.ee 6 points 1 year ago (1 children)

Goddamn I didn't even think of that, I spent days on an alternate solution to a problem this would've solved.

[–] luthis@lemmy.nz 4 points 11 months ago

I spent most of the day intensely learning nftables and ulogd thinking that was the way. Nope, there was a simple way all along.

[–] Illecors@lemmy.cafe 5 points 1 year ago
[–] WhyAUsername_1@lemmy.world 2 points 11 months ago (1 children)

What are you trying to achieve? I am sorry I don't understand the end goal

[–] luthis@lemmy.nz 4 points 11 months ago

Remote access while ProtonVPN is active.