VPN tethering on root android through USB and wifi hotspot¶
I wanted to tether my rooted S5 android to my ASUS N66U router so I could use my mobile data connection through my router over a VPN.
Getting this working with a VPN for both USB and wifi was a little more tricky than I thought, but I found the following two solutions in the end.
Plug your phone into your router via USB and enable tethering on your android phone.
Your router screen should go through the following steps, this may take a minute.
Once you’re connected and have an IP, you should be able to access the internet by connecting to your router, all your traffic is being sent over the default network. Next, connect to your VPN, I’m using OpenVPN.
Create a new file with the following contents and save it to your android device.
# Copy into /data/local/userinit.sh iptables -t filter -F FORWARD iptables -t nat -F POSTROUTING iptables -t filter -I FORWARD -j ACCEPT iptables -t nat -I POSTROUTING -j MASQUERADE ip rule add from 192.168.43.0/24 lookup 61 ip rule add from 192.168.42.0/24 lookup 61 ip route add default dev tun0 scope link table 61 ip route add 192.168.43.0/24 dev wlan0 scope link table 61 ip route add 192.168.42.0/24 dev wlan0 scope link table 61 ip route add broadcast 255.255.255.255 dev wlan0 scope link table 61 ip route add 172.27.232.0/24 dev tun0 table 61
We need to run this script as the root user.
I use root browser from the playstore to run my script via a GUI.
Check your IP, all your traffic should now be sent over your VPN.
The order you perform these steps in matters. If you’re already connected to your VPN when you enable tethering, you’ll have to disconnect and then reconnect so your router can be setup with regular tethering (before applying the VPN tunnel).
The process that worked for me is as follows:
- Disconnect from all VPN networks
- Plug in USB
- Enable tethering on your device
- Wait for the router DHCP to give an IP
- Now we have an IP, connect to your VPN
- Run the script as root to tunnel all traffic to your VPN
This works for USB, but for mobile hotspot / wifi tethering, you can use the next method below.