Skip to content

NewExtra 20% Off on 3-Month Plans! πŸŽ‰ Order your VDS server for 3 months and get an extra 20% off in the cart!

Blog

Tutorials, guides, product news and engineering insights from the team that runs your hosting.

Network & VPN

How to Install WireGuard: Ubuntu VPN Setup Guide

8 October 2026 16 min read
Share this post

WireGuard is a fast VPN protocol with a short configuration that runs integrated into the Linux kernel. In this guide, you'll set up a WireGuard server from scratch on Ubuntu 22.04 and Ubuntu 24.04, connect a Windows client and see the causes of the most common problems. If you follow the commands in order, you'll end up with your own VPN server up and running.

What Is WireGuard?

WireGuard is open-source VPN software that creates an encrypted tunnel between two devices. It uses modern encryption algorithms, and its code base is much smaller than alternatives such as OpenVPN or IPsec. That makes it easier both to audit and to configure.

WireGuard comes as a module that runs inside the Linux kernel. The Ubuntu 22.04 and 24.04 kernels already include this support. On the server, you only need to install the management tools (wireguard-tools).

WireGuard has no "username and password" in the classic sense. Each device has a private key and a public key. A connection is established when both sides know each other's public key.

What Is WireGuard Used For?

You can use your own WireGuard VPN server mainly for the following purposes:

  • Secure internet access: Routing your traffic through your server on networks you don't trust, such as public Wi-Fi.
  • A fixed outbound IP address: Always going out with your server's IP address. This is useful for accessing services with IP restrictions.
  • Remote access: Accessing devices, panels or databases on your home or office network without exposing them to the internet.
  • A private network between servers: Connecting several servers to each other over an encrypted internal network.

This guide covers the classic "my own VPN server" scenario, where all traffic goes out through the server. To adapt it to another scenario, see the AllowedIPs section.

Requirements for Installing WireGuard

Before you start, you need:

  • A server running Ubuntu 22.04 or Ubuntu 24.04
  • Root privileges on the server, or a user who can use sudo
  • SSH access to the server
  • A public IPv4 address
  • Permission to open port 51820/UDP on the server and, if there is one, in the firewall in your provider's panel
  • A client device (Windows in this guide)

To set up your own WireGuard server, you can use a VPS/VDS with a public IPv4 address. If you don't have a server yet, you can explore Netiyo VDS solutions.

Updating the Ubuntu Server

Connect to the server via SSH and update the package list:

sudo apt update
sudo apt upgrade -y

If the kernel is updated during the upgrade, you may need to restart the server:

sudo reboot

Installing the WireGuard Package

sudo apt install wireguard -y

Confirm that the installation is complete with the version information:

wg --version

If the command prints the wireguard-tools version, the installation is complete.

Creating the Server Private/Public Keys

Every WireGuard device has a key pair. We create the keys for the server in the /etc/wireguard directory. The command below generates the files so that only root can read them (umask 077):

cd /etc/wireguard
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key'

Print both keys to the screen and note them down:

sudo cat /etc/wireguard/server_private.key
sudo cat /etc/wireguard/server_public.key

Important: Don't share the private key with anyone, and don't send it by email, messaging app, support ticket or screenshot either. Anyone who has the private key can impersonate your server. Only the public key can be shared.

Enabling IP Forwarding

For the server to forward VPN clients' traffic to the internet, IPv4 forwarding must be enabled in the kernel. To make the setting permanent, we write it to a separate file:

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

Check that the value is active:

sysctl net.ipv4.ip_forward

The output should be net.ipv4.ip_forward = 1.

Finding the Server's Network Interface

In the next step, we'll write a NAT rule. This rule needs the name of the server's network interface that goes out to the internet. This name isn't eth0 on every server. Depending on the virtualization platform and Ubuntu version, you may come across different names such as eth0, ens3, ens18 or enp1s0. Find the real name with this command:

ip route

Example output:

