Connecting a Raspberry Pi to Ubuntu when Using a VPN and Sharing the Internet Connection
This is not a standalone post - you should read an earlier post to understand how to connect a Raspberry Pi to Ubuntu using a direct Ethernet cable, and how to share the internet connection from Ubuntu to the Raspberry Pi.
The VPN Problem
There are some generalizations here, and you may need to adjust the steps for your specific VPN.
VPNs usually route all traffic through the VPN. This means the Raspberry Pi may not be assigned a DHCP address and may not be able to access the internet via that Ubuntu machine.
The DHCP Problem
There are a couple of choices here: you can assign a static IP address to the Raspberry Pi, but that means you need a monitor and keyboard, and my goal is to avoid using them.
Instead, go to your VPN and add ports 67 and 68 on UDP to your allowlist. This will let the Raspberry Pi get a DHCP address from the Ubuntu machine.
If your Pi is connected, pull the Ethernet cable and reconnect it, and it should get a DHCP address.
Then use the following command to check the address assigned to the Raspberry Pi -
sudo cat /var/lib/NetworkManager/dnsmasq-*.leasesYou still may not be able to access the internet, though.
The Internet Problem
Again, this is going to depend on your VPN, but I needed to allow forwarding on the VPN interface.
Because I don’t often connect a Pi directly to my Ubuntu machine, I am happy to do it only when I need to.
You can turn on forwarding with the following on all network interfaces using -
sudo sysctl -w net.ipv4.ip_forward=1If you want to turn it on only for the VPN interface, you need to know the name of the interface, which you can find with -
ip addrThe name might be something like tun0. If you don’t know the name, you can turn off the VPN, run the above command again, and see which interface disappears. Now you have the name of the VPN interface.
Then you can turn on forwarding for that interface with -
sysctl net.ipv4.conf.tun0.forwarding=1This command does not survive reboots, you will need to run in again the next time you need forwarding.