Skip to content

Instantly share code, notes, and snippets.

@XavM
Last active March 29, 2021 07:43
Show Gist options
  • Save XavM/2a5435e619b33a5021a6396394878945 to your computer and use it in GitHub Desktop.
Save XavM/2a5435e619b33a5021a6396394878945 to your computer and use it in GitHub Desktop.
Poor man's VPN using SSH (tcp in tcp is not a good idea, but ... can be handy)

Poor man's VPN using SSH (tcp in tcp is not a good idea, but ... can be handy)

Client :

WiFi connected using wlan0 on address range 172.20.10.0/28

sudo su 
apk install autossh
modprobe tun
sysctl -w net.ipv4.ip_forward=1
# ip tuntap add dev tun0 mode tun user root
# The ssh conn will create the Tun interfaces for both server and client
autossh ${SERVER_PUBLIC_IP} \
  -M 0 \
  -o "ServerAliveInterval 5" \
  -o "ServerAliveCountMax 3" \
  -o TCPKeepAlive=yes \
  -NTCfw 0:0
ip addr add 10.0.0.1/32 peer 10.0.0.2 dev tun0
ip link set tun0 up
ip route add 192.168.1.0/24 via 10.0.0.2
iptables -t nat -A POSTROUTING -s 10.0.0.2 -o wlan0 -j MASQUERADE

Server :

Eth connected using eth0 on address range 192.168.1.0/24 + NAT on the ISP Box forwarding ${SERVER_PUBLIC_IP}:22 to ${SERVER_PRIVATE_IP}:22

sudo su
modprobe tun
sysctl -w net.ipv4.ip_forward=1
# ip tuntap add dev tun0 mode tun user root
# The ssh client conn will create the Tun interfaces for both server and client
ip addr add 10.0.0.2/32 peer 10.0.0.1 dev tun0 
ip link set tun0 up
ip route add 172.20.10.0/28 via 10.0.0.1
iptables -t nat -A POSTROUTING -s 10.0.0.1 -o eth0 -j MASQUERADE

sshd must be configured with “PermitTunnel yes” (/etc/ssh/sshd_config)

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