Wednesday, February 25, 2009

accessing a VPN on ubuntu Linux via ssh port forwarding

If you need to access a VPN on Linux but the VPN software is only available for Windows, use ssh port forwarding to a Windows VM hosted on Virtual Box.

Here's how to do it:

1. Follow my previous post on
how to configure host interface networking for virtualbox

2. Install OpenSSH for windows either using cygwin. If you don't want to install the complete cygwin environment, but just openssh server I recommend installing openssh for windows

3. Make sure you can connect to the VPN on your virtualbox windows VM.

4. Setup port forwarding to the windows VM connected to the VPN. On your linux box:
% ssh -C -L localPort:vpnHost:remotePort windowsVM
localPort is the port on your linux machine
vpnHost:remotePort is the host and port on the VPN you want to forward traffic to

Let's look at step 4 more closely because it can be somewhat confusing at first. The -C switch tells ssh to compress data as it sends it. The -L switch tells ssh to setup port forwarding. SSH will connect the windowsVM, compress and forward all local traffic on your linux machine destined for localPort to the vpnHost:remotePort. Note that vpnHost can be any host accessible to windowsVM

To give a more concrete example suppose you want to access a webserver (port 80) on a VPN IP/host (10.10.74.164). On your linux box, you cannot open http://10.10.74.164:80, but your virtualbox windowsVM (192.168.1.10) can because it is running the VPN software. If you want to access http://10.10.74.164:80 from your Linux box, you setup ssh port forwarding as follows:

% ssh -C -L 8080:10.10.74.164:80 192.168.1.10

The above command will port all traffic to my local port 8080 to VPN host 10.10.74.164 port 80 via my windowsVM. On your linux box, you can access http://10.10.74.164:80
by http://localhost:8080. NOTE: if you want to forward local port 80 to remote port, you must be ssh as root because on Linux/Unix, only root can access ports less than 1024

No comments: