Skip to contentSkip to conversation

Enable Wireguard only for specific user

DISCUSSION Florent B. 2 participants 3 messages Sep 30, 2021

lore
Share & tools
lore

Florent B.

author
#0 lore raw

Hi list,

On a Linux system (Ubuntu), I would like to enable Wireguard VPN only for a single user on my system.

Currently by default, every packet of every user is getting though Wireguard.

How can I do to route packets only for 1 user ?

I think I have to play with packets marking (wg show wg0 fwmark / iptables owner/mark module), I tried some commands but was unable to success.

Can someone help please ?

Thank you.

Florent

#1 lore raw

Hi,

What I've had success with is using policy-based routing, selecting the routing table to use based on UID's. You create a new routing table for the WireGuard VPN and add a rule directing that users traffic to that table.

Using commands you can do this to route traffic from the user with UID 1000 to table 500.

Quoted 1 line

ip rule add pref 20 uidrange 1000-1000 lookup 500

You can also do this in systemd-networkd if you're using that. Not sure about other network managers.

Best Regards
Nils

Quoted 19 lines · Florent B.

Hi list,

On a Linux system (Ubuntu), I would like to enable Wireguard VPN only for a single user on my system.

Currently by default, every packet of every user is getting though Wireguard.

How can I do to route packets only for 1 user ?

I think I have to play with packets marking (wg show wg0 fwmark / iptables owner/mark module), I tried some commands but was unable to success.

Can someone help please ?

Thank you.

Florent

author
#2 lore raw

Hi,

Thank you it works very well matching my excluded user and using "lookup main" ;)

Quoted 1 line

ip rule add pref 20 uidrange 1001-1001 lookup main

I though it was more difficult and didn't know that "uid matching" in "ip rule".

Thanks

Le 30/09/2021 à 12:53, Nils Andreas Svee a écrit :

Quoted 32 lines

Hi,

What I've had success with is using policy-based routing, selecting the routing table to use based on UID's. You create a new routing table for the WireGuard VPN and add a rule directing that users traffic to that table.

Using commands you can do this to route traffic from the user with UID 1000 to table 500.

Quoted 1 line

ip rule add pref 20 uidrange 1000-1000 lookup 500

You can also do this in systemd-networkd if you're using that. Not sure about other network managers.

Best Regards
Nils

Quoted 19 lines · Florent B.

Hi list,

On a Linux system (Ubuntu), I would like to enable Wireguard VPN only for a single user on my system.

Currently by default, every packet of every user is getting though Wireguard.

How can I do to route packets only for 1 user ?

I think I have to play with packets marking (wg show wg0 fwmark / iptables owner/mark module), I tried some commands but was unable to success.

Can someone help please ?

Thank you.

Florent