WIREGUARD VPN SERVER

A working WireGuard server on Ubuntu 24.04: keys, clients, and the admin-plane lockdown that keeps SSH and dashboards off the public internet.

A self-hosted WireGuard VPN on your server

 

Set up a self-hosted WireGuard VPN on your dedicated server

A self-hosted VPN turns your dedicated server into the front door for a private network you control. WireGuard server setup on Ubuntu 24.04 is a short job: install one package, generate a keypair, write a config file, and you have an encrypted network that your laptops, phones and other servers can join from anywhere. This guide runs the whole thing top to bottom, with every command ready to paste.

The reader here is the sysadmin. Server.Biz sells unmanaged dedicated servers, so the box, the keys and the firewall are yours to run. Follow this in order and you finish with a WireGuard server, a first client, your admin ports reachable only over the tunnel, and a private link between two servers.

What a self-hosted WireGuard actually buys you

Three jobs make a private VPN on a dedicated server worth the half hour. The first is an admin plane: SSH, a Grafana dashboard, a Proxmox web UI, a database port, all of them bound to the VPN and invisible to the public internet. Scanners never see a login prompt because there is nothing listening for them to find. The second is a private path between your own machines, so an app server reaches its database or its backup target over an encrypted link instead of the open network. The third is a simple exit for a small team: a handful of people route their traffic out through one trusted IP address, useful for reaching an allowlisted service or working from an untrusted network.

Be honest about the one job this is not for. If you want to hop between countries to watch streaming catalogues, a commercial VPN with servers in dozens of cities fits far better than a single box in one datacenter. A self-hosted VPN gives you one exit location and one IP, which is exactly wrong for that use and exactly right for locking down your own infrastructure.

One policy note before you build. Whatever traffic leaves your server carries your server's IP, and under the acceptable use policy that traffic is your responsibility. An exit node for your own team is fine. An open relay for strangers is not, and it is the kind of thing that gets an IP address blocklisted. Keep the peer list to people and machines you control.

How WireGuard works in three sentences

In WireGuard, a keypair is an identity: each machine has a private key it keeps and a public key it hands out, and a peer is simply another public key plus the set of addresses allowed to arrive from it. There is no session to log in to and no connection state to negotiate; a packet either carries a valid signature from a known peer or it is dropped, silently. Because there is no daemon handshake to keep alive, a peer can change its IP address, roam from wifi to mobile, or sleep for an hour, and the tunnel picks up again on the next valid packet with no reconnect step.

1 UDP port
A single UDP port, 51820 by default, is all you expose
In-kernel
Runs as a Linux kernel module, not a userspace daemon
Roaming
Peers survive IP address and network changes without a reconnect

Install WireGuard and generate keys

WireGuard is in the Ubuntu 24.04 repositories, so installation is one line. Add qrencode at the same time; you will use it to hand configs to phones later.

Install the tools. The kernel module is already present on Ubuntu 24.04, so this only adds the userspace commands.

shell
sudo apt update
sudo apt install -y wireguard qrencode

Generate the server keypair. The umask 077 is the important part: it makes the key files readable only by root, so the private key never leaks through loose permissions.

shell
umask 077
wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key

That one line does three things: wg genkey makes a private key, tee saves it, and wg pubkey derives the matching public key from it. The private key stays on this server forever. The public key is safe to share, and you will paste it into every client config. Read either file back with "sudo cat" when you need its contents.

Write the server config

WireGuard interfaces are defined by a single file per interface. Create /etc/wireguard/wg0.conf with the interface's own addresses and its listening port. Give the VPN a private IPv4 range (10.8.0.0/24 here) and a private IPv6 range from the unique-local block (fd0d:cafe:1::/64), so clients get both.

The base server config. Replace SERVER_PRIVATE_KEY with the exact contents of /etc/wireguard/server_private.key. The server takes .1 in each range; clients get .2 and up.

/etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24, fd0d:cafe:1::1/64
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

# Peer blocks (one per client or server) get appended below.

The PrivateKey line is a placeholder on purpose. wg-quick reads this file as root, and the private key belongs to this interface, so you paste the key itself into the config rather than a path to it. Keep the file mode at 600 (the directory is already root-only from the umask step) and never copy this file to a client. Each client gets its own separate keypair.

Turn the server into a gateway

