OpenVPN server on Amazon EC2

I was recently asked to set up a personal VPN for a friend here in Beijing and thought I’d share my process. I went with Amazon’s EC2 service because they have a reasonable choice of DC locations and their “micro” instances are free to trial for one year.

I’m assuming that you already have an AWS account, if not then create one and login to the EC2 console to follow along.

Provision the EC2 instance

Launch a new “micro” instance using the latest Amazon Linux AMI image. Don’t forget to save the PEM keyfile.

Create & assign a new Security Group (I called mine “openvpn” but name isn’t important) and open the following ports: 22 (TCP) and 1194 (TCP & UDP).

Login via SSH using the PEM file you downloaded and run the initial updates:

$ ssh ec2-user@[ec2-hostname] -i path/to/keyfile.pem

$ sudo yum upgrade

Install OpenVPN

Enable the EPEL repository which contains the openvpn package. EPEL is installed by default but not enabled, so edit /etc/yum.repos.d/epel.repo and change the enabled=0 line to enabled=1. It should look like this:

name=Extra Packages for Enterprise Linux 6 - $basearch

Then install as normal via yum:

$ sudo yum install openvpn

Configuring the OpenVPN server

Please note: at this point you can follow any of the usual openvpn guides found online. I opted for a TUN configuration using a pre-shared key for simplicity. This works fine for the purpose of tunneling all network traffic to circumvent the GFW here. Your needs may vary, so please read the openvpn manpage and the online docs.

Firstly we need to generate the pre-shared key. This isn’t user-specific so I put it in the system config directory:

$ sudo openvpn —genkey —secret /etc/openvpn/key.txt

Also in the config directory is the openvpn.conf file which should look like this:

dev tun
port 1194
proto udp
secret key.txt
keepalive 10 120
status server.log

You’ll also need to enable IP forwarding in the openvpn initialisation file in /etc/init.d/openvpn. Open the file in an editor and search for “ip_forward”. Uncomment the line, save and exit. It should look like this:

echo 1 > /proc/sys/net/ipv4/ip_forward

Finally, start the server process:

$ sudo service openvpn start

Configure the firewall for forwarding/masquerading

The openvpn server can’t do much except accept packets unless you configure iptables appropriately. Edit /etc/sysconfig/iptables with the following:


Restart iptables to load the new config:

$ sudo service iptables restart

Lastly, set both openvpn and iptables to start on boot:

$ sudo chkconfig openvpn on && sudo chkconfig iptables on

OpenVPN client config

All clients will need a copy of the /etc/openvpn/key.txt file and the following config file:

dev tun
proto udp
remote [your ec2 hostname goes here] 1194
resolv-retry infinite
secret key.txt
dhcp-option DNS
redirect-gateway def1

I tested this using the Windows OpenVPN client from, but it should work fine with any client so long as you put key.txt and ec2.ovpn in their openvpn/config directory.