Ubuntu 22.04 LTS Set Up OpenVPN Server
Setting up an OpenVPN server on Ubuntu 22.04 LTS can be done in just a few minutes by following these steps:
- Install OpenVPN:
sudo apt update
sudo apt install openvpn- Generate the necessary encryption keys and certificates:
sudo easyrsa init-pkirnsudo easyrsa build-carnsudo easyrsa build-server-full server nopassThis will create the necessary keys and certificates in the /etc/openvpn/easy-rsa/pki directory.
- Create the server configuration file:
sudo nano /etc/openvpn/server.conf- Add the following contents to the file, replacing the IP address with the IP of your server:
port 1194
proto udprndev tunrnca /etc/openvpn/easy-rsa/pki/ca.crtrncert /etc/openvpn/easy-rsa/pki/issued/server.crtrnkey /etc/openvpn/easy-rsa/pki/private/server.keyrndh /etc/openvpn/easy-rsa/pki/dh.pemrnserver 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txtrnkeepalive 10 120
comp-lzornpersist-keyrnpersist-tunrnstatus openvpn-status.logrnverb 3- Enable IP forwarding:
sudo nano /etc/sysctl.confand add or uncomment the following line:
net.ipv4.ip_forward=1- Enable the OpenVPN service:
sudo systemctl start openvpn@server- Enable the OpenVPN service to start automatically at boot time:
sudo systemctl enable openvpn@server- Configure the firewall to allow OpenVPN traffic, you can use
ufwfor that.
sudo ufw allow 1194/udprnsudo ufw enable- Create a client configuration file, you can use a
.ovpnextension for it.
sudo nano /etc/openvpn/client.ovpn- Add the following contents to the file, replacing the IP address with the IP of your server:
clientrndev tunrnproto udprnremote <your server IP> 1194
resolv-retry infiniternnobindrnpersist-key
persist-tunrncomp-lzornverb 3
<ca>
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
</ca>
<cert>
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
</cert>
<key>
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
</key>You can copy the ca, cert and key from the server's /etc/openvpn/easy-rsa/pki directory.
