Skip to content

Instantly share code, notes, and snippets.

@varfrog
Last active November 30, 2023 11:43
Show Gist options
  • Save varfrog/0cdd9a3d975f7337671786ba50e205e5 to your computer and use it in GitHub Desktop.
Save varfrog/0cdd9a3d975f7337671786ba50e205e5 to your computer and use it in GitHub Desktop.
vm-host-os.md

Host configuration

Resolve the guest IP:

From the output of ip addr show on the guest: near enp023 (in my case):

inet 192.168.0.106/24 (...)

hence the guest IP is 192.168.0.106.

On host run: sudo ip route add default via 192.168.0.106

I needed to delete the old default gateway: ip route del default

Make the route persistent

sudo vim /usr/local/bin/set_gateway_route.sh

add

#!/bin/bash

# Remove existing default gateway
ip route del default 2>/dev/null

# Add the new default route
ip route add default via 192.168.0.106 dev enp5s0
sudo chmod +x /usr/local/bin/set_gateway_route.sh

Now host routes through the guest:

➜  ~ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  _gateway (192.168.0.106)  0.573 ms  0.551 ms  0.549 ms
 2  192.168.0.1 (192.168.0.1)  1.021 ms  1.148 ms  1.154 ms
 3  device.lan (192.168.1.254)  2.161 ms  2.173 ms  2.364 ms
(...)

When the VM is up, run the bash script. Check the results with ip route show. Expect line default via 192.168.0.106 where the IP is the guest's IP.

Now host routes through the VPN on the guest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment