Creating WireGuard VPN Server

Most of these article contents are AI-assisted (Opencode with DeepSeek V4.1 Flash Free) and verified against the official WireGuard documentation.
About wireguard
WireGuard is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN.1
Unlike OpenVPN, WireGuard has no certificates, no TLS handshake, and no long configuration files. Each peer is identified by a key pair, and the whole tunnel fits in a short .conf file. On Linux it runs as a kernel module, so it is also light on CPU.
WireGuard has no real “server” or “client” role — every side is a peer. In this article, “server” means the peer with a public IP that others connect to, and “client” means the peer behind NAT.
Scenario
| Role | OS | Public IP | Tunnel IP | Port Forwarding |
|---|---|---|---|---|
| Server | Ubuntu 24 | 103.190.0.22 | 192.168.8.1/24 | 34320 -> 51822 |
| Client | Arch Linux | dynamic | 192.168.8.2/32 | - |

The server routes the client’s internet traffic (full tunnel). Replace the IPs, interface name, and port with your own values.
Installation
sudo apt updatesudo apt install wireguardsudo pacman -S wireguard-toolsOn most modern kernels the wireguard module is already included, so wireguard-tools is all you need for the wg and wg-quick commands.2
If a config uses DNS = (recommended for clients), wg-quick needs a resolvconf provider to apply it. On Arch, install openresolv before starting the tunnel:
sudo pacman -S openresolvServer setup
1. Generate the key pair
umask 077wg genkey | tee server-private.key | wg pubkey > server-public.keyumask 077 makes sure only the owner can read the private key. The official quick start uses the same pattern.3
2. Create /etc/wireguard/wg0.conf
[Interface]Address = 192.168.8.1/24ListenPort = 51822PrivateKey = <SERVER_PRIVATE_KEY>
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEPostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]# client1PublicKey = <CLIENT_PUBLIC_KEY>AllowedIPs = 192.168.8.2/32
Then restrict the file:
sudo chmod 600 /etc/wireguard/wg0.confeth0 is the internet-facing interface of the server, not the tunnel. Check yours with:
ip route get 1.1.1.1The PostUp/PostDown rules above use iptables. If your server only ships nftables, translate them or install iptables first.
3. Enable IP forwarding
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.confsudo sysctl --systemWithout forwarding, clients can reach the server but not the internet through it.
4. Open the UDP port
If you use UFW:
sudo ufw allow 51822/udpIf you use a cloud provider (AWS, GCP, Oracle, etc.), also allow 51820/udp in its security group or firewall panel. This is the most common reason a tunnel never handshakes.
5. Start the tunnel
sudo systemctl enable --now wg-quick@wg0
The systemd unit brings the interface up and starts it on boot. For manual control, use sudo wg-quick up wg0 and sudo wg-quick down wg0 instead.
Client setup
1. Generate the client keys
Generate them on the client so the private key never leaves the device:
umask 077wg genkey | tee client-private.key | wg pubkey > client-public.keycat client-public.keyCopy the printed public key into the server’s [Peer] section.
2. Create /etc/wireguard/wg0.conf
[Interface]Address = 192.168.8.2/32PrivateKey = <CLIENT_PRIVATE_KEY>DNS = 1.1.1.1
[Peer]PublicKey = <SERVER_PUBLIC_KEY>Endpoint = 203.0.113.10:51820AllowedIPs = 0.0.0.0/0, ::/0PersistentKeepalive = 25DNS = makes wg-quick call resolvconf after bringing the interface up. If that command is missing, install openresolv first as described in Installation.
3. Start the tunnel
sudo systemctl enable --now wg-quick@wg0
After editing the config later, apply the changes with sudo systemctl restart wg-quick@wg0.
To route only the VPN subnet and keep normal traffic outside the tunnel, change AllowedIPs to:
AllowedIPs = 192.168.8.0/24PersistentKeepalive = 25 keeps the NAT mapping alive when the client is behind a router. You do not need it on a server with a public IP.
Verify the tunnel
On the server:
sudo wg showYou should see a peer entry with a latest handshake line. Then test from the client:
ping -c 3 192.168.8.1curl ifconfig.mecurl should return the server’s public IP (103.190.0.22), which means the traffic is going through the tunnel. If the client’s wg show has no handshake, check Troubleshooting.

Adding more peers
Repeat the client key generation, add one [Peer] block per device on the server, and give each one a unique /32 address:
[Peer]# laptopPublicKey = <LAPTOP_PUBLIC_KEY>AllowedIPs = 192.168.8.3/32Apply the new config without dropping existing connections:
sudo wg syncconf wg0 <(sudo wg-quick strip wg0)wg syncconf only reloads the configuration; it does not create or remove the interface.
Optional: use an installer script
If you do not want to configure everything manually, angristan/wireguard-install automates key generation, NAT, and client creation with an interactive menu:
curl -O https://raw.githubusercontent.com/angristan/wireguard-install/master/wireguard-install.shchmod +x wireguard-install.shsudo ./wireguard-install.shThe result is the same as the manual setup above, just faster.
Troubleshooting
- No handshake: the UDP port is blocked, the
Endpointis wrong, or the server is not running. Checksudo wg showon both sides and confirm the port withsudo ss -ulnp | grep 51820. - Connected but no internet: IP forwarding is disabled, the
PostUpNAT rule points to the wrong interface, or the provider firewall blocks forwarding. - Some websites hang or time out: MTU is too high. Add
MTU = 1380under[Interface]and restart the tunnel. - DNS leaks: set
DNS =in the client’s[Interface]section.wg-quickwrites it toresolv.confautomatically. resolvconf: command not found(systemd exit status 127): the client has noresolvconfprovider.wg-quickcreates the interface, fails on the DNS step, then deleteswg0again, so the tunnel never comes up. Installopenresolv(sudo pacman -S openresolv) orsystemd-resolvconf, then runsudo systemctl restart wg-quick@wg0.wg0 already exists: the interface is already up. Runsudo wg-quick down wg0before starting the systemd unit.- IPv6 errors: if your server has no IPv6, remove
::/0fromAllowedIPs.
- Keep private keys at
chmod 600and never commit them to a repository. - A
PresharedKeyis optional, but adds a symmetric layer for post-quantum resistance. Generate one per peer pair withwg genpsk. - WireGuard is silent by default: a closed port looks like no service at all. That is normal, not a bug.
Footnotes
Support & Share
If this article helped you, please share or support!












