Raspberry Pi

How to Create a Dedicated VPN server on Raspberry Pi

Turning Raspberry Pi into Virtual Private Network (VPN) server can provide better security and privacy over the internet. The VPN tool protect your privacy during port communication (transferring packets). A VPN makes your network connection private through encryption method. While using VPN, your IP address and location becomes concealed, which also increases the online privacy and security.

If you are looking for privacy and security features while using Raspberry Pi, you can turn your device into the VPN server. Follow this article, if you want to learn how to turn Raspberry Pi into a VPN server.

How to Create Dedicated VPN Server on Raspberry Pi

A few steps are required to turn Raspberry Pi into VPN server and it requires a list of prerequisites.

    • Raspberry Pi device
    • Internet Connection
    • SD Card

Setting VPN Server on Raspberry Pi

Before installation make sure your Raspberry Pi device is up to date. If not, use below-given command to check for updates on your Raspberry Pi device.

$ sudo apt update

 

Run the following command to install upgrades on Raspberry Pi:

$ sudo apt upgrade

 

After the update, use the following steps to create a dedicated VPN server on Raspberry Pi:

Step 1: To setup VPN server, install piVPN on your Raspberry Pi using the following command:

$ curl -L https://install.pivpn.io | bash

 

You will be prompted to a window, which shows the confirmation of piVPN installer on your Raspberry Pi device. Then press “Ok”.


Step 2: After that, a VPN server asks for a permission to access the DHCP server for the IP address. Select “Ok” to proceed.


Step 3: Next is to setup the statistic IP address by hitting the “No” option.


Step 4: After that, a statistic IP address will pop out. Hit enter on “Yes”.


Step 5: Next is the IP information will let the users know about the conflict of multiple IP addresses over the same router. Press the enter on the “Ok” option to proceed.


Step 6: Again, press enter for “Ok” option.


Step 7: Now choose “pi” as a user press “Ok”.      


Step 8: Now choose the VPN service you want. In my case, I am going with “OpenVPN”.


The immediate installation of packages will start when you proceed with “Yes” option.


Step 9: Now it will ask, do you want to use UDP or TCP protocol. As UDP has less tracking and much faster than the TCP so better to go with UDP.


Step 10: Now you need to choose the port number for VPN server. This port is needed to allow internet traffic through a router. In my case the port number is “1194”.


Press “Yes” to confirm the defined port.


Step 11: Now pick the DNS Server. In my case the “Google” will be the default DNS server. You can choose other you want.


Now hit “Yes” for custom DNS domain.


Step 12: Next is to add the server domain. Use the “No” option to automatically add the domain.

Step 13: Now use the DNS Entry in the next step if you do not have a statistic IP.


Step 14: Now select “Ok” and you will be directed to the information, where you will be using the OpenVPN 2.4. If you want to use older version of OpenVPN then you can press “No”.


Step 15: Next step will ask to use the encryption certificate. Select the default one, which is “256 Method”.


Step 16: And it will generate the server key and HMA key. You can press “Ok”.


Step 17: After selecting the Ok. It will automatically setup the device into the VPN server. But it will require a reboot permission. Press “Ok” to reboot.


After that, different security patches will be added to the Raspberry Pi, which are necessary to secure your connection.


Enable them using the “Yes” option.


A reboot option will appear. Just reboot the device by hitting the “Ok” option.

Add Profile to VPN on Raspberry Pi

After a successful reboot, you have to add a profile for piVPN using the following command.

$ pivpn add

 

Now you will be asked to add name, days, and password to setup VPN server.


After adding the information, you can see the OpenVPN file created in the specific folder, which is highlighted in the below image.


Once the setup is successful, go the the directory “ovpn” using the “cd” command and use the following cat command to retrieve VPN related information.

$ cat pi.ovpn

 

To confirm VPN is successfully setup on Raspberry Pi, use the following command:

$ hostname -I

 
The highlighted IP address is the private network address assigned to Raspberry Pi.

Connect Mobile to Dedicated VPN Server

To connect another system like mobile to dedicated Raspberry Pi VPN server, follow the below-given steps.

Step 1: Copy file “pi.ovpn” into your mobile or PC. We are using mobile device here. To setup connection, first download the “OpenVPN Connect” form App Store.


Open application from mobile and click on “Agree” to accept all terms and conditions.


After that, an import file page will pop-out there you can import the generated VPN server from mobile directory. To do this, follow the screenshot guidelines.


And import the “pi.ovpn” into OpenVPN server application.


Click “Ok” to import in OpenVPN server application.


After the successful import, you will be directed to a page, where OpenVPN profile will reside with the specific IP Address and name of server.


When you slide to connect with created VPN server. A password permission will appear. Type password you set during add VPN step and click on “Ok”.


Again click “Ok” to integrate VPN settings into your mobile.


Next is a notification will appear on your mobile screen, that will notify the successful connection over the specific IP Address.


That’s it you have successfully connected your mobile to a dedicated Raspberry Pi VPN server.

Conclusion

You can setup a dedicated VPN server on Raspberry Pi using the above-mentioned steps that uses the Pi-VPN service to successfully setup a VPN server on Raspberry Pi. Once the VPN server is created, you have to add a profile to VPN, which creates a file inside the Raspberry Pi directory. You can use this file and import it into other devices like mobile using the OpenVPN application.

About the author

Muhammad Nasir

Hi! I am an engineer and technical blogger by profession and I enjoy getting things done with electronics. I am using this platform to share my knowledge in the field of Raspberry Pi, Linux, and Embedded Systems with others.