Setting up a VPN on Ubuntu can be done in several ways, depending on whether you want to connect to a VPN server (e.g., for privacy or accessing remote networks) or host your own VPN server (e.g., using OpenVPN or WireGuard). Below are common methods:
Using GUI (Network Manager)
Most VPN providers (e.g., NordVPN, ProtonVPN) offer .ovpn configuration files. Here's how to import them:
- Install OpenVPN (if not pre-installed):
sudo apt update sudo apt install openvpn network-manager-openvpn network-manager-openvpn-gnome
- Import VPN Configuration:
- Download the
.ovpnfile from your VPN provider. - Go to Settings > Network > VPN and click to add a new VPN.
- Choose "Import from file" and select the
.ovpnfile. - Enter your VPN username/password if required.
- Download the
Using Command Line (OpenVPN)
If you prefer the terminal:
sudo openvpn --config your-config.ovpn
(Enter credentials when prompted.)
Hosting Your Own VPN Server
Option A: WireGuard (Fast & Modern)
-
Install WireGuard:
sudo apt update sudo apt install wireguard resolvconf
-
Generate Keys:
umask 077 wg genkey | sudo tee /etc/wireguard/private.key sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key
-
Configure Server (
/etc/wireguard/wg0.conf):[Interface] PrivateKey = <server-private-key> Address = 10.0.0.1/24 ListenPort = 51820 PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE [Peer] # Client configuration PublicKey = <client-public-key> AllowedIPs = 10.0.0.2/32
-
Enable IP Forwarding:
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf sudo sysctl -p
-
Start WireGuard:
sudo systemctl enable --now wg-quick@wg0
Option B: OpenVPN (Traditional)
- Install OpenVPN & Easy-RSA:
sudo apt update sudo apt install openvpn easy-rsa
- Set Up PKI:
make-cadir ~/openvpn-ca cd ~/openvpn-ca nano vars # Customize fields source vars ./clean-all ./build-ca # Build CA ./build-key-server server # Server cert ./build-dh # Diffie-Hellman openvpn --genkey --secret keys/ta.key
- Configure Server (
/etc/openvpn/server.conf):port 1194 proto udp dev tun ca /path/to/ca.crt cert /path/to/server.crt key /path/to/server.key dh /path/to/dh.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 8.8.8.8" keepalive 10 120 comp-lzo user nobody group nogroup persist-key persist-tun status openvpn-status.log verb 3
- Start OpenVPN:
sudo systemctl start openvpn@server sudo systemctl enable openvpn@server
Troubleshooting
- Connection Issues: Check logs with
journalctl -u openvpnorsudo wg show. - Firewall: Allow VPN ports (e.g., UDP 1194 for OpenVPN, UDP 51820 for WireGuard):
sudo ufw allow 51820/udp
Which VPN to Choose?
- For Speed/Simplicity: Use WireGuard.
- For Compatibility: Use OpenVPN.
- For Privacy: Use a trusted VPN provider (avoid free ones).
Let me know if you need help with a specific step!









