Code
Build A Home VPN With OpenVPN

VPN stands for Virtual Private Network, but the name can be confusing. A VPN is a protocol that lets a client join a non-local network through an encrypted connection and act as if that network were its local one. I recommend that everyone have one, and this tutorial shows how to build your own at home with OpenVPN.

What a VPN does

A VPN has two parts: a server, which hosts the VPN, and a client, which uses it. When a client wants access to resources on the VPN, it starts an encrypted connection to the server over ordinary TCP or UDP. Once connected, the server assigns the client an IP address and the client can reach everything on the VPN.

In our case the client also pushes all of its network traffic through the encrypted connection. When the client opens a web page, its browser sends the request through the encrypted connection to the VPN, the VPN sends the request out to the website on the public internet, the website replies to the VPN, and the VPN returns the response to the client over the encrypted connection. This makes it safe to browse private information from shared public WiFi in coffee shops, hotels and the like.

Suppose Bob is sitting in a coffee shop and needs to check his bank account over the shop's WiFi. Typing his password into a public network is not safe. But Bob has his own home VPN server, so he connects to it first and reaches his bank through his secure home network. His connection to the VPN is encrypted and his home network is secure, so his login stays safe.

What you will build

A remote client, for example on public WiFi, connects through an encrypted tunnel on TCP port 1194. The home router forwards that port to the VPN server inside the home network, which gives the client an address in 10.8.0.x and sends its requests out to a website on the public internet, returning the response through the tunnel.
The VPN this tutorial builds. The client's traffic enters the home network through the forwarded port 1194, and the VPN server sends it on to the internet and brings the reply back through the tunnel.

What you need

For hardware:

  • A Linux-based computer for the server. I recommend a Raspberry Pi 3B, which is what I use. It is a cheap Linux machine that can be dedicated to being the VPN server.
  • Access to your home router's port forwarding and firewall options.
  • A client for testing. Any Windows, OSX or Linux laptop will do, or an Android phone. I have not tested any iOS clients, but I am fairly confident a good OpenVPN client app exists for iOS.

For software:

  • A Linux-based OS for the server. On a Raspberry Pi I recommend Raspbian.
  • A client of your choice. On Windows, OpenVPN GUI is a good option. On Android I used the OpenVPN for Android app. On OSX a free client such as TunnelBear will work, or you can run openvpn PathToDirectory/client.conf from the terminal.

Step 1: Install the server software

Once Raspbian is installed, run these commands on the server in order:

$ sudo apt-get update -y
$ sudo apt-get upgrade -y
$ sudo apt-get install python-pip -y
$ sudo yes | pip install pexpect && apt-get install libssl-dev -y
$ sudo apt-get install openvpn

Step 2: Build the certificates and keys

Now we build the VPN itself: a certificate authority, a server certificate, client certificates, Diffie-Hellman parameters and a TLS authentication key. These lines are run from the Easy-RSA directory, so copy it into place and change into it first.

$ sudo cp -r /usr/share/easy-rsa /etc/openvpn/easy-rsa
$ cd /etc/openvpn/easy-rsa

Then run the following, in order:

$ sudo cp openssl-1.0.0.cnf openssl.cnf
$ sudo source ./vars && ./clean-all && ./build-ca
$ sudo source ./vars && ./pkitool --server server
$ sudo source ./vars && ./pkitool client1 && ./pkitool client2 && ./pkitool client3 && ./pkitool client3 && ./pkitool client4 && ./pkitool client5 && ./pkitool client6 && ./pkitool client7 && ./pkitool client8 && ./pkitool client9 && ./pkitool client10
$ sudo source ./vars && ./build-dh
$ cd keys
$ sudo openvpn --genkey --secret ta.key

