Make sure to replace your_domain
with your actual domain name. You need to ensure that your domain points to the IP address of your Scaleway Instance.
Installing OpenVPN on Ubuntu 20.04 or later
- vpn
- OpenVPN
- Ubuntu
- Bionic-Beaver
OpenVPN is an open-source software to run a virtual Private Network (VPN) to create secure point-to-point or site-to-site connections in routed or bridged configurations. The software uses a proprietary security protocol that uses SSL/TLS for key exchange.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- An Instance running on Ubuntu 20.04 or later
Installing Easy-RSA
The first step in building an OpenVPN configuration is to establish a PKI (Public Key Infrastructure). It is composed of the following elements:
- a public and private key for the server and each client
- the certification authority (CA) and the key used to identify servers as well as the client certificate
OpenVPN supports two-way certificate-based authentication, this means that the client must authenticate the server certificate and the server must authenticate the client certificate before mutual trust is established.
Both the server and the client will authenticate each other. First, the certificate needs to be signed by the certification authority (CA) then, the information in the header (common name of the certificate or the certificate type) of the authenticated certificate can be tested.
-
Connect to your Instance via SSH.
-
Update the package List:
apt updateapt upgrade -y -
Install OpenVPN and Easy-RSA:
apt install -y openvpn easy-rsa -
Set Up the CA Directory:
make-cadir ~/openvpn-cacd ~/openvpn-ca -
Initialize the PKI:
./easyrsa init-pki -
Build the Certificate Authority:
./easyrsa build-ca nopass -
Generate the server certificate and key:
./easyrsa gen-req server nopass./easyrsa sign-req server server -
Generate the Diffie-Hellman parameters:
./easyrsa gen-dh -
Generate a shared secret:
openvpn --genkey secret ta.key
Configuring the OpenVPN server
-
Copy the server certificate and key files:
cp pki/ca.crt pki/private/server.key pki/issued/server.crt ta.key /etc/openvpn/ -
Create the OpenVPN Server configuration file:
nano /etc/openvpn/server.confAdd the following configuration, save the file and quit
nano
:port 1194proto udpdev tunca ca.crtcert server.crtkey server.keydh dh.pemauth SHA256tls-auth ta.key 0server 10.8.0.0 255.255.255.0ifconfig-pool-persist ipp.txtpush "redirect-gateway def1 bypass-dhcp"push "dhcp-option DNS 8.8.8.8"push "dhcp-option DNS 8.8.4.4"keepalive 10 120cipher AES-256-CBCuser nobodygroup nogrouppersist-keypersist-tunstatus openvpn-status.loglog-append /var/log/openvpn.logverb 3
Setting up a Let’s Encrypt TLS certificate
-
Install Certbot:
apt install -y certbot -
Obtain the TLS certificate:
certbot certonly --standalone -d your_domainTip -
Configure OpenVPN to use the Let’s Encrypt certificate:
- Update the
server.conf
file to use the Let’s Encrypt certificate and key:ca /etc/letsencrypt/live/your_domain/fullchain.pemcert /etc/letsencrypt/live/your_domain/cert.pemkey /etc/letsencrypt/live/your_domain/privkey.pem
- Update the
Enabling IP forwarding and adjusting the firewall
-
Enable IP forwarding:
echo 1 > /proc/sys/net/ipv4/ip_forward- Make the change permanent by editing the
sysctl.conf
file:Uncomment the following line:nano /etc/sysctl.confnet.ipv4.ip_forward=1
- Make the change permanent by editing the
-
Configure the firewall of the Instance (UFW):
ufw allow 1194/udpufw allow OpenSSHufw enableAdd the following rules to
before.rules
to allow forwarding:nano /etc/ufw/before.rulesAdd these lines before the
*filter
line:*nat:POSTROUTING ACCEPT [0:0]-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADECOMMIT
Starting the OpenVPN server
-
Start and enable OpenVPN:
systemctl start openvpn@serversystemctl enable openvpn@server -
Check the status of the OpenVPN server:
systemctl status openvpn@server
Setting up client configuration
-
Generate client certificates:
cd ~/openvpn-ca./easyrsa gen-req client1 nopass./easyrsa sign-req client client1 -
Create the client configuration file:
nano ~/client1.ovpnAdd the following configuration:
clientdev tunproto udpremote your_domain 1194resolv-retry infinitenobindpersist-keypersist-tunca ca.crtcert client1.crtkey client1.keytls-auth ta.key 1cipher AES-256-CBCverb 3 -
Transfer the client configuration files to the remote (client) machine:
scp ~/openvpn-ca/pki/ca.crt ~/openvpn-ca/pki/issued/client1.crt ~/openvpn-ca/pki/private/client1.key ta.key user@your_client_machine:~/client1/scp ~/client1.ovpn user@your_client_machine:~/client1/
Your OpenVPN server is now set up on your Scaleway Instance, secured with a Let’s Encrypt certificate, and ready for clients to connect. Follow the client configuration steps for each device you want to connect to your VPN.
For ongoing maintenance, remember to renew your Let’s Encrypt certificates regularly (they expire every 90 days), and you can automate this with a cron job:
echo "0 0 1 */2 * certbot renew --quiet" | crontab -