default via 203.0.113.1 dev ens3 proto dhcp src 203.0.113.10 metric 100

The value after the word dev in the line starting with default is your interface name. In the example above, it's ens3. You'll use this name in the configuration in the next section.

wg0.conf Server Configuration

Open the configuration file with a text editor:

sudo nano /etc/wireguard/wg0.conf

Paste the following content. Replace SERVER_PRIVATE_KEY with the value from the server_private.key file. Also replace eth0 with the interface name you found in the previous step:

[Interface]
Address = 10.66.66.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -I FORWARD 1 -i wg0 -j ACCEPT; iptables -I FORWARD 1 -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.66.66.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.66.66.0/24 -o eth0 -j MASQUERADE

Save the file and exit (in nano, Ctrl+O, Enter, Ctrl+X). Then restrict the file permissions:

sudo chmod 600 /etc/wireguard/wg0.conf

What the lines mean

  • Address: The server's address on the VPN network. 10.66.66.0/24 is a private internal network and is independent of the server's real IP address.
  • ListenPort: The UDP port WireGuard will listen on. 51820 is used by default.
  • PrivateKey: The server's private key.
  • PostUp / PostDown: Commands that run when the interface comes up and goes down. They allow the VPN clients' traffic and send it out to the internet with the server's IP address (NAT/MASQUERADE).

If the interface name is misspelled, the handshake succeeds but the client can't reach the internet. If you have problems, check this line first.

51820/UDP Firewall Settings

WireGuard uses UDP only. If you use UFW on Ubuntu, first check its status:

sudo ufw status

If UFW isn't active and you're thinking of enabling it, allow SSH access first. Otherwise, you may lose your connection to the server:

sudo ufw allow OpenSSH

Then open the WireGuard port:

sudo ufw allow 51820/udp

If you haven't enabled UFW yet, enable it last:

sudo ufw enable
sudo ufw status

If your provider's panel has a separate firewall or security group, you also need to open port 51820/UDP there.

Starting the WireGuard Service and Enabling Autostart

A single command is enough to start the WireGuard interface and make it come up automatically when the server restarts:

sudo systemctl enable --now wg-quick@wg0

Check the service status:

sudo systemctl status wg-quick@wg0

To see that the interface and listening status are correct:

sudo wg show
ip addr show wg0

You should see the 10.66.66.1/24 address in the ip addr show wg0 output. If the service fails, you can look at the details with the sudo journalctl -u wg-quick@wg0 --no-pager command. Most of the time the problem comes from typos in the configuration file.

Creating Client Keys

Each device needs a separate key pair. The safest method is for the private key never to leave the device.

For Windows: When you use the "Add empty tunnel" option in the WireGuard app, the key pair is generated automatically by the app. You only need to see and copy the public key on the app screen. Details are in the Windows section below.

For a Linux client: You can generate the keys in the client's own terminal:

umask 077
wg genkey | tee client_private.key | wg pubkey > client_public.key

Whichever method you use, only the public key is given to the server. The private key is never sent to the server or anyone else.

Adding a Peer

The server learns which clients to allow from the [Peer] blocks. Open the configuration again:

sudo nano /etc/wireguard/wg0.conf

Add the first client to the end of the file:

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.66.66.2/32

Replace CLIENT_PUBLIC_KEY with the client's public key. /32 means that only a single VPN IP address (10.66.66.2) is assigned to this client.

To apply the change, you can restart the service. This briefly interrupts existing connections:

sudo systemctl restart wg-quick@wg0

If you want to apply it without disconnecting connected users, you can use this command:

sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'

Windows WireGuard Client Configuration

Download and install the client app only from the official WireGuard website. Then:

  • Open the app, click the arrow next to Add Tunnel at the bottom left and choose Add empty tunnel....
  • Give the tunnel a name in the window that opens. The app automatically generates a PrivateKey and shows the Public key value at the top.
  • Copy this public key and add it to the [Peer] block on the server as CLIENT_PUBLIC_KEY (previous section).
  • Complete the client-side configuration as follows. Don't delete the existing PrivateKey line; add the rest below it.
