VPN in Linux: WireGuard and OpenVPN
What a VPN is
A VPN (virtual private network) carries one or more network connections
over another network, usually the internet. The packets travel inside an
encrypted tunnel, so the network in between sees only the tunnel, not what
goes through it. The article
VPN for normal people
covers when a VPN helps and when it only moves the problem.
Three shapes cover almost every case:
- host to site: one computer joins a remote network, the usual «corporate VPN» and the one this page sets up;
- site to site: two networks are joined, for example an office and a data centre;
- host to host: two computers talk over the tunnel and nothing else is involved.
Both ends agree on keys first, then every packet gets encrypted and
wrapped in a new one addressed to the other end of the tunnel. The
applications keep using their normal sockets: they see an interface with
an address, exactly like an Ethernet card.
A VPN moves traffic, it does not hide it from the other end. The exit
server sees the unencrypted requests and knows your address, so a VPN
changes who can watch the traffic, not whether anybody can.
Tunnels and encryption
Tunnelling encapsulates one protocol in another: the original packet,
headers included, becomes the payload of a carrier packet. That is what
makes a logical link out of two endpoints that need not be neighbours.
Two things are added on top: encryption, so nobody on the path reads the
payload, and authentication, so both ends know whom they talk to. A tunnel
without encryption is just a route; one without authentication can be
redirected to an impostor.
The price is the overhead: a new IP header, a UDP header and the protocol
headers of the tunnel take tens of bytes out of every packet, so the
usable MTU of the tunnel interface is smaller than 1500. WireGuard uses
about 60 bytes over
IPv4
and about 80 over
IPv6
, which is why its default
MTU is 1420 and why a link that already has a smaller MTU needs a lower
value.
OpenVPN
OpenVPN (OpenVPN Inc., GPLv2) builds a point-to-point or server-to-clients
tunnel over a single UDP or TCP port, 1194 by default. It uses TLS for the
handshake, so the usual certificates apply. It passes NAT when the port is
reachable; behind a router that means a port forward, and on the server
the firewall has to allow the port.
OpenVPN 2 reads one .ovpn profile and is what
every distribution still ships. The newer openvpn3
client keeps profiles in its own store; the commands below use 2.x.
The server side
Both protocols need a server that hands out addresses and routes the
clients out to the internet. OpenVPN does it with a
server.conf, a certificate authority from
easy-rsa and net.ipv4.ip_forward=1; WireGuard
does it with a peer per client, the same forwarding switch and a NAT rule.
The server sections below use an RPM system (Rocky, CentOS,
RHEL
); the client sections work on any distribution. The NAT rule and
the open port
belong to the firewall: the article
Firewall in Linux
covers
ufw
, firewalld and nftables, and the distribution pages go into the
details, one per system:
CentOS
(firewall),
Rocky
(firewall),
Ubuntu
(firewall)
and
Debian
(firewall).
Ports to open: 1194/udp for OpenVPN and
51820/udp for WireGuard. Both are conventions, and
both are worth changing on a public server.
Install OpenVPN
The server needs openvpn and easy-rsa, which builds the certificate authority. On Rocky and CentOS 8 and newer (on CentOS 7 use yum instead of dnf):
sudo dnf install epel-release
sudo dnf install openvpn easy-rsa
The sample server.conf ships with the package, and its path carries the package version. Ask rpm where it is, then copy the file it prints:
rpm -ql openvpn | grep server.conf
sudo cp /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/server.conf /etc/openvpn/server.conf
The version in that path is the one installed: on a newer package the directory is /usr/share/doc/openvpn/sample/sample-config-files/.
easy-rsa
Copy the tool, then build the authority and the server certificate. The directory is versioned on some packages (/usr/share/easy-rsa/3.0.8), plain /usr/share/easy-rsa/3 on others; on Debian the files sit directly in /usr/share/easy-rsa/. Copy the contents of the one that exists, then run the commands from /etc/openvpn/easy-rsa:
sudo mkdir -p /etc/openvpn/easy-rsa
sudo cp -r /usr/share/easy-rsa/3/* /etc/openvpn/easy-rsa/
cd /etc/openvpn/easy-rsa
The transcripts below come from easy-rsa 3.2 with EASYRSA_BATCH=1, which answers the prompts instead of you. The run kept its PKI in a throwaway directory under /tmp, so the paths in the output are not /etc/openvpn/easy-rsa/pki; the progress dots OpenSSL prints are left out.
sudo ./easyrsa init-pki
Notice ------ 'init-pki' complete; you may now create a CA or requests. Your newly created PKI dir is: * /tmp/eatest/pki
sudo ./easyrsa build-ca nopass
Notice ------ CA creation complete. Your new CA certificate is at: * /tmp/eatest/pki/ca.crt Build-ca completed successfully.
sudo ./easyrsa gen-req server nopass
----- Notice ------ Private-Key and Public-Certificate-Request files created. Your files are: * req: /tmp/eatest/pki/reqs/server.req * key: /tmp/eatest/pki/private/server.key
sudo ./easyrsa sign-req server server
Using configuration from /tmp/eatest/pki/c5dcac78/temp.00 Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'server' Certificate is to be certified until Feb 28 07:39:10 2029 GMT (825 days) Write out database with 1 new entries Database updated WARNING ======= INCOMPLETE Inline file created: * /tmp/eatest/pki/inline/private/server.inline Notice Certificate created at: * /tmp/eatest/pki/issued/server.crt
sudo ./easyrsa gen-dh
Generating DH parameters, 2048 bit long safe prime DH parameters appear to be ok. Notice ------ DH parameters of size 2048 created at: * /tmp/eatest/pki/dh.pem
sudo openvpn --genkey secret ta.key
build-ca asks for a common name and sign-req asks to confirm when the commands are run by hand; gen-dh took under a minute on the machine that produced these transcripts, and a busy or an old machine takes minutes. Then copy what server.conf expects into /etc/openvpn:
sudo cp pki/ca.crt pki/issued/server.crt pki/private/server.key pki/dh.pem ta.key /etc/openvpn/
easy-rsa 2 wanted its vars file sourced first;
easy-rsa 3 refuses that with
You appear to be sourcing an Easy-RSA *vars* file.
Edit vars instead, or answer the prompts.
OpenVPN 2.7 deprecated the old spelling of the last command,
--genkey --secret ta.key: it still writes the file
and adds
DEPRECATED OPTION: The option --secret is deprecated.
and
WARNING: Using --genkey --secret filename is DEPRECATED. Use --genkey secret filename instead.
Configure the server
Open /etc/openvpn/server.conf and check these lines. The sample already has them, commented out where a choice is expected:
sudo vi /etc/openvpn/server.conf
port 1194 proto udp dev tun ca ca.crt cert server.crt key server.key dh dh.pem tls-auth ta.key 0 user nobody group nobody persist-key persist-tun push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 1.1.1.1"
proto udp is the better default; TCP is for
networks that block UDP, and then the firewall rule is
1194/tcp. user and
group drop privileges after startup; the group is
nogroup on Debian and Ubuntu.
persist-key and persist-tun
keep the tunnel usable across a restart of the process, and OpenVPN warns
when user and group are set without them. OpenVPN 2.7 ignores
persist-key and says so:
DEPRECATED OPTION: --persist-key option ignored. Keys are now always persisted across restarts.
The two
push lines make a full tunnel and send a resolver
with it; drop them for a split tunnel.
port-share localhost 80 lets a TCP server share
port 443 with a web server. It does nothing with UDP and is not needed
here.
Enable forwarding, or the server will not route the clients out:
sudo vi /etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p
Open the port and add NAT. With firewalld:
sudo firewall-cmd --permanent --add-port=1194/udp
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload
On Ubuntu and Debian the port is sudo ufw allow 1194/udp, and NAT is a masquerade rule in /etc/ufw/before.rules or a line in the server configuration; the article Firewall in Linux shows the front ends.
Check the service
The unit openvpn@server reads /etc/openvpn/server.conf. The newer openvpn-server@server unit reads /etc/openvpn/server/server.conf instead; use the one that matches where you put the file. Start it and read the status:
sudo systemctl enable --now openvpn@server
systemctl status openvpn@server
The line Initialization Sequence Completed in Status means the tunnel is up. The journal holds the same line and the reason when it is not:
journalctl -u openvpn@server -n 50
OpenVPN client
On the client you need the package and a profile: one .ovpn file for OpenVPN 2, or the newer openvpn3 client with a profile imported into its own store. The 2.x client is what every distribution still ships.
sudo apt install openvpn
On the .rpm systems the same package is
sudo dnf install openvpn.
Put the profile where the service expects it. One profile is called
client here, and the name after the
@ is the profile name:
sudo cp office.ovpn /etc/openvpn/client/client.conf
Start it once in the foreground to see the log, then as a service. The foreground run prints the handshake and stays in the terminal until Ctrl + C.
sudo openvpn --config /etc/openvpn/client/client.conf
sudo systemctl start openvpn-client@client
The profile is the file the server administrator gives you: it holds the remote address and port, the protocol, the certificates or their inline copies, and often the routes to push. Do not edit the certificates by hand; if the profile is wrong, ask for a new one.
WireGuard client
WireGuard is in the
Linux
kernel since 5.6 and speaks UDP only. It has no daemon of its own to
configure: a private key, a public key per peer and a list of allowed
addresses are the whole configuration, and
wg-quick turns that into an interface.
Install the tools:
sudo apt install wireguard
On the .rpm systems the tools are
sudo dnf install wireguard-tools.
Generate the key pair with a strict umask, so the private key is not
readable by other users. The private key stays on the machine; the public
one goes to the server administrator, who puts it into the peer stanza of
the server.
umask 077; wg genkey | tee privatekey | wg pubkey > publickey
The command prints nothing itself; both files get a base64 key, and the private one is readable by its owner only because of the umask. A throwaway pair generated for this page:
cat privatekey publickey
UK5tn/qnfgN0nK79FlbOk9dPGnW6/hRXn1dHZ34WrWo= 9Fuep3WKK1+GHhG6dKWmq5bHibYgphBFUI/ygJbfwRo=
Create the interface file. Its name is the interface name, so /etc/wireguard/wg0.conf becomes wg0.
sudo vi /etc/wireguard/wg0.conf
[Interface]
PrivateKey = <contents of privatekey>
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = <server public key>
Endpoint = vpn.example.com:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Address is the address inside the tunnel and must
be the one the server assigned to this peer.
AllowedIPs does double duty: packets to those
networks go into the tunnel, and packets from the peer with any other
source address are dropped. 0.0.0.0/0, ::/0 sends
everything, which is the usual client setup;
10.8.0.0/24 would send only the tunnel network.
DNS is applied by
wg-quick through resolvconf,
so it needs that tool installed.
Bring the interface up once to test it:
sudo wg-quick up wg0
The command needs root: it re-runs itself through sudo and then asks the kernel for a WireGuard interface. Without NET_ADMIN and /dev/net/tun the kernel answers RTNETLINK answers: Operation not permitted, which is why this step has no output quoted.
To start it at boot, enable the unit. The unit runs wg-quick up itself, so do not run both on an interface that is already up:
sudo systemctl enable wg-quick@wg0
Check the tunnel
The first sign of a working tunnel is the interface. OpenVPN creates tun0 or tap0, WireGuard creates the name of its file, wg0. Before the tunnel is up the answer is short:
ip a show tun0
Device "tun0" does not exist.
The second sign is the route. Ask the kernel which interface it would use for an outside address; without a tunnel it names the physical one:
ip route get 1.1.1.1
1.1.1.1 via 172.17.0.1 dev eth0 src 172.17.0.5 uid 1000 cache
For WireGuard the peer state is the honest answer: a recent handshake means the keys agree and packets flow both ways. With no interface up wg show prints nothing and exits 0.
sudo wg show
For OpenVPN the log of the service holds the line Initialization Sequence Completed when the tunnel is up, and the reason when it is not:
journalctl -u openvpn-client@client -n 50
Finally, check which address the world sees. Without a VPN it is the address of the provider; with one it is the exit address of the server.
curl -s https://ifconfig.me
The command printed the public address of the machine that ran it. The value is not repeated here: it describes that machine and not the command, and on your machine it will be a different one.
Common errors
Handshake, but nothing works. WireGuard drops packets whose source
address is not in AllowedIPs, and a client with
0.0.0.0/0 still needs forwarding and NAT on the
server. Check ip route get first, then the server
rules.
Pages load, large transfers stall. That is MTU. Lower the MTU of
the tunnel interface to 1380 or 1360 and try again; the same value goes
into MTU = in the interface section.
DNS stops resolving. A full tunnel takes the
DNS
server with it: either push a resolver that is reachable through the
tunnel, or set one in the client profile. A resolver that only answers
inside the corporate network is the usual culprit.
Connection refused or timeout. The port is closed or filtered. The
test with telnet or nc from the article
telnet in Linux
tells «refused» from «filtered» before you read
logs.
The server exits at once. Read the journal: a typo in
server.conf and a missing file are the two usual
causes. The first example is the protocol: OpenVPN 2.4 and 2.5 stop on
Options error: --explicit-exit-notify can only be used with --proto udp Use --help for more information
and OpenVPN 2.7.9 only notes it and carries on with NOTICE: --explicit-exit-notify ignored for --proto tcp. A missing certificate set names every file, and this is the 2.7.9 list:
Options error: --dh fails with 'dh2048.pem': No such file or directory (errno=2) Options error: --ca fails with 'ca.crt': No such file or directory (errno=2) Options error: --cert fails with 'server.crt': No such file or directory (errno=2) 2026-11-26 07:40:17 WARNING: cannot stat file 'server.key': No such file or directory (errno=2) Options error: --key fails with 'server.key': No such file or directory (errno=2) Options error: Please correct these errors. Use --help for more information.
The timestamp is the moment of that run. A missing
ta.key stops the run before the list:
Cannot pre-load keyfile (ta.key), then
Exiting due to fatal error.
These lines come from a server that ran OpenVPN from
/etc/openvpn with relative file names and had the
files somewhere else. Keep the certificates next to
server.conf, or give absolute paths.
TLS handshake failed. The client does not trust the certificate
authority that signed the server certificate, or the dates do not overlap.
This transcript comes from a real client and was not reproduced here:
OpenSSL: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed TLS_ERROR: BIO read tls_read_plaintext error TLS Error: TLS handshake failed
Check that the client profile has the current ca.crt and that the server certificate has not expired.
Article author: Arthur Isaev