Back to blog

How to Set Up a WireGuard VPN Server on Ubuntu (Step by Step)

September 21, 2026 2 min read

This walkthrough sets up a WireGuard VPN server on a fresh Ubuntu VPS by hand. You'll need root or sudo access. (If you'd rather skip the manual steps, there's a faster option at the end.)

What you need

  • An Ubuntu 22.04 or 24.04 VPS with a public IP (see how to choose one)
  • SSH access
  • A device to connect from

1. Install WireGuard

sudo apt update
sudo apt install -y wireguard

2. Generate the server keys

wg genkey | sudo tee /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key
sudo chmod 600 /etc/wireguard/server_private.key

Never share the private key. The public key goes to clients.

3. Find your network interface

The config below needs your server's main interface name:

ip route | grep default

Look for the name after dev — often eth0, ens3, or enp1s0.

4. Create the server config

Create /etc/wireguard/wg0.conf (replace the interface name and key):

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <contents of server_private.key>
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -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

The PostUp rules let VPN clients reach the internet through the server.

5. Enable IP forwarding

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

6. Open the firewall port

WireGuard uses UDP. If you use ufw:

sudo ufw allow 51820/udp

Also check any firewall in your provider's control panel.

7. Start the server

sudo systemctl enable --now wg-quick@wg0
sudo wg show

8. Create a client

On the client device, generate its own keypair, then add it to the server as a peer. Append to wg0.conf:

[Peer]
PublicKey = <client public key>
AllowedIPs = 10.8.0.2/32

Apply it with sudo systemctl restart wg-quick@wg0.

The client's config looks like:

[Interface]
PrivateKey = <client private key>
Address = 10.8.0.2/24
DNS = 1.1.1.1
MTU = 1280

[Peer]
PublicKey = <server public key>
Endpoint = <server public IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Don't skip the DNS line. Without it, many clients keep using their old DNS server and nothing loads even though the tunnel shows "connected."

Troubleshooting

  • Connected but no internet: check IP forwarding, the interface name in PostUp, and the client's DNS line.
  • No handshake: confirm UDP 51820 is open at every firewall and the endpoint IP is correct.
  • Only some sites load: try a lower MTU such as 1280.

The faster way

Every new device means repeating steps 8 and the restart by hand. Nullsify installs WireGuard on your VPS for you and generates a separate config for each device, with import links and QR codes — no editing config files. See what WireGuard is for background.

Ready to run your own VPN?

Bring your own VPS and have VLESS, AmneziaWG, or WireGuard running in minutes — no manual setup required.