Instructions for setting up a raspberry pi for vpn
Raspberry Pi + power supply + SD card (min. 8 Gb)
Raspbian (the minimal version)
Etcher (OSX) OR Win32 Disk Imager (Windows)
- Use Etcher or Win32 Disk Imager to write the raspbian image to the SD card
- Create an empty file with the name
ssh
(no extention) and place it on the sd root - this will enable ssh access to the pi - Put the SD in the Pi, connect it to the network and then the power supply
- Wait a few seconds for it to boot
- On a computer in the same network, look for the Pi's local IP address (I'm using the IP Scanner)
arp -a
on the terminal may also do the job
- Connect to the Pi using SSH
- on OSX, open the terminal and type
ssh [email protected]
(with the Pi's local IP address instead of the xxx). This may prompt a security question, just typeyes
and press enter. - on Windows, use PuTTY
- on OSX, open the terminal and type
- When asked for the password, the raspbian default is
raspberry
- The connection will be made and terminal will show
pi@raspberry:~ $
. All the commands from now on, until weexit
, will run in the Pi. - Update the OS
sudo apt-get update
sudo apt-get upgrade
- To change the Pi login password: type
passwd
, press enter, and when asked, the new password
ifconfig
on the terminal to show the network information. What we need is in theeth0
section. Make a note of the values:- inet (current local IP address)
- netmask (mine was 255.255.255.0)
- broadcast (mine was 192.168.1.255)
route -n
and take note of the Gateway (mine was 192.168.1.254)cat /etc/resolv.conf
and take note of the nameservers (DNS) used- Pick a local IP address for the Pi that has not been attributed to other machine - best pick from outside the automatic IP range (usually 50 and up) and the router IP (usually 1).
I'm using 8 in this exemple (you can use any IP but may need to reserve that IP on the router). Edit the interfaces file
sudo nano /etc/network/interfaces
(to exit the text editor pressctrl + x
) and edit it so it looks something like:
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.8
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
dns-nameservers 8.8.8.8 8.8.4.4
- Press
ctrl + x
to exit,y
to answer yes to the save prompt,enter
to apply the changes to the file - Restarting the network may suffice but I rather reboot the system -
sudo reboot
- SSH again, this time with the new static local IP address and the new password.
Instructions of this coming soon as this is different from router to router.
- Install openVPN - inside the user "pi" folder (
cd ~
)wget https://git.io/vpn -O openvpn-install.sh
sudo bash openvpn-install.sh
- Follow the steps using the recommended values
- We need to move the key certificate to a folder to later access it with FTP
- the file is in the
root
folder, so we need to usesudo
permissions - to check if the file is there, do
sudo ls ../../root
- move the file to the pi user's folder with
sudo mv ../../root/filename.ovpn filename.ovpn
- use
ls
to check if the file was moved
- the file is in the
- After the initial install, everytime we need to create a key certificate for a new user:
- from the pi user folder (
cd ~
) run the installersudo bash openvpn-install.sh
- press
1
for new - type the name for what machine this is going to be used on and press enter. The file will be generated inside the root folder.
- from the pi user folder (
- from a new terminal window (not in ssh)
scp [email protected]:/home/pi/filename.ovpn /Users/mycomputerusername/Desktop/filename.ovpn
- after the file was copied to the computer, you can delete it from the pi, so ssh into it and
rm filename.ovpn
For this stage I recomend using another device so you don't need to keep reconnecting to the Pi. I use the OpenVPN app on the iPhone. For OSX use Tunnelblick
- Install the key
- iOS - email yourself, open the file on the iPhone and chose to open it with the OpenVPN app
- OSX - just drag the file to the Tunnelblick top bar icon
- Turn on the VPN
- iOS - inside the OpenVPN app, press the on/off switch. You can then just use the VPN switch inside the iOS settings
- OSX - click the top bar icon and chose connect "name_of_client"
A good wait to make sure it's working, is to use mobile data (turn wifi off) and go to what is my ip and check if your IP is the same as your home network.
Best install this after VPN is working, so when it stops working, you know you need to tweek the firewall doors.
- Install the Uncomplicated Firewall with
sudo apt-get install ufw
- Edit the file ufw -
sudo nano /etc/default/ufw
yes
in the IPv6 (the default) -IPV6=yes
ctrl + x
to exit
- Setup the firewall:
- to allow all outgoing and none incoming -
sudo ufw default allow outgoing
andsudo ufw default deny incoming
- to allow VPN -
sudo ufw allow 1194
- to allow SSH -
sudo ufw allow ssh
orsudo ufw allow 2222/tcp
(for specific port) - to allow FTP -
sudo ufw allow ftp
(open when FTP is needed) - to block FTP -
sudo ufw delete allow ftp
(close when FTP is not needed)
- to allow all outgoing and none incoming -
- Usefull ufw commands
sudo ufw status sudo ufw disable sudo ufw enable
- Other commands:
sudo ufw allow www sudo ufw allow 80/tcp sudo ufw allow ftp sudo ufw allow 21/tcp sudo ufw delete allow ssh sudo ufw delete allow 80/tcp
- To reset all ufw settings
sudo ufw reset