WireGuard VPN Setup: Step-by-Step on an Ubuntu Server (wg-quick)

Sunucun 4 min read

WireGuard is a fast, open source VPN protocol with a simple design that uses modern cryptography. In this guide, we explain how to install WireGuard with the official packages on a server running Ubuntu (for example a VDS) and connect a computer or phone to the internet through that server. The commands are based on WireGuard’s official installation page and the Ubuntu manual pages (wg, wg-quick).

Illustration: Ubuntu

Requirements

  • A server running Ubuntu (or Debian) with root/sudo access and a static public IP address
  • A UDP port that can be opened on the server (51820 in this guide)
  • The official WireGuard app for the device that will connect: Windows, macOS (App Store), Android (Google Play) or iOS (App Store)

1. Install WireGuard

WireGuard is available in the official Ubuntu and Debian repositories:

sudo apt update
sudo apt install wireguard

2. Generate the Keys

Each device has a private key and a public key. Generate the server keys with the one-line command from WireGuard’s quick start page:

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

You can generate the keys for the connecting device in the same way (for example client_private.key / client_public.key), or let the client app create them by choosing “add empty tunnel”. Never share private keys with anyone.

3. Server Configuration (wg0.conf)

For the command wg-quick up wg0, wg-quick first looks for the configuration in /etc/wireguard/wg0.conf. Create the file (sudo nano /etc/wireguard/wg0.conf) and fill it in with your own keys. Replace eth0 with the name of your server’s internet-facing network interface (shown in the “default … dev” part of the ip route output):

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
# The connecting device
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
  • Address: The server’s address inside the VPN.
  • AllowedIPs (on the server): Which source addresses are accepted for traffic from this device; give each device its own /32 address.
  • PostUp / PostDown: Commands that run when the tunnel comes up and goes down; here they let VPN traffic reach the internet through the server’s IP (NAT).

4. Enable IP Forwarding

For the server to forward traffic from the VPN to the internet, IPv4 forwarding must be enabled:

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

5. Open the Port in the Firewall

If you use UFW, allow the WireGuard port (make sure your SSH port is also allowed):

sudo ufw allow 51820/udp

If your server provider also has a network firewall in its panel, open UDP 51820 there as well.

6. Start the Tunnel

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

systemctl enable wg-quick@wg0 makes the tunnel start automatically when the server reboots. wg show displays the peers, the latest handshake time and the data transferred.

7. Client (Computer or Phone) Configuration

Create a new tunnel in the WireGuard app and enter this configuration:

[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP_ADDRESS:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
  • AllowedIPs = 0.0.0.0/0: Sends all IPv4 traffic through the tunnel; if you only want to reach the server’s internal network, write 10.8.0.0/24.
  • PersistentKeepalive = 25: According to the wg manual, this is an example value for keeping the connection open for devices behind NAT; most users do not need it.

On a phone, instead of typing this configuration by hand, you can turn it into a QR code on the server with the qrencode package and scan it from the app: qrencode -t ansiutf8 < client.conf

If the Connection Does Not Work

  • No “latest handshake” in wg show: UDP 51820 is closed, the Endpoint IP is wrong or the keys have been swapped (the server must have the client’s public key).
  • Handshake works but there is no internet: IP forwarding is off or the network interface name in the PostUp line is wrong.
  • Sites do not open but access by IP works: Check the DNS line on the client.

Adding More Devices

For each new device, generate a new key pair, add a new [Peer] section to the server’s wg0.conf with that device’s public key and its own address (for example AllowedIPs = 10.8.0.3/32), and use that address in the device’s Address line. Then apply the change with sudo systemctl restart wg-quick@wg0. Do not use the same key pair on two devices; they would conflict with each other.

Security Tips

  • Make sure only root can read the private key files (the umask 077 above takes care of this).
  • Delete the [Peer] section of devices you no longer use and restart the tunnel.
  • Update the server’s packages regularly: sudo apt update && sudo apt upgrade.

For your own WireGuard server, you can use a VDS server located in Türkiye with root access.

Categories

Latest guides