Setting up a VPN on Linux can be done in several ways, depending on your needs (e.g., connecting to a commercial VPN service, hosting your own VPN, or using a corporate VPN). Below are common methods: Most VPN providers offer Linux CLI tools or OpenVPN/WireGuard configurations.
Steps:
- Install the VPN client (if available):
# Example for NordVPN (Debian/Ubuntu) sudo apt install curl curl -s https://repo.nordvpn.com/gpg/nordvpn_public.asc | sudo apt-key add - echo "deb https://repo.nordvpn.com/deb/nordvpn/debian stable main" | sudo tee /etc/apt/sources.list.d/nordvpn.list sudo apt update sudo apt install nordvpn
- Log in and connect:
nordvpn login nordvpn connect
- Disconnect:
nordvpn disconnect
Alternative: Manual OpenVPN/WireGuard Setup
- Download
.ovpn(OpenVPN) or.conf(WireGuard) files from your VPN provider. - Use
openvpnorwg-quickto connect:sudo apt install openvpn # For OpenVPN sudo openvpn --config yourfile.ovpn
sudo apt install wireguard # For WireGuard sudo wg-quick up yourfile.conf
Self-Hosted VPN (WireGuard or OpenVPN)
Option A: WireGuard (Recommended)
WireGuard is lightweight and secure.
-
Install WireGuard:
sudo apt install wireguard resolvconf # Debian/Ubuntu
-
Generate Keys:
umask 077 wg genkey | tee privatekey | wg pubkey > publickey
-
Configure Server/Client:
-
Edit
/etc/wireguard/wg0.conf(server example):[Interface] Address = 10.0.0.1/24 PrivateKey = <SERVER_PRIVATE_KEY> ListenPort = 51820 [Peer] PublicKey = <CLIENT_PUBLIC_KEY> AllowedIPs = 10.0.0.2/32
-
On the client, create a similar config with the server's public key.
-
-
Start WireGuard:
sudo wg-quick up wg0 sudo systemctl enable wg-quick@wg0 # Enable on boot
Option B: OpenVPN
- Install OpenVPN:
sudo apt install openvpn easy-rsa
- Set up a CA and generate certificates:
make-cadir ~/openvpn-ca cd ~/openvpn-ca source vars ./clean-all ./build-ca ./build-key-server server ./build-key client1
- Configure Server/Client:
- Copy sample config:
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ gunzip /etc/openvpn/server.conf.gz
- Edit
/etc/openvpn/server.confand client configs.
- Copy sample config:
Corporate VPN (e.g., Cisco AnyConnect, OpenConnect)
For Cisco VPNs, use openconnect:
sudo apt install openconnect sudo openconnect vpn.example.com
Troubleshooting
- No Internet After VPN: Check routes with
ip routeand disable VPN's killswitch (if any). - DNS Leaks: Use
resolvectlor manually set DNS (e.g.,nameserver 1.1.1.1in/etc/resolv.conf).
GUI Options
- NetworkManager: Supports OpenVPN/WireGuard via GUI (right-click network icon > VPN).
- GNOME/KDE VPN Plugins: Integrate with system settings.
Let me know if you need help with a specific setup!

