OpenSSH 4.3 VPN Example  

ssh-vpn-diagram.png
OpenSSH 4.3 comes with TUN/TAP support. This means that you can establish an encrypted virtual tunnel between two computers. This tunnel can be used to establish a VPN between these two networks. In the sample network you can establish an SSH connection to 55.56.57.58 but not the other two machines because they're firewalled off. Using an SSH VPN tunnel you can gain access to that entire network (anything that 55.56.57.58 would have access to). To clarify this is not SSH port forwarding. This is full IP forwarding using a tunnel interface.

This is done by creating a tunnel between your home PC (1.2.3.4) and the network gateway PC (55.56.57.58). This is done with the -w command in SSH.

Code:

ssh -w0:0 55.56.57.58

This creates a tun0 interface on both ends of the SSH session. Once the tunnel is established you will need to put an IP on both sides of the tunnel using the following commands.

Note: the PermitTunnel option must be turned on in your sshd_config file for this to work.

Code:

# IP Address for your Home PC
ifconfig tun0 10.0.2.1 netmask 255.255.255.252

Code:

# IP Address for the network gateway PC
ifconfig tun0 10.0.2.2 netmask 255.255.255.252

At this point you should be able to ping both sides of the tunnel from both machines. Now a little Linux routing knowledge comes in handy. You'll need two route statements to do this. One to force access to the network gateway PC to go out eth0 (or whatever your output device is), and the other to tell it to use tun0 for access to the rest of that subnet.

Code:

route add -host 55.56.57.58 dev eth0
route add -net 55.56.57.58/24 dev tun0

Everything will route properly now, but the firewalled machines will not know how to get back to your home PC. A little NAT will fix that right up. You'll need to setup IP Forwarding and NAT on the network gateway PC to masquerade all requests from your home PC.

Code:

echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Leave A Reply - 4 Replies
Replies
jimcooncat 2007-02-19 04:57am - No Email - Logged IP: 72.73.108.230
Thank you for this article! I've been trying to figure this out from:
http://gentoo-wiki.com/HOWTO_VPN_over_SSH_and_tun
dirtbag 2008-03-25 06:56pm - No Email - Logged IP: 71.70.174.94
So, this is all fine and dandy, but how would you purpose to automate it? i.e. im trying to set up a lan-to-lan tunnel with this, something like
192.168.1.0/24---(router)-=-=-=-=innernets=-=-=-=-=(router)----192.168.2.0/24
and I have 2 linux boxes on each end where I wanna do openssh vpn between them and route like so
route add -net 192.168.1.0/24 dev tun1 (on one side)
route add -net 192.168.2.0/24 dev tun1 (on the other side)

but how to automate this so that it can be kicked off from just one of the servers.

-Dirtbag

Scott Baker 2008-03-25 07:55pm - No Email - Logged IP: 65.182.224.60
The easiest thing to do would be to setup OpenVPN. SSH VPNs are good, ONLY if it's your last option. They're a pain otherwise.
jyg 2008-07-21 03:29pm - No Email - Logged IP: 67.127.54.94
Actually this is FAR simpler than setting up OpenVPN. If you need a quick and dirty tunnel between networks, this is the simplest and quickest way to go.
All content licensed under the Creative Commons License