Openvpn On Raspberry Pi

10 min read Oct 10, 2024
Openvpn On Raspberry Pi

Setting Up OpenVPN on a Raspberry Pi: A Comprehensive Guide

The Raspberry Pi, a small and affordable computer, offers a versatile platform for various applications. One of its popular uses is setting up a secure VPN server using OpenVPN. This allows you to access the internet privately and securely, bypassing geographical restrictions and enhancing your online privacy. This guide will walk you through the process of configuring OpenVPN on your Raspberry Pi, step-by-step.

Why Choose OpenVPN on Raspberry Pi?

OpenVPN is a widely-used, robust, and secure VPN protocol. It uses strong encryption to protect your internet traffic, making it ideal for protecting your online privacy and security. The Raspberry Pi's affordability, low power consumption, and flexibility make it an excellent choice for hosting a personal OpenVPN server.

Prerequisites

Before you begin, ensure you have the following:

  • A Raspberry Pi: Any Raspberry Pi model is suitable for this task.
  • An SD card: A microSD card with at least 8GB of storage.
  • A power supply: A 5V/2.5A power adapter for your Raspberry Pi.
  • Ethernet cable or Wi-Fi connection: To connect your Raspberry Pi to the internet.
  • An SSH client: PuTTY, MobaXterm, or a similar SSH client is required to access your Raspberry Pi remotely.
  • A basic understanding of Linux commands: Familiarity with basic Linux commands will be helpful throughout the process.

Step-by-Step Guide to Setting Up OpenVPN on Raspberry Pi

1. Prepare your Raspberry Pi:

  • Install Raspberry Pi OS: Download the latest version of Raspberry Pi OS (formerly known as Raspbian) and flash it onto your SD card. You can use the official Raspberry Pi Imager tool for this.
  • Connect to your Raspberry Pi: Connect your Raspberry Pi to your network using an Ethernet cable or Wi-Fi. Once booted up, you can access your Raspberry Pi via SSH using the IP address assigned to it.

2. Update and Upgrade:

  • Open a terminal window: Use your SSH client to connect to your Raspberry Pi.
  • Update and upgrade system packages: Ensure you have the latest system updates by running the following commands:
    sudo apt update
    sudo apt upgrade -y 
    

3. Install OpenVPN:

  • Install OpenVPN: Use the following command to install the OpenVPN package:
    sudo apt install openvpn -y 
    

4. Configure OpenVPN:

  • Create a configuration file: Navigate to the OpenVPN configuration directory using the following command:
    cd /etc/openvpn 
    
  • Create a new configuration file: Create a new configuration file for your OpenVPN server using a text editor like nano:
    sudo nano server.conf
    

5. Configure the OpenVPN server:

  • Paste the following configuration into the server.conf file:

    port 1194
    proto udp
    dev tun
    ca ca.crt
    cert server.crt
    key server.key
    dh dh2048.pem
    server 10.8.0.0 255.255.255.0
    push "redirect-gateway def1"
    push "dhcp-option DNS 8.8.8.8"
    push "dhcp-option DNS 8.8.4.4"
    keepalive 10 120
    cipher AES-256-CBC
    user nobody
    group nogroup
    persist-key
    persist-tun
    status /var/log/openvpn-status.log
    log-append /var/log/openvpn.log
    verb 3
    mute 20
    client-to-client
    explicit-exit-notify 1
    management 127.0.0.1 5555
    management-query-passwords
    management-hold
    management-log
    management-unix-sockets /var/run/openvpn-status.sock
    management-timeout 120
    tls-auth ta.key 1
    auth-user-pass auth.txt
    redirect-dns
    topology subnet
    
    
  • Save the configuration file: Press Ctrl+X, then Y, and then Enter to save the changes.

6. Create the Certificate Authority (CA):

  • Generate the CA key and certificate: Use the following command to generate the necessary files for the CA:
    sudo openvpn --genkey --secret ca.key --name "OpenVPN CA"
    
  • Create the CA certificate:
    sudo openvpn --easy-rsa --genrsa --out ca.crt --batch ca.key
    

7. Generate the Server Certificate and Key:

  • Generate the server key:
    sudo openvpn --easy-rsa --genrsa --out server.key --batch
    
  • Generate the server certificate:
    sudo openvpn --easy-rsa --sign-file server.key server.crt --batch
    

8. Generate the Diffie-Hellman (DH) Parameters:

  • Generate DH parameters:
    sudo openvpn --easy-rsa --genrsa --out dh2048.pem --batch
    

9. Generate the TLS Authentication Key:

  • Generate the TLS authentication key:
    sudo openvpn --easy-rsa --genrsa --out ta.key --batch
    

10. Create the Authentication File:

  • Create a new file named auth.txt in the /etc/openvpn directory:
    sudo nano auth.txt
    
  • Paste the following lines into the file:
    username password
    
  • Replace username and password with your desired credentials: This will be used for authentication when connecting to the server.
  • Save the file: Press Ctrl+X, then Y, and then Enter.

11. Configure the OpenVPN service:

  • Edit the OpenVPN service file:
    sudo nano /etc/systemd/system/[email protected]
    
  • Modify the file by replacing the lines:
    ExecStart=/usr/sbin/openvpn --config /etc/openvpn/server.conf --daemon
    
  • Save the file: Press Ctrl+X, then Y, and then Enter.
  • Enable the OpenVPN service:
    sudo systemctl enable [email protected]
    
  • Start the OpenVPN service:
    sudo systemctl start [email protected]
    

12. Install OpenVPN Client:

  • Install the OpenVPN client:
    sudo apt install openvpn
    

13. Configure the OpenVPN client:

  • Create a configuration file on your client device:

    sudo nano client.conf
    
  • Add the following lines to the configuration file:

    remote [Raspberry Pi IP address] 1194
    proto udp
    dev tun
    ca ca.crt
    cert client.crt
    key client.key
    auth-user-pass auth.txt
    tls-auth ta.key 1
    cipher AES-256-CBC
    verb 3
    mute 20
    
  • Replace [Raspberry Pi IP address] with the actual IP address of your Raspberry Pi.

  • Generate the client certificate and key:

    sudo openvpn --easy-rsa --genrsa --out client.key --batch
    
    sudo openvpn --easy-rsa --sign-file client.key client.crt --batch
    

14. Connect to the OpenVPN server:

  • Copy the configuration file (client.conf) and the certificates (ca.crt, client.crt, client.key, and ta.key) to your client device.
  • Import the certificates into your OpenVPN client software.
  • Connect to the server using the OpenVPN client.

15. Test the Connection:

  • Once connected, check your IP address: Use a website like **** to verify that your IP address has changed to the server's IP address.

Conclusion

Setting up an OpenVPN server on a Raspberry Pi provides a cost-effective and secure way to access the internet privately. This guide provides a detailed walkthrough of the process, from installing the necessary software to configuring both the server and client sides. By following these steps, you can enjoy the benefits of a secure VPN connection, enhancing your online privacy and security.

Featured Posts