In one of my many projects, I needed to access a network that only had an SSH server. I knew that SSH tools were capable of providing a VPN tunnel, but I'd never tried it. It's quite easy to implement and can come in handy in many cases.
So I'm going to show you step-by-step in this article how to set up a temporary VPN with OpenSSH.
PermitRootLogin yes
PermitTunnel yes
root@host:~# systemctl restart sshd
We need a masquerade rule to allow the client to access the remote network. We'll look at how to do this using iptables or nftables.
root@host:~# echo 1 | tee /proc/sys/net/ipv4/ip_forward
root@host:~# iptables -t nat -A POSTROUTING -d 192.168.1.0/24 -o eth0 -j MASQUERADE
root@host:~# nft add table ip NAT
root@host:~# nft add chain ip NAT my_masquerade '{ type nat hook postrouting priority 100; }'
root@host:~# nft add rule NAT my_masquerade ip daddr { 192.168.1.0/24 } oifname eth0 counter masquerade
Now that we've finished pre-configuring our Debian server, we need to start the SSH tunnel from the client.
root@host:~# ssh -Nf -w 0:0 -p 22 root@1.1.1.1
root@host:~# ip addr add 10.110.0.200/32 peer 10.110.0.100 dev tun0
root@host:~# ip link set tun0 up
root@host:~# ip route add 192.168.1.0/24 via 10.110.0.100
With the previous command (ssh -Nf -w 0:0) run from the client, the tun0 virtual device should appear on the Debian server. The last step is to activate and configure it on the Debian side.
root@host:~# ip addr add 10.110.0.100/32 peer 10.110.0.200 dev tun0
root@host:~# ip link set tun0 up
That's it! The 192.168.1.0/24 network should now be reachable from the client.
Contact :