Saeed
2 min readFeb 9, 2023

Socks proxy as VPN in Linux

You probably know that you can use your socks proxy as VPN via tun2socks
The challenging part of tun2socks in Linux is the system routing
Especially if your socks proxy is running on the same machine
I tried many ways with no luck, but finally I found the solution and I like to share it with you 😊

Install tun2socks on your machine:
https://github.com/xjasonlyu/tun2socks

Find the default network interface name:
ip route | grep default
You should see somethings like:
default via {gateway} dev {interface} ...
Here is mine: gateway = 192.168.1.1, interface = enp45s0
Maybe for you is something else, so check these first
Then you should setup the tun device:

sudo ip tuntap add mode tun dev tun0
sudo ip addr add 10.10.10.10/24 dev tun0
sudo ip link set dev tun0 up

Add new default route to the tun device:

sudo ip route add default dev tun0 metric 1

Here we have a small problem:
We are passing all traffic to the tun device, including the proxy server itself
So we have to exclude it:

sudo ip route add {proxy_server} via {gateway} dev {interface}

Run your socks proxy
Finally run tun2socks:

sudo tun2socks -device tun://tun0 -proxy socks5://127.0.0.1:{port} -interface {interface} -tcp-auto-tuning

That’s it, now you don’t have to set proxy for every app
(Note: you should also tunnel DNS requests through the proxy, read “DNS resolution with socks proxy” for more information)
I created a script that you can easily manage your system routing
First edit variables based on your environment
Then run it: sudo bash socks2vpn.sh
Enjoy 😉

socks2vpn.sh

Update:
I developed socks2vpn for windows too!
You can check it here: socks2vpn.bat