VPN in Linux: WireGuard and OpenVPN client setup
What a VPN is
A VPN (virtual private network) is a set of technologies that carry 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. Ordinary
layered models keep the layers apart; a tunnel deliberately puts a packet
of the same or a lower layer inside another, which 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; a tunnel 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 client
OpenVPN (OpenVPN Technologies, GNU GPL) builds a point-to-point or
server-to-clients tunnel over a single UDP or TCP port, 1194 by default.
It works through NAT and firewalls without changing their settings, and it
uses TLS for the handshake, so the usual certificates apply.
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
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 route 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
Generate the key pair. 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.
wg genkey | tee privatekey | wg pubkey > publickey
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.
Bring the interface up, and make it come up at boot:
sudo wg-quick up wg0
sudo systemctl enable --now wg-quick@wg0
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 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.
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.
ip a show tun0
The second sign is the route. Ask the kernel which interface it would use for an outside address:
ip route get 1.1.1.1
For WireGuard the peer state is the honest answer: a recent handshake means the keys agree and packets flow both ways.
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
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.
Article author: Arthur Isaev