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

configuring host interface networking for virtualbox on ubuntu

Host interface networking is a network option in virtualbox that allows your VM to share the host network card. With this option the VM can have its own routeable IP address on your network. It is unfortunate that this setup is not as simple as choosing this option and clicking OK.

To use host interface networking for your virtualbox VM, you must first create a bridge to your physical network interface.

Follow these steps to create a bridge:

1. install bridge-utils, uml-utilities package
% sudo apt-get install bridge-utils uml-utilities

2. put your NIC (e.g. eth0) in promiscuous mode
% sudo ifconfig eth0 0.0.0.0 promisc

3. create the bridge br0
% sudo brctl addbr br0

4. attach bridge (e.g. br0) to NIC (e.g. eth0)
% sudo brctl addif br0 eth0

5. configure IP address for your bridge (e.g. br0)
% sudo ifconfig br0 192.168.1.2
or if you're using dhcp
% sudo dhclient br0

6. skip this step if you're using dhcp. configure the default gateway (e.g. 192.168.1.1) for your bridge (e.g. br0)
% sudo route add default gw 192.168.1.1 br0

7. create virtual NIC (e.g. veth0)
% sudo tunctl -t veth0 -u root

8. attach the bridge (e.g. br0) to your virtual NIC (veth0)
% sudo brctl addif br0 veth0

That's it! Now your VM can use veth0 as its network interface. In the settings|network settings of your VM, choose "Host Interface" in the Attached to drop down box. Then use veth0 as the host interface name. If your network has a DHCP server, it should be broadcasting DHCP request to veth0 otherwise you will have to configure the network manually on your VM.