Perhaps you are behind a corporate firewall which does not allow you to access certain types of resources on the Internet. Or, perhaps you are accessing the Internet over an open wifi where you do not trust your network traffic to your fellow wifi users or the admins running the local network.
These instructions guide you in setting up an OpenVPN server on an EC2 instance, sending all your network traffic through a secure channel to port 80 on the EC2 instance and from there out to the Internet.
EC2 Instance
Run the latest Ubuntu 9.10 Karmic image. You can find the most current AMI id in a table on http://alestic.com
ec2-run-instances --key <KEYPAIR> ami-1515f67c
Make a note of the instance id (e.g., i-6fceba06). Watch the status using a command like this (replace with your own instance id):
ec2-describe-instances <INSTANCEID>
Repeat the describe instances command until it shows that the instance is “running” and make a note of the external hostname (e.g., ec2-75-101-179-94.compute-1.amazonaws.com).
Connect to the instance using the external hostname you noted.
remotehost=<HOSTNAME>
ssh -i <KEYPAIR>.pem ubuntu@$remotehost
OpenVPN Server
Upgrade the EC2 instance and install the necessary OpenVPN software:
sudo apt-get update &&
sudo apt-get upgrade -y &&
sudo apt-get install -y openvpn
sudo modprobe iptable_nat
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
sudo iptables -t nat -A POSTROUTING -s 10.4.0.1/24 -o eth0 -j MASQUERADE
Generate a secret key for secure communication with OpenVPN:
sudo openvpn --genkey --secret ovpn.key
Start the OpenVPN server on the EC2 instance. We are (ab)using port 80 because most closed networks will allow traffic to this port ;-)
sudo openvpn --proto tcp-server --port 80 --dev tun1 --secret ovpn.key --ifconfig 10.4.0.1 10.4.0.2 --daemon
OpenVPN Client
Back on the local (non-EC2) workstation, set up the software:
sudo apt-get install -y openvpn
sudo modprobe tun # if necessary
sudo iptables -I OUTPUT -o tun+ -j ACCEPT
sudo iptables -I INPUT -i tun+ -j ACCEPT
Download the secret key from the EC2 instance:
ssh -i <KEYPAIR>.pem ubuntu@$remotehost 'sudo cat ovpn.key' > ovpn.key
chmod 600 ovpn.key
Start the OpenVPN client:
sudo openvpn --proto tcp-client --remote $remotehost --port 80 --dev tun1 --secret ovpn.key --redirect-gateway def1 --ifconfig 10.4.0.2 10.4.0.1 --daemon
Edit /etc/resolv.conf and set it so that DNS is resolved by the EC2 name server:
sudo mv /etc/resolv.conf /etc/resolv.conf.save
echo "nameserver 172.16.0.23" | sudo tee /etc/resolv.conf
You should now be able to access any Internet resource securely and without restriction.
Teardown
When you are done with this OpenVPN tunnel, remember to shut down the EC2 instance and restore the DNS configuration:
sudo killall openvpn
ec2-terminate-instances <INSTANCEID>
sudo mv /etc/resolv.conf.save /etc/resolv.conf
If you have ways to improve this approach, please leave a comment.
Disclaimer
These instructions are not intended to assist in illegal activities. If you are breaking the laws or rules of your government or college or company or ISP, then you should understand the security implications of the above steps better than I do and be willing to accept consequences of your actions.
[Update 2009-01-15: Upgrade instructions for Karmic]


Follow Eric Hammond on Twitter
"Ubuntu 9.10 Intrepid" should read "Ubuntu 8.10 Intrepid". Interesting article - thanks.
Mark: Fixed, thanks!
Gotta say: love your work Eric. Keep up sharing the goodness and thanks a lot.
Thanks for the howto. I found this really easy to set up.
I'd like to use nonsymmetric keys and use the server with multiple computers. I'm finding the openvpn documentation hard to get into though. Any pointers to tutorials to make those changes?
wibge: Though I have used openvpn with asymmetric keys, it is indeed a lot more work, which is why I didn't choose that approach for a quick tutorial like this. Here are a couple resources which might help in your quest:
- http://openvpn.net/index.php/open-source/documentation/howto.html
- http://dev.riseup.net/grimoire/networking/openvpn/
Nice article. Could you please also provide a Windows client configuration? I have tried a little myself but cannot get it work. It will help me a lot since tons of websites are blocked here.
mitnickcbc: I've heard about Windows, but I don't have any. You'll need to seek help elsewhere for them.
One server and client, before issuing iptables commands:
sudo iptables-save >/etc/iptables.rules
HTH
Following the guide above, I tried to run openvpn on a custom 10gb esb AMI I created from 'Debian 5.0 lenny Server (ami-b1f38ce3)'.
To qualify for the EC2 free tier, I followed this guide
to convert the 15gb Debian 5.0 lenny Server AMI to a 10GB AIM.
Openvpn couldn't find any tun device when it tried to start.
I checked and there wasn't any tun device loaded so I tried to load the tun by placing 'tun' in '/etc/module' and restarting my instance. The tun device didn't load. I read that ec2 doesn't allow kernel modules to be loaded.
I'm not sure where the source of my problem is.
Weather I chosen a kernel without tun compiled when making the 10gb ami or if tun wasn't compiled in 'ami-b1f38ce3' in the 1st place.
What can I do now? Am I still able to enable a tun device for openvpn to work on my current custom AMI?
techntools: The above instructions worked on Ubuntu. I haven't tried Debian.
Combining the above with the info in the link below
http://www.adamsinfo.com/quick-linux-and-windows-openvpn-howto-and-tutorial-including-vpn-routing/
gave me a solution to VPN out on Ubuntu and MS-Windows 7 clients via an OpenVPN server running on Ubuntu on hosted cloud service.
The key element for me was the routing on MS-Windows.
I quote:
On your windows client, you’ll now need to change your default gateway:
Use route print and find out your current default gateway, then, assuming your current local default gateway is: 192.168.1.1 and server’s IP address is XX.XXX.124.95, issue the following commands:
route DELETE 0.0.0.0
route ADD XX.XXX.124.95 MASK 255.255.255.255 192.168.1.1
route ADD 0.0.0.0 MASK 0.0.0.0 10.4.0.1
The first ADD command is used to tell your client how to access the ‘new default gateway’. Without specifying your real default gateway, the client machine would have no idea how to reach your VPN server.
You can specify 10.4.0.1 as your default gateway, as it is now virtually on the same LAN as your 10.4.0.2 adapter, but without the additional route to XX.XXX.124.95, your connection to the server would have to terminate and you’d lose your tun interface.
Thanks for this guide. Doesn't seem to work with Maverick Meerkat though - after following your instructions and trying to connect to the VPN from the client, I'm getting this error:
Note: Cannot ioctl TUNSETIFF tun1: Operation not permitted (errno=1)
Note: Attempting fallback to kernel 2.2 TUN/TAP interface
Cannot open TUN/TAP dev /dev/tun1: No such file or directory (errno=2)
Exiting
Ubuntu 10.10 is installed on both client and server. Looking at the output of ifconfig, there is indeed no tun device.
mwolff:
Thanks for pointing this out. I've added a task to my todo list to update this article for modern Ubuntu :)
"sudo iptables -t nat -A POSTROUTING -s 10.4.0.1/2 -o eth0 -j MASQUERADE"
slash 2? Are we serious? How many hosts does that cover? Even legal?
pcharlesleddy:
Looks like a typo. Corrected to "/24"