WireGuard on a Linux VPS gives you a small, modern VPN tunnel you can run with a few configuration files and a UDP port. This guide covers a typical setup: one VPS as the server, one or more peers as clients, key generation, forwarding, firewall rules and a first connectivity check. It is a how-to, not a provider comparison, and it does not require a commercial VPN service.
Work from a sudo account, keep a console session available, and do not paste private keys into tickets, chat or screenshots. The examples use Ubuntu or Debian with wg-quick. The same configuration style works on AlmaLinux or Rocky Linux once the wireguard-tools package is installed.
What you need before you start
You need a Linux VPS with a public IPv4 address, SSH or console access, and a UDP port you can open. The usual default is 51820/udp, but any unused UDP port works if the firewall and both ends agree. You also need a client device, such as a laptop or a phone, that can install a WireGuard client.
A clean baseline helps. Update the VPS, confirm SSH still works, and make sure you can recover access if a firewall typo goes wrong. The VPS setup guide and the VPS security checklist cover that first-week work. If SSH starts refusing connections after a firewall change, use the provider console and the SSH connection refused guide before you touch WireGuard again.
Decide what the tunnel is for. A simple site-to-site or laptop-to-VPS tunnel only needs a private address on each peer. If clients should reach the internet through the VPS, you also need IP forwarding and a masquerade rule on the VPS. Those are extra steps, not the default of a working tunnel.
Step 1: install WireGuard
On Ubuntu or Debian:
sudo apt update
sudo apt install wireguard wireguard-tools -y
sudo sysctl net.ipv4.ip_forward
On AlmaLinux or Rocky Linux, install wireguard-tools from the normal repositories for your release, then confirm wg and wg-quick are on the PATH.
Modern kernels already include the WireGuard module. If modprobe wireguard fails, you are probably on a custom or very old kernel and should fix that before continuing. Check the official WireGuard installation notes for your distribution if the package names differ.
Step 2: generate keys on the VPS
Create a directory with tight permissions, then generate a server key pair:
sudo mkdir -p /etc/wireguard
sudo umask 077
cd /etc/wireguard
sudo wg genkey | sudo tee server.key | sudo wg pubkey | sudo tee server.pub
sudo chmod 600 /etc/wireguard/server.key
The private key stays on this VPS. The public key is what you will place in each client configuration. Repeat the same idea on the client, or generate the client pair on the VPS and copy only the client config to the device, never the server private key.
A convenient one-off client pair on the VPS looks like this:
wg genkey | tee client1.key | wg pubkey > client1.pub
Move those files out of your home directory when you are done, or delete them after the values are in the config files. Private keys are not passwords you can reset later. If one leaks, generate a new pair and update the peer list.
Step 3: create the server configuration
Pick a tunnel subnet that does not collide with the client’s LAN. 10.8.0.0/24 is a common choice. Edit /etc/wireguard/wg0.conf as root:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32
Replace the key placeholders with the real values, without quotes or extra spaces. Each additional client gets its own [Peer] block and a unique tunnel IP in AllowedIPs, for example 10.8.0.3/32.
AllowedIPs is both a routing table and an access list. On the server, list only the tunnel addresses you expect from that client. On the client, list the networks you want to send into the tunnel. A typo here is the usual reason a handshake succeeds but no packets flow.
Set the file mode after saving:
sudo chmod 600 /etc/wireguard/wg0.conf
Step 4: open the UDP port and start the interface
Allow the listen port on the host firewall. With UFW:
sudo ufw allow 51820/udp
sudo ufw reload
sudo ufw status verbose
Also allow the same UDP port on any provider firewall or security group in the control panel. The port ping guide is useful for TCP services, but WireGuard is UDP, so a TCP probe to 51820 will not prove the tunnel port is open.
Start the interface and enable it at boot:
sudo systemctl enable --now wg-quick@wg0
sudo wg show
ip addr show wg0
wg show should list the listening port, the server public key and each configured peer. A handshake timestamp appears only after a client connects. If the service fails, run sudo journalctl -u wg-quick@wg0 -e --no-pager and check for a key typo, a port already in use, or a missing interface name.
Step 5: configure a client
A laptop configuration typically looks like this:
[Interface]
Address = 10.8.0.2/24
PrivateKey = CLIENT_PRIVATE_KEY
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = VPS_PUBLIC_IP:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25
Use the VPS public IP, or a hostname that already resolves to that IP. PersistentKeepalive = 25 helps when the client sits behind NAT, which is the usual home or mobile case. It is not a performance feature, and you do not need it on the server for this design.
If the client should send all traffic through the VPS, set AllowedIPs = 0.0.0.0/0 on the client, then add forwarding and masquerade on the VPS. If you only need access to services on the VPS or to other peers, keep AllowedIPs limited to 10.8.0.0/24.
Import the file into the official client, or save it as wg0.conf and run sudo wg-quick up wg0 on a Linux client. The official quick start is on the WireGuard site.
Step 6: enable forwarding only if the VPS is an exit node
Skip this section if clients only need to reach the VPS itself or other tunnel peers. If clients should use the VPS as their path to the internet, enable forwarding and masquerade.
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard-forward.conf
sudo sysctl --system
Then add a NAT rule on the VPS public interface. The interface name may be eth0, ens3 or something else; check with ip route and use the device that has the default route:
ip route show default
A direct nftables or iptables masquerade works, and some Ubuntu hosts add it under UFW’s before.rules. The important part is that packets leaving the VPS toward the internet use the VPS public address, and that the firewall allows traffic forwarded from wg0. Test one client before you add a second. A wrong NAT rule can break ordinary SSH if you edit the wrong chain, so keep the console open.
IPv6 forwarding is a separate decision. Only enable it if you have a routed IPv6 prefix and you have written matching addresses, firewall rules and peer AllowedIPs.
Step 7: test the tunnel
From the client, bring the interface up and ping the VPS tunnel address:
ping -c 4 10.8.0.1
sudo wg show
On the VPS, sudo wg show should now show a recent handshake and a rising transfer counter. If the handshake never appears, the UDP port is usually blocked, the endpoint IP or port is wrong, or the public and private keys are mismatched. If the handshake appears but ping fails, look at AllowedIPs, the interface addresses, and forwarding rules.
Then test an actual service on the VPS over the tunnel IP, for example SSH to 10.8.0.1. That confirms you can administer the box through WireGuard later, which is useful if you later restrict SSH to the tunnel. Do not cut public SSH until the tunnel has survived a reboot:
sudo systemctl restart wg-quick@wg0
sudo wg show
# from the client, ping again after the restart
Common setup failures
If wg show never gets a handshake, work through the path in order. Confirm the client Endpoint uses the current VPS public IP and the same UDP port as ListenPort. Confirm the server public key in the client file matches sudo wg show on the VPS, and that the client public key in wg0.conf matches the client. Then check the provider firewall as well as UFW or firewalld. A rule that allows TCP 51820 will not help.
If the handshake appears and ping still fails, print both configs and read AllowedIPs out loud. A server peer that lists 0.0.0.0/0 for a single laptop, or a client that forgets 10.8.0.1/32 or 10.8.0.0/24, will drop the packets after they arrive. Check ip addr show wg0 on both ends so the tunnel addresses match the files you think you saved.
Operational habits that keep the tunnel boring
Back up /etc/wireguard/ with the same care you give to SSH keys. After a VPS rebuild, new keys mean every client must be updated. A snapshot is not a substitute for a copy you can read offline.
Remove peers you no longer use. Rotate a key if a laptop is lost. Watch disk and CPU like any other service; WireGuard is light, but a misconfigured client that tries to send unexpected networks can still surprise you. The Linux monitoring guide is enough for a first pass.
Do not run this as a way to hide an open, unpatched VPS. The tunnel is only as safe as the keys, the peer list and the services you expose inside it.
Frequently asked questions
Can I use a hostname instead of the VPS IP?
Yes, in the client Endpoint field, if the hostname already points at the current public IP. After a VPS rebuild the IP may change, so update DNS or the client config before you assume the old name still works.
Does WireGuard replace SSH hardening?
No. It can reduce how much of the SSH port is exposed, but you still want key-based SSH, a firewall and updates. WireGuard is an extra path, not a full server security program.
Why does the handshake work and DNS fail when I route all traffic?
The client is probably sending DNS through the tunnel, and the VPS is not forwarding or allowing that traffic. Test with an IP address first, then fix forwarding, masquerade and the client DNS setting. A broken DNS line will look like a “dead VPN” even when ping to 10.8.0.1 works.
Can several clients share one private key?
Do not do that. Each peer should have its own key pair and its own tunnel IP. Shared keys make it hard to revoke one device and can cause confusing routing when two clients come online at once.
Which UDP port should I use?
51820 is fine if you can open it. A different unused UDP port is reasonable when a network blocks 51820, as long as the server ListenPort, the client Endpoint and every firewall use the same value.