For the admin-plane and server-to-server jobs, the config above is enough. If you also want the server to act as an internet exit for clients, it has to forward and translate their traffic. Enable IP forwarding for both address families first.

Turn on forwarding in a sysctl drop-in so it survives reboots, then apply it.

/etc/sysctl.d/99-wireguard.conf
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1

Load the setting now without a reboot.

shell
sudo sysctl --system

Add these two lines to the [Interface] block of wg0.conf. They set up NAT when the tunnel comes up and tear it down when it stops. Replace eth0 with your real uplink interface; find it with "ip route get 1.1.1.1".

/etc/wireguard/wg0.conf
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i %i -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i %i -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

Open the WireGuard port in ufw, then enable the interface as a service so it starts on boot.

shell
sudo ufw allow 51820/udp
sudo systemctl enable --now wg-quick@wg0
sudo wg show

In those PostUp lines, wg-quick fills in the interface name (wg0) for you. The FORWARD accept rules matter on a ufw host: ufw drops forwarded traffic by default, and these lines let the tunnel's packets through. If you only want the admin plane and never an exit, you can skip the forwarding sysctl and the PostUp/PostDown lines entirely. The final "wg show" should now print your wg0 interface with its public key and listening port.

Add your first client

A client is your laptop or phone. Generate a keypair on the client the same way (umask 077, wg genkey), then write its config. The one decision that matters is AllowedIPs, which controls what traffic goes into the tunnel. Full tunnel sends everything through the server; split tunnel sends only the VPN's own range, so your normal browsing stays direct and only admin traffic is tunnelled.

The laptop config. Keep one AllowedIPs line and comment out the other. Replace the keys, and set Endpoint to your server's public IP and port.

laptop.conf
[Interface]
PrivateKey = LAPTOP_PRIVATE_KEY
Address = 10.8.0.2/32, fd0d:cafe:1::2/128
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = 203.0.113.10:51820
# Full tunnel: send all traffic through the server.
AllowedIPs = 0.0.0.0/0, ::/0
# Split tunnel: only reach the admin plane, leave normal traffic direct.
# AllowedIPs = 10.8.0.0/24, fd0d:cafe:1::/64
PersistentKeepalive = 25

On the server, append this peer to /etc/wireguard/wg0.conf. The AllowedIPs here is the reverse side of the setting: it is the single address the server accepts from this client.

/etc/wireguard/wg0.conf
[Peer]
# roy-laptop
PublicKey = LAPTOP_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32, fd0d:cafe:1::2/128

Apply the new peer without dropping the running tunnel. wg-quick strip removes the wg-quick-only lines so wg syncconf accepts the config.

shell
sudo wg syncconf wg0 <(wg-quick strip wg0)

For a phone, print the client config as a QR code and scan it in the WireGuard app. Nothing is typed by hand.

shell
qrencode -t ansiutf8 < laptop.conf

The DNS line matters for full tunnel: without it, name lookups can still leak to your local network. On a Linux client the DNS setting needs openresolv or systemd-resolved installed; on the phone and desktop apps it is handled for you. PersistentKeepalive = 25 sends a tiny packet every 25 seconds so a client behind a home router or mobile carrier keeps its path open.

Lock your admin surfaces to the VPN

This is the pattern that pays for the whole setup. Once every admin lives on the VPN, you can close those ports to the public internet and open them only to the tunnel's address range. A port scan from outside then finds nothing: no SSH banner, no Grafana login, no Proxmox page, no database port.

Allow each admin service only from 10.8.0.0/24. The examples below are SSH, Grafana on 3000, the Proxmox web UI on 8006, and PostgreSQL on 5432; add or drop lines to match what you run.

Permit the admin ports from the VPN range only. These rules do not open anything to the public internet.

shell
sudo ufw allow from 10.8.0.0/24 to any port 22 proto tcp
sudo ufw allow from 10.8.0.0/24 to any port 3000 proto tcp
sudo ufw allow from 10.8.0.0/24 to any port 8006 proto tcp
sudo ufw allow from 10.8.0.0/24 to any port 5432 proto tcp

Only after you have confirmed SSH works over the VPN, remove the public SSH rule so port 22 is no longer reachable from the open internet.

shell
sudo ufw delete allow 22/tcp
sudo ufw status verbose

Test before you close the door

