0

I send all the traffic through a Wireguard VPN, but I need to setup an exception. Packets directed to my own public IP have to go through the default gateway of the system.

It works as intended if I add this to the kernel routes: route add X.X.X.X gw 192.168.1.254. But, of course, here I'm hardcoding my router's gateway and it won't work if I'm connected to any other network. How can I generalize this behaviour so that I always send packets directed to X.X.X.X through the default gateway? Perhaps using iptables rather than adding kernel routes would be better?

NANI SORE
  • 3
  • 1
  • Your computer cannot guess the gateway's IP on its own: either you set it yourself (static configuration that you did), or you get it dynamically using DHCP, and the default route will be added automatically. – Gohu Aug 25 '20 at 09:35

1 Answers1

0

You can use a variable:

defaultGW=$(/sbin/ip route | awk '/default/ { print $3 }')

then:

route add X.X.X.X gw $defaultGW


Perhaps you should start using ip route, it's the modern alternative to route.

Krackout
  • 1,559
  • 6
  • 17
  • 1
    Thank you, works as intended! Only minor problem is that for some reason networkmanager's wireguard plugin ignores the commands if I use them as PreUp/PostDown directives, whereas wireguard's own cli works like a charm, but I guess it's a problem with nm. I will look into ip route, thanks for the tip! – NANI SORE Aug 25 '20 at 10:46