How to: Setting up OpenVPN on Raspberry Pi

By Rory Monaghan

SHARE

Yes, this is yet another How to for setting up an OpenVPN on a Raspberry Pi device. I put this one together because, personally I tend to document everything I do incase I need to re-trace my steps and also because I tried following a few tutorials I found online and ended up running into little quirks a long the way, like needing to update the system first. So here’s a long step by step on how to get this all set up and going.

I referenced a really great YouTube tutorial which you can view here:

I hit a couple of issues which the fella in the video didn’t seem to hit e.g. I needed to run updates and also set the permissions on my files differently because it didn’t work the suggested way. I think there were one or two other quirks which I had to get around also…but I set it up over 2 months ago, so I honestly can’t remember.

I have some screenshots here but not one for each line. Each line indicates a line to enter in the command\terminal window. I took pictures from my phone and so many came out terrible and were not worth adding. It should still be pretty straight forward to follow.

Before you start. Ensure you’ve got your SD Card (Recommend 8GB) at the ready.

Prepare Device

Download the NOOBS with Network from HERE
Format 8GB SD Card

Extract downloaded NOOBS zip to the SD Card

Insert SD Card into Raspberry Pi system, plug it all in and go.

20140621_135731

OS Installation and Configuration

20140726_153957

 

Select to install Raspbian

20140726_154020

 

Click Yes

20140726_154139

 

Installation will commence

20140726_155727

 

Click OK

20140726_155745

 

Raspberry Pi should start up as above, type sudo raspi-config and hit enter

20140726_160428

 

When installed change Overclock to Medium

20140726_160448

Navigate to Advanced

20140726_160505

Select Memory Split

20140726_160516

GPU is set to 64

20140726_160524

Change the value to 16

20140726_160531

Expand the File System to ensure all 8GB is available

20140726_160541

Choose Finish

20140726_160546

Choose Yes to Reboot

20140726_160842

Login as Username: Pi Password: raspberry

20140726_161356

Type sudo apt-get install openvpn openssl

20140726_161427

 

Type Y and Enter

20140726_161636

 

Wait until complete

20140726_161716

Type sudo apt-get install rpi-update to see if there’s any firmware updates

20140726_161742

Type sudo rpi-update to see if there’s any software updates

20140726_161914

 

Wait until complete

20140726_161951

Type cd /etc/openvpn

20140726_162053

Type sudo Cp –r /usr/share/doc/openvpn/examples/easy-rsa/2.0 ./easy-rsa

20140726_164100

Type sudo su

Type cd /etc/openvpn

Type nano easy-rsa/vars

Replace the text that says pwd with /etc/openvpn/easy-rsa

Type . ./easy-rsa/vars

Type ./easy-rsa/clean-all

Cd easy-rsa

Type ln –s openssl-1.0.0.cnf openssl.cnf to create a symbolic link to our openssl file

Cd ..

Type ./easy-rsa/build-ca OpenVPN

20140726_165351

Enter Meta-data if you wish

20140726_165503

.easy-rsa/build-key-server server

Enter metdata

Just hit enter when prompted for challenge password and company name

Enter Y to create the certificate and y again to commit

Type ./easy-rsa-/build-key client1

 

Repeate steps of entering meta-data. Hitting enter for challenge password and organization. As well as y to create and y to commit certificate

Type ./easy-rsa/build-dh   (this may take a few minutes to complete)

Type nano openvpn.conf

 

Type in:

Dev turn

Proto udp port 1194

ca /etc/openvpn/easy-rsa/keys/ca.crt

cert /etc/openvpn/easy-rsa/keys/server.crt

key /etc/openvpn/easy-rsa/keys/server.eky

dh /etc/openvpn/easy-rsa/keys/dh1024.pem

user nobody

group nogroup

server 10.8.0.0 255.255.255.0

persist-key

persist-tun

status /var/log/openvpn-status.log

verb 3

client-to-client

push “redirect-gateway def1”

#set the dns servers

push “dhcp-option DNS 8.8.8.8”

push “dhcp-option DNS 8.8.4.4”

log-append /var/log/openvpn

comp-lzo

Ctrl X and yes to save and then hit enter to overwrite the file

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

Ifconfig

(note the inet addr: e.g. 192.168.0.11 (you should also see eht0…if not you may need to note what it is, for the next command)

iptables –t nat –A POSTROUTING –s 10.8.0.0/24 –o eth0 –j SNAT – to 192.168.0.11

nano sysctl.conf.

20140726_173357

Remove the comment from the ipforwarding line

cd..

sudo /etc/init.d/openvpn start

nano newvpn.ovpn

Contents should be as follows:

dev tun

client

proto udp

remote <Your Raspberry Pi’s Public IP address>

resolv-retry infinite

nobind

persist-key

persist-tun

ca ca.crt

cert client1.crt

key client1.key

comp-lzo

verb 3

 

Router1

Open up port 1194 on Router

Sudo nano /etc/rc.local

 

Between the fi and exit 0 enter the following:

 

Iptables –t nat –A INPUT –I eth0 –p udp –m udp –dport 1194 –j ACCEPT

20140726_173134

Iptables –t nat –A POSTROUTING –s 10.8.0.0/24 –o eth0 –j SNAT –to-source 192.168.0.11

Cp –rf /etc/openvpn/easy-rsa/keys /home/pi

20140726_175741

Chown pi:pi /home/pi

Chown pi:pi /home/pi/keys

Chown pi:pi /home/pi/keys/ca.crt

Chown pi:pi /home/pi/keys/client1.crt

Chown pi:pi /home/pi/keys/client1.key

FTP1

 

Connect using the internal IP address and pi username and password. I used WinSCP as a free download for Windows.

FTP2-2

Copy all three files using SFTP to you client machines. These will be required to connect.

DMZ2

 

Set IP address of Raspberry Pi in the DMZ

Type curl ifconfig.me back on the Raspberry Pi UI to get the Public IP address

Ready to test!! Fire up your Favorite VPN client (if using MAC you could use the built in client) and give it a whirl. You’ll need to point to the three key files you copied via the SFTP earlier. Enjoy

Let's make virtualization easier!

Be amongst the first to know when I publish new reviews, guides and tools to simplify your projects.

By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.

We'll virtualise your 5 most complex apps for FREE