Keep your current SSH session open, bring up the VPN on your laptop, and open a second SSH session to 10.8.0.1 in a new terminal. Only when that VPN session works should you delete the public rule. If you ever do lock yourself out, ask support for access to the management console (the BMC, sometimes called IPMI): it reaches the machine below the firewall, so a bad rule can be undone from there.

Add and remove team members cleanly

Run one peer per person per device. Give each device its own address and keypair, so revoking one laptop never disturbs anyone else. The rule of thumb: the person generates their own keypair and sends you only their public key, you assign them an address and add the peer, and removing them later is deleting that one block.

  1. Onboard: collect a public key

    The new team member installs WireGuard, runs the umask-and-genkey step on their own device, and sends you their public key. Their private key never leaves their machine.

  2. Onboard: assign and apply

    Pick a free address (10.8.0.11, 10.8.0.12 and so on), append a [Peer] block with a comment naming the device, and apply it with wg syncconf. Hand back the server public key and endpoint for their config.

  3. Offboard: delete the peer

    Remove that person's [Peer] block from wg0.conf and re-apply. Their key is now unknown to the server, so their tunnel stops on the next packet. No password to rotate, no shared secret to change for everyone else.

The peer commands

Two commands cover the whole lifecycle. Append the peer to the file for the permanent record, then apply it live without restarting the interface. To revoke immediately, remove the peer from the running interface and delete its block from the file.

Add a peer live after appending its block to wg0.conf. wg syncconf applies only the difference, so existing tunnels stay up.

shell
sudo wg syncconf wg0 <(wg-quick strip wg0)

Revoke a device instantly by removing its key from the running interface, then delete its block from wg0.conf so it does not come back on the next reload.

shell
sudo wg set wg0 peer THEIR_PUBLIC_KEY remove

Do not use "wg-quick save" to persist peers: it rewrites the file from the running state and drops your comments and PostUp lines. Editing wg0.conf by hand and applying with wg syncconf keeps the file readable and your NAT rules intact.

Troubleshooting and performance

When a client cannot connect, "wg show" on the server is the first and usually the last place to look. It lists each peer with its endpoint, the time of the latest handshake, and bytes transferred in each direction.

Read the live state of every peer.

shell
sudo wg show

A recent "latest handshake" and a rising transfer count mean the tunnel is healthy. If the handshake line never appears, the two sides are not agreeing, and it is almost always one of three things: the public keys are crossed (the client needs the server's public key and the server needs the client's), the UDP port is not open (check "sudo ufw status" for 51820/udp), or the Endpoint address or port is wrong. Work through those three before suspecting anything subtler.

If the tunnel is up and you can ping 10.8.0.1 but hostnames do not resolve, DNS is the issue: the client's DNS line is not being applied, so install openresolv or systemd-resolved on a Linux client, or point applications at an internal resolver reachable over the VPN.

On performance, WireGuard is light. It runs in the kernel, uses modern fast ciphers, and carries very little per-packet overhead, so on current CPUs it saturates a gigabit link without trouble and the network, not the encryption, is the limit you hit first. On the higher uplink tiers offered by the network you are far more likely to be bound by your own uplink than by anything WireGuard is doing.

Which AllowedIPs for which job

AllowedIPs is the one setting that changes what the tunnel does. This is the pattern for each of the jobs in this guide.

Use case Client AllowedIPs What it does
Admin plane only 10.8.0.0/24, fd0d:cafe:1::/64 Reach servers on the VPN; normal traffic stays direct
Full tunnel exit 0.0.0.0/0, ::/0 All of the client's traffic goes out through the server
Reach a remote subnet 10.9.0.0/24 Route into a second server's private range
Server-to-server link the peer's /32 (and /128) Point-to-point only, no exit

The short version

Install WireGuard, generate keys with umask 077, write wg0.conf with a 10.8.0.0/24 range plus an IPv6 ULA, open one UDP port, and enable wg-quick@wg0. Add clients one keypair at a time, choosing full or split tunnel with AllowedIPs. Then do the part that matters: move SSH, dashboards and database ports behind the VPN, test the VPN path before you close the public one, and remember that console access is on request, through support, if you lock yourself out. Harden the box itself first with our getting-your-server-online guide.

Continue reading

Need help choosing?

Our experts can help you find the perfect server for your needs.

Contact us »