...
Skip to main content
Print

How to Install a WireGuard VPN on Your VPS

With WireGuard you create your own VPN server on a VPS in fifteen minutes. You install WireGuard, create keys, set up the server and connect your computer or phone as a client. Your own VPN gives you a fixed IP address, an encrypted connection on public Wi-Fi and secure access to your servers.

Step 1: install WireGuard

apt update
apt install wireguard -y

Step 2: create the keys

Create a key pair for the server and one for the client:

cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee client.key | wg pubkey > client.pub

Step 3: set up the server

Check with ip a what your network interface is called, for example eth0. Then create the configuration file:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = INHOUD-VAN-server.key
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = INHOUD-VAN-client.pub
AllowedIPs = 10.8.0.2/32

Step 4: enable IP forwarding and start WireGuard

echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p
ufw allow 51820/udp
systemctl enable --now wg-quick@wg0

Step 5: set up the client

Install the WireGuard app on your computer or phone and add a tunnel with this configuration:

[Interface]
PrivateKey = INHOUD-VAN-client.key
Address = 10.8.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = INHOUD-VAN-server.pub
Endpoint = JOUW-IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Activate the tunnel. Check on the server with wg show whether the client is connected.

Related articles

Table of Contents