In this section, we will cover the steps to set up a Virtual Private Network (VPN) server on a Linux system. A VPN allows you to create a secure connection to another network over the Internet. This can be useful for accessing resources on a private network, securing your internet connection, or bypassing geo-restrictions.

Objectives

By the end of this section, you will be able to:

  1. Understand the basics of VPNs and their use cases.
  2. Install and configure OpenVPN on a Linux server.
  3. Generate the necessary keys and certificates for secure connections.
  4. Configure client devices to connect to the VPN server.
  5. Test the VPN connection to ensure it is working correctly.

Prerequisites

  • Basic knowledge of Linux command line.
  • A Linux server with root or sudo access.
  • A domain name or a static IP address for your server.

  1. Understanding VPNs

What is a VPN?

A VPN (Virtual Private Network) is a technology that creates a secure and encrypted connection over a less secure network, such as the internet. VPNs are commonly used to:

  • Securely connect remote users to a private network.
  • Protect data transmitted over public networks.
  • Bypass geo-restrictions and censorship.

How VPNs Work

VPNs work by creating a secure tunnel between the client and the server. All data transmitted through this tunnel is encrypted, ensuring that it cannot be intercepted by unauthorized parties.

  1. Installing OpenVPN

OpenVPN is a popular open-source VPN solution. Follow these steps to install OpenVPN on your Linux server.

Step 1: Update Your System

First, ensure your system is up to date:

sudo apt update
sudo apt upgrade -y

Step 2: Install OpenVPN and Easy-RSA

Install OpenVPN and Easy-RSA, a tool for managing SSL certificates:

sudo apt install openvpn easy-rsa -y

  1. Configuring OpenVPN

Step 1: Set Up the CA Directory

Create a directory for the Certificate Authority (CA):

make-cadir ~/openvpn-ca
cd ~/openvpn-ca

Step 2: Configure the CA Variables

Edit the vars file to set the CA variables:

nano vars

Modify the following lines to match your information:

export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="MyOrg"
export KEY_EMAIL="[email protected]"
export KEY_OU="MyOrgUnit"

Save and close the file.

Step 3: Build the CA

Initialize the PKI and build the CA:

source vars
./clean-all
./build-ca

Step 4: Generate the Server Certificate and Key

Generate the server certificate and key:

./build-key-server server

When prompted, enter the necessary information and confirm the prompts.

Step 5: Generate Diffie-Hellman Parameters

Generate the Diffie-Hellman parameters:

./build-dh

Step 6: Generate Client Certificates and Keys

Generate a certificate and key for each client:

./build-key client1

Repeat this step for additional clients, changing client1 to the desired client name.

  1. Configuring the OpenVPN Service

Step 1: Copy the Files to the OpenVPN Directory

Copy the generated files to the OpenVPN directory:

sudo cp ~/openvpn-ca/keys/{server.crt,server.key,ca.crt,dh2048.pem} /etc/openvpn/

Step 2: Create the OpenVPN Server Configuration File

Create and edit the OpenVPN server configuration file:

sudo nano /etc/openvpn/server.conf

Add the following configuration:

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
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"
keepalive 10 120
cipher AES-256-CBC
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
log-append /var/log/openvpn.log
verb 3

Save and close the file.

Step 3: Enable IP Forwarding

Enable IP forwarding by editing the sysctl.conf file:

sudo nano /etc/sysctl.conf

Uncomment the following line:

net.ipv4.ip_forward=1

Apply the changes:

sudo sysctl -p

Step 4: Configure UFW

Allow OpenVPN through the firewall:

sudo ufw allow 1194/udp

Add a rule to allow forwarding:

sudo nano /etc/ufw/before.rules

Add the following lines before the *filter line:

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE
COMMIT

Enable UFW:

sudo ufw enable

Step 5: Start the OpenVPN Service

Start and enable the OpenVPN service:

sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

  1. Configuring Client Devices

Step 1: Create Client Configuration Files

Create a client configuration file:

nano ~/client1.ovpn

Add the following configuration:

client
dev tun
proto udp
remote your_server_ip 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
remote-cert-tls server
cipher AES-256-CBC
verb 3

Replace your_server_ip with your server's IP address.

Step 2: Transfer the Files to the Client

Transfer the client1.ovpn, ca.crt, client1.crt, and client1.key files to the client device.

Step 3: Connect to the VPN

Use an OpenVPN client to connect to the VPN using the client1.ovpn file.

  1. Testing the VPN Connection

Step 1: Verify the Connection

On the client device, connect to the VPN and verify the connection:

ping 10.8.0.1

You should receive responses from the VPN server.

Step 2: Check the IP Address

Check your public IP address to ensure it matches the VPN server's IP:

curl ifconfig.me

Conclusion

In this section, you learned how to set up a VPN server using OpenVPN on a Linux system. You installed and configured OpenVPN, generated the necessary keys and certificates, configured client devices, and tested the VPN connection. This setup provides a secure way to connect to your private network over the internet.

© Copyright 2024. All rights reserved