[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.66.66.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
  • SERVER_PUBLIC_KEY: The server_public.key value you created on the server
  • SERVER_IP: Your server's public IPv4 address
  • DNS: 1.1.1.1 is used in the example; you can enter a DNS server of your choice if you like.
  • PersistentKeepalive = 25: Keeps the connection alive for clients behind NAT. It's useful on home and mobile networks.

Save and click the Activate button. If the connection succeeds, the status becomes "Active" and handshake information appears.

What Is AllowedIPs?

AllowedIPs is WireGuard's most commonly misunderstood setting. It actually does two jobs at once:

  • On the client side: It determines which destinations are sent through the tunnel (like a routing table).
  • On the server side: It determines from which source IP addresses packets from a peer are accepted.

If you set AllowedIPs = 0.0.0.0/0 on the client, all IPv4 traffic goes through the VPN. Websites see you with the server's IP address. This is full-tunnel use.

If you only want to access the devices on the WireGuard network and the server, you can use this value:

AllowedIPs = 10.66.66.0/24

In this case, only traffic going to the 10.66.66.0/24 network goes through the tunnel; your internet traffic keeps going out through your normal connection. This is called a split tunnel.

Note: 0.0.0.0/0 only covers IPv4 traffic. The configuration in this guide doesn't include IPv6 routing. Additional server configuration is needed to route IPv6 through the tunnel as well.

In the peer blocks on the server, the right approach is to give each client only its own VPN address (/32).

Testing the Connection

With the client active, open Command Prompt or PowerShell on Windows and ping the server's VPN address:

ping 10.66.66.1

If you get a reply, the tunnel is working. Verify the connection on the server side:

sudo wg show

The output should show the latest handshake and transfer lines for the peer. If the handshake information shows a few seconds to a few minutes ago, the connection is established.

If you use a full tunnel, also check that your outbound IP is the server's IP. In Windows PowerShell:

curl.exe -4 ifconfig.me

The returned address should be the same as your server's public IP. You can also run the same command with the tunnel off to see the difference.

The "Handshake but No Internet" Problem

If the handshake completes and pings to the VPN address work but you can't reach the internet, the problem is usually in the forwarding and NAT settings on the server. Check in this order:

  • IP forwarding may be off. The output of sysctl net.ipv4.ip_forward should be 1.
  • The wrong network interface name may have been entered. The -o eth0 part in wg0.conf must match the interface in the ip route output exactly.
  • NAT or FORWARD rules may be missing. You can see the rules with these commands.
sudo iptables -t nat -L POSTROUTING -n -v
sudo iptables -L FORWARD -n -v

In the NAT table, you should see a MASQUERADE rule for the 10.66.66.0/24 source. If not, the PostUp line didn't run. Restart the service and look at the error messages.

  • There may be a DNS problem. On the client, ping an IP address and then a domain name. If pinging the IP works but the domain name doesn't resolve, check the DNS line.
ping 1.1.1.1
ping example.com
  • There may be an MTU problem. If some sites open and others load only partially, try adding the line MTU = 1380 to the [Interface] section of the client configuration.

If you use UFW on the server and the forwarding policy has been tightened, traffic may be blocked in the FORWARD chain. In that case, review the sudo ufw status verbose output and the iptables rules together.

The "No Handshake" Problem

If the latest handshake line never appears for the peer in the sudo wg show output, the server and client can't reach each other. Possible causes:

  • 51820/UDP is closed. Even if it's open in UFW, it may be closed in the firewall in the provider's panel. The port that needs to be opened is UDP, not TCP.
  • The Endpoint is wrong. The Endpoint line on the client must contain the server's correct IP address and port.
  • The keys are mixed up. The client's PublicKey field must contain the server's public key, and the peer block on the server must contain the client's public key. Mixing them up with the private keys is a very common mistake.
  • The service isn't running. Check with sudo systemctl status wg-quick@wg0.
  • The client's network restricts UDP. Some corporate or school networks block 51820/UDP. You can tell by trying on a different network (for example a phone hotspot).

You can see whether the server is listening on the port with this command:

sudo ss -ulpn | grep 51820

To see whether packets reach the server, you can use tcpdump:

sudo apt install tcpdump -y
sudo tcpdump -ni any udp port 51820

If no packets appear in this command while you try to connect from the client, the problem is on the network, firewall or Endpoint side. If packets arrive but no handshake happens, check the keys again.

Adding Multiple Devices

Each device should have its own key pair and its own VPN IP address. Don't use the same configuration on more than one device; two devices connecting at the same time will knock each other's connection off.

Add a separate block for each device to the wg0.conf file on the server:

[Peer]
# Windows laptop
PublicKey = LAPTOP_PUBLIC_KEY
AllowedIPs = 10.66.66.2/32

[Peer]
# Phone
PublicKey = TELEFON_PUBLIC_KEY
AllowedIPs = 10.66.66.3/32

[Peer]
# Tablet
PublicKey = TABLET_PUBLIC_KEY
AllowedIPs = 10.66.66.4/32

For each device, the Address value in the client configuration must match the corresponding AllowedIPs value on the server (for example 10.66.66.3/32 for the phone). Adding a comment with # at the start of lines makes it easier to remember later which key belongs to which device.

After applying the change, don't forget to run sudo systemctl restart wg-quick@wg0 or the syncconf command.

WireGuard Security Recommendations

  • Don't share private keys. Server and client private keys are never shared under any circumstances. If you suspect a key has been compromised, create a new key pair and remove the old peer.
  • Restrict file permissions. wg0.conf and the key files should have 600 permissions so that only root can read them.
  • Give each device its own key. When you lose a device, you only need to delete its peer block.
  • Remove unused peers. Delete the access of devices you no longer use from the server.
  • Give peers on the server /32. Each client should only be able to use its own VPN address.
  • Keep unnecessary ports closed. Only open the ports you need in the firewall. For SSH, consider key-based login and disabling root login.
  • Keep the system up to date. Apply Ubuntu security updates regularly.

The general security of the server is beyond the scope of this guide; we'll cover server hardening in a separate article.

Do You Need a VDS for WireGuard?

To set up your own WireGuard server, you need an always-on Linux server with a public IPv4 address and root access. A home computer can technically do the job too; however, home internet usually has a changing IP address, port forwarding is needed behind the modem and some providers don't allow incoming connections. That's why a VDS is a more practical option for most users.

Here's what we can say about resource needs:

  • Because WireGuard runs at the kernel level, it generally uses little CPU and memory.
  • For personal use with a few devices, an entry-level VDS can be enough for most scenarios.
  • The real requirement depends on the number of connected users and the amount of traffic passing through. If you route all traffic through the tunnel, the server's bandwidth and monthly traffic limit become important.
  • The server's location directly affects connection latency.

Before buying, also check the virtualization type. Because WireGuard uses a kernel module, it may not work in some container-based virtualization environments. If you're not sure, confirm with the provider. Also make sure your VPN use complies with the provider's terms of use and applicable laws.

Conclusion

In this guide, we installed WireGuard on Ubuntu, created the server and client keys, configured NAT and firewall settings, connected a Windows client and looked at the causes of the two most common problems. Once the setup is done correctly, adding more devices takes just a few lines.

If you have problems, look at the sudo wg show output first: if there's no handshake, the problem is in the connection or the keys; if there's a handshake but no internet, it's in the NAT and forwarding settings.

If you want to set up your WireGuard server on your own VDS, you can explore Netiyo VDS plans.

Never Miss a Post

Get our latest tutorials, guides and product news in your inbox.

You're subscribed to our newsletter.