build-ca asks a series of questions about the certificate authority. The playbook in the repository linked at the end accepts the default answer to each. In the original post the double-dash options were typeset as en dashes; the lines above use the plain -- that the shell expects, as in the playbook. The cd lines come from the playbook too, which runs each command in the directory shown. Because source is a shell builtin, sudo source may fail on your system. If it does, open a root shell with sudo -s and run the same lines without sudo, which is how the playbook runs them.

That is all for the keys. Each pkitool call creates a certificate and key for one client, so you get client1 through client10 and can give each device its own.

Step 3: Write the server configuration

Create the server configuration file:

$ sudo touch /etc/openvpn/easy-rsa/keys/server.conf
$ sudo nano /etc/openvpn/easy-rsa/keys/server.conf

Copy the following in, then press control-x, y, enter to save:

port 1194
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
client-to-client
keepalive 10 120
tls-auth ta.key 0
cipher AES-256-CBC
persist-key
persist-tun
status openvpn-status.log
verb 3

A few of these lines matter for what we are building. The server listens on TCP port 1194 and gives each client an address in 10.8.0.0/24. The redirect-gateway line is what pushes all of the client's traffic through the tunnel, and the two dhcp-option lines give the client Google's DNS servers. The server side is now done.

Step 4: Set up your home router

This is a short two-step process on the router:

  1. Set up a static IP for your VPN server.
  2. Enable port forwarding to port 1194 for the static IP you just set.

If things do not work and this looks correctly set, temporarily disable your router's firewall to see whether it is still blocking the port. Sometimes the firewall setting has to be set to low for this to work.

Step 5: Set up a client

Depending on the client, you may or may not need to write a configuration file by hand. Here is the Android client, which I used.

Copy the files to the phone

Use SFTP to download these four files from /etc/openvpn/easy-rsa/keys to your computer:

ta.key
ca.crt
client1.crt
client1.key

You may need to adjust the permissions so you can download them:

$ sudo chmod ugo+rw file

Plug the Android into your computer, make a folder on it named OpenVPN, and put the four files there.

Create the profile in OpenVPN for Android

  1. Open the OpenVPN for Android app and add a profile. Name it, then use the select buttons to choose the CA certificate (ca.crt), the client certificate (client1.crt) and the client certificate key (client1.key) from the OpenVPN folder you made.
  2. On the server list tab, enter your public IP address (search for "what is my IP" if you cannot find it), or your own domain name if you have one linked to your public IP. Set the port to 1194 and select the TCP protocol, which matches the server configuration above. If you switched the server to UDP, select UDP.
  3. On the IP and DNS tab, scroll to the bottom and set the DNS server to 8.8.8.8 and the backup DNS server to 8.8.4.4. These are Google's, which I prefer, but use others you trust if you like. Check "Override DNS Settings by Server".
  4. On the routing tab, check the IPv4 box "Use Default Route".
  5. On the authentication/encryption tab, check "Expect TLS server certificate". Scroll to the bottom, enable "Use TLS Authentication", and set the TLS Auth File to the ta.key you copied.
  6. On the allowed apps tab, make sure "VPN is used for all apps but exclude selected" is enabled and that none of the boxes are checked.

Step 6: Test it

Switch the phone to LTE or a different WiFi network and tap the profile you just created. If everything is set correctly, it will report that the VPN is activated.

To check that you are really coming out of your home network, search for "what is my IP" and see whether the address matches the public IP of your home network. If it does, you have built your own home VPN.

A note from 2026

This tutorial is from 2019, and several parts have moved on. Easy-RSA has been rewritten: its current version uses a single easyrsa command instead of the vars, clean-all and pkitool scripts used here. Python 2 and its pip package are gone from current systems, and Raspbian is now called Raspberry Pi OS. OpenVPN itself has also changed how it sets cipher options and the syntax of some commands, so check its current documentation before copying the configuration. WireGuard has also become a popular and much simpler alternative to OpenVPN, with far less to configure. The steps above are unchanged from the original, so treat them as a record of how I built it at the time.

Source

The commands are also automated as an Ansible playbook in the raspberrypi-openvpn repository.