Your Own VPN on a Linux Server With WireGuard

With WireGuard your computer or phone sends all its IPv4 internet traffic through your server. Websites then see the server's address instead of yours, and on a public Wi-Fi nobody in between can read your traffic. The steps were tested on Ubuntu 26.04 LTS with a Linux client; run the commands as root.

IPv6 traffic is not routed by this configuration.


1. Install WireGuard and Create the Keys

apt install wireguard qrencode
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee client1.key | wg pubkey > client1.pub

Each side has a private key, which stays secret, and a public key, which the other side gets. umask 077 makes sure only root can read the new files.

2. Find the Name of Your Network Card

ip route show default

The name follows dev, for example default via 198.51.100.126 dev eno0. Use your name wherever eno0 appears below.

3. Configure the Server

In the same folder, this command writes the file /etc/wireguard/wg0.conf with both keys already filled in:

cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = $(cat server.key)
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eno0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eno0 -j MASQUERADE

[Peer]
PublicKey = $(cat client1.pub)
AllowedIPs = 10.8.0.2/32
EOF

Let the server pass traffic on, open the WireGuard port in the firewall and start the VPN:

echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-wireguard.conf
sysctl --system
ufw allow 51820/udp
ufw route allow in on wg0 out on eno0
systemctl enable --now wg-quick@wg0

If you do not use ufw, leave out the two ufw lines. wg show shows the VPN and its devices.

4. Create the File for Your Device

Replace YOUR.SERVER.IP with your server's address:

cat > /etc/wireguard/client1.conf <<EOF
[Interface]
PrivateKey = $(cat client1.key)
Address = 10.8.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = $(cat server.pub)
Endpoint = YOUR.SERVER.IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

AllowedIPs = 0.0.0.0/0 sends all IPv4 traffic through the VPN. To reach only the server through it, write AllowedIPs = 10.8.0.0/24 instead.

5. Connect

  • Phone (WireGuard app for Android or iOS): show the file as a QR code on the server with qrencode -t ansiutf8 < /etc/wireguard/client1.conf and scan it in the app.
  • Windows and macOS (WireGuard app from wireguard.com): copy client1.conf to your computer, for example with SFTP, and import it with Import tunnel(s) from file. How the app looks is shown in Wireguard configuration.
  • Linux: install wireguard-tools, save the file as /etc/wireguard/wg0.conf and run wg-quick up wg0.

Check it: myip.com must now show your server's address. On the server, wg show shows a latest handshake for the device.

⚠ One file per device

The file contains the device's private key: keep it private, and give every device its own key and address. To add a device, create keys client2.key and client2.pub as in step 1, add a new [Peer] block with AllowedIPs = 10.8.0.3/32 to wg0.conf, and run systemctl reload wg-quick@wg0. Connected devices stay connected.

Stop the VPN

systemctl disable --now wg-quick@wg0

Related Guides

View all Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)