Building a VPN server on Ubuntu can provide a secure way to connect to your network from remote locations. A popular software to achieve this is OpenVPN, a robust and highly flexible VPN daemon. This guide will provide a high-level overview of setting up an OpenVPN server on an Ubuntu machine. Please note that this guide assumes you're using Ubuntu 20.04 or later.
Step 1: Install OpenVPN and Easy-RSA
First, you need to install the OpenVPN software and Easy-RSA, a small RSA key management package used for generating SSL certificates.
Open a terminal and run:
sudo apt update
sudo apt install openvpn easy-rsa
Step 2: Set Up the Certificate Authority
The Certificate Authority (CA) is responsible for creating and managing digital certificates. It will allow your VPN client and server to authenticate one another.
- Make a directory for your CA:
make-cadir ~/openvpn-ca
cd ~/openvpn-ca
- You'll need to modify the
vars file to customize your certificates. Open the file with your preferred editor:
nano vars
Find and modify the KEY_* variables (e.g., KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, KEY_EMAIL) to match your organization's details.
- After saving and closing the file, source the vars file and build your CA:
source vars
./clean-all
./build-ca
Step 3: Create the Server Certificate, Key, and Encryption Files
- First, source the vars file if you've not already:
source vars
- Then, build the server certificate and key:
./build-key-server server
Follow the on-screen prompts. The defaults should be fine if you've correctly edited the vars file in the previous step.
- Generate the Diffie-Hellman key exchange file:
./build-dh
- Generate an HMAC signature to strengthen the server's TLS integrity verification capabilities:
openvpn --genkey --secret keys/ta.key
Step 4: Configure the OpenVPN Service
- Copy the example server configuration file to the OpenVPN directory:
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
cd /etc/openvpn
sudo gzip -d server.conf.gz
- Edit the configuration file:
sudo nano server.conf
Un-comment and adjust the lines:
ca ca.crt
cert server.crt
key server.key
dh dh.pem
Also, the path might need adjustment depending on where your CA is located.
- Copy the server keys and certificates into the OpenVPN directory:
sudo cp ~/openvpn-ca/keys/{server.crt,server.key,ca.crt,dh2048.pem,ta.key} /etc/openvpn/
Step 5: Adjust the Network Configuration
- Allow IP forwarding by editing the
sysctl.conf file:
sudo nano /etc/sysctl.conf
Find and uncomment the line net.ipv4.ip_forward=1.
Apply the changes:
sudo sysctl -p
- Configure
ufw (Uncomplicated Firewall) to allow the VPN traffic:
Adjust your ufw settings accordingly, allowing UDP traffic on the OpenVPN port (default 1194) and configuring NAT forwarding.
Step 6: Starting OpenVPN
Start the OpenVPN server by:
sudo systemctl start openvpn@server
Enable it to start at boot:
sudo systemctl enable openvpn@server
Step 7: Creating Client Configuration Files
You'll then need to create configuration files for each client. This process involves generating client certificates and keys, modifying client configuration samples, and ensuring the client is configured to connect to your server's public IP.
Remember, this guide provides a high-level overview. Each of these steps involves deeper considerations, especially related to security settings and firewalls. Always refer to the latest OpenVPN documentation and Ubuntu security guidelines when configuring your VPN.