How to set up a WireGuard VPN on your own server
Install WireGuard (a modern open-source VPN) on an Ubuntu 24.04 server you control, create a key pair, write /etc/wireguard/wg0.conf, turn on IP forwarding, open UDP port 51820 in the firewall, and start it with systemctl enable --now wg-quick@wg0. Then add each person as a peer with their own key, and remove their peer the day they leave.
Who is this for?
- Small-business owners with remote staffYou’ll use it to give your team private access to office systems from a server you control.
- IT leads and admins comfortable with a Linux command lineYou’ll use it to set up WireGuard on Ubuntu 24.04 step by step, and remove a departed employee's access.
Who should self-host a VPN, and who shouldn't?
Self-hosting fits a small team with someone comfortable on a Linux command line who wants one private, fixed exit to the internet or a private path to a few servers. You own the server, the keys and the logs.
Don't self-host if nobody on the team will keep the server patched, if you need to prove compliance to auditors, or if you need per-person device checks and single sign-on. A commercial VPN for privacy, or a managed zero-trust tool (a service that checks each person's login and device before letting them reach a system) for company access, is the better choice. NIST SP 800-46 and CISA's Zero Trust Maturity Model describe what managed remote access should cover.
What do you need before you start?
A server you own or rent running Ubuntu 24.04 LTS, with a public IP address and SSH access as a user who can run sudo. Your hosting provider's firewall must let UDP port 51820 in (check its control panel as well as the server's own firewall).
A list of the people and devices that will connect, so each gets its own key and its own address. Never share one key between people: you couldn't remove one person without removing everyone.
Placeholders in the commands are written in capitals inside angle brackets, like <SERVER_PUBLIC_IP>. Replace the whole thing, brackets included.
How do you set it up, step by step?
Install WireGuard
Update the package list and install WireGuard from Ubuntu's own repository. This installs the wg and wg-quick commands; the WireGuard module is already part of Ubuntu's kernel.
sudo apt update sudo apt install -y wireguardGenerate the server's keys
Create a private key and its public key in /etc/wireguard, readable only by root. The private key never leaves this server; the public key goes into every client's config.
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub' sudo cat /etc/wireguard/server.pubFind the server's internet-facing interface
The NAT rule in the next step needs the name of the network interface that reaches the internet. This prints it (often eth0 or ens3).
ip -o -4 route show to default | awk '{print $5}'Write /etc/wireguard/wg0.conf
Copy the block into a text editor first and replace <SERVER_PRIVATE_KEY> with the output of sudo cat /etc/wireguard/server.key and <WAN_INTERFACE> with the name from the last step, then paste it into the server's terminal to write the file. The PostUp and PostDown lines turn on NAT (masquerade: the server's address stands in for the VPN addresses on the internet) while the VPN is up.
sudo tee /etc/wireguard/wg0.conf > /dev/null <<'EOF' [Interface] Address = 10.8.0.1/24 ListenPort = 51820 PrivateKey = <SERVER_PRIVATE_KEY> PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o <WAN_INTERFACE> -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o <WAN_INTERFACE> -j MASQUERADE EOF sudo chmod 600 /etc/wireguard/wg0.confTurn on IP forwarding
Let the server pass traffic from VPN clients on to the internet, now and after every reboot.
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf sudo sysctl --systemOpen the firewall for SSH and the WireGuard port
Allow SSH first so you don't lock yourself out, then UDP port 51820, then turn the firewall on. Open only these ports.
sudo ufw allow OpenSSH sudo ufw allow 51820/udp sudo ufw enable sudo ufw statusStart WireGuard and keep it on after reboots
Start the wg0 interface now and at every boot, then check that it is listening.
sudo systemctl enable --now wg-quick@wg0 sudo wg showAdd a person (a peer)
Make a key pair for each device. The free WireGuard config generator does this in your browser, or run the commands below on the server. Give each person the next free address (10.8.0.2, 10.8.0.3 and so on). Append a [Peer] block to wg0.conf and load it without dropping anyone's connection.
sudo sh -c 'umask 077; wg genkey | tee /etc/wireguard/<NAME>.key | wg pubkey > /etc/wireguard/<NAME>.pub' sudo cat /etc/wireguard/<NAME>.pub sudo tee -a /etc/wireguard/wg0.conf > /dev/null <<'EOF' # <NAME> [Peer] PublicKey = <CLIENT_PUBLIC_KEY> AllowedIPs = 10.8.0.2/32 EOF sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'Set up the client on Windows, macOS, iOS or Android
Install the official WireGuard app (wireguard.com/install for Windows; the App Store for macOS and iOS; Google Play for Android). The commands below write the client's config to <NAME>.conf (replace the placeholders first; sudo cat /etc/wireguard/<NAME>.key prints the client's private key), show it as a QR code for a phone, then delete the copies from the server. On a computer, import the file in the app instead.
umask 077 cat > <NAME>.conf <<'EOF' [Interface] PrivateKey = <CLIENT_PRIVATE_KEY> Address = 10.8.0.2/32 DNS = 1.1.1.1 [Peer] PublicKey = <SERVER_PUBLIC_KEY> Endpoint = <SERVER_PUBLIC_IP>:51820 AllowedIPs = 0.0.0.0/0, ::/0 PersistentKeepalive = 25 EOF sudo apt install -y qrencode qrencode -t ansiutf8 < <NAME>.conf rm <NAME>.conf sudo rm /etc/wireguard/<NAME>.keyCheck that it works
Turn the tunnel on in the app. On the server, a recent "latest handshake" means the client connected. On the client, a what-is-my-IP site should now show the server's address, and a DNS leak test should show only the DNS server you set.
sudo wg showKeep the server patched
Turn on Ubuntu's automatic security updates so fixes to the kernel, WireGuard and SSH install without waiting for you. Reboot when a kernel update asks for it.
sudo apt install -y unattended-upgrades sudo dpkg-reconfigure -plow unattended-upgradesRotate keys
WireGuard keys don't expire. Replace a key whenever a device is lost, sold or might have been copied, and on a schedule you set: make a new pair, put the new public key in the peer's [Peer] block, give the person their new config, and reload.
sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'Remove a departed employee's access
Do this on their last day, as part of offboarding. Remove the peer from the running interface, delete its [Peer] block from wg0.conf so it doesn't come back after a restart, and check it is gone.
sudo wg set wg0 peer <CLIENT_PUBLIC_KEY> remove sudo nano /etc/wireguard/wg0.conf # delete that person's [Peer] block sudo wg show
Safety and scope
Run this only on a server you own or rent. Keep every private key private: anyone with a client's private key can connect as that person. Open only SSH and UDP 51820. If you route all of a person's traffic through your server, you are responsible for what leaves it, so set rules for what the VPN may be used for.
Questions
- Is WireGuard free?
- Yes. WireGuard is free, open-source software, and it is included in the Linux kernel. You pay only for the server you run it on.
- Which port does WireGuard use?
- Whatever ListenPort you set in wg0.conf. 51820 over UDP is the usual choice. Open that UDP port in the server's firewall and in your hosting provider's firewall.
- How do I remove someone's VPN access when they leave?
- Run sudo wg set wg0 peer <CLIENT_PUBLIC_KEY> remove, delete their [Peer] block from /etc/wireguard/wg0.conf, and check with sudo wg show. Do it on their last day.
- Should a small business self-host its VPN?
- Only if someone will keep the server patched and the keys organized. If not, a commercial VPN or a managed zero-trust tool is safer.
Related
Sources
- WireGuard: Quick Start
- wg(8) manual page
- wg-quick(8) manual page
- Ubuntu Server documentation: WireGuard VPN
- WireGuard: Installation (official apps)
- Ubuntu Server documentation: automatic updates
- NIST SP 800-46 Rev. 2: Guide to Enterprise Telework, Remote Access, and BYOD Security
- CISA: Zero Trust Maturity Model
This page is general information, not legal advice. Rules on monitoring, data retention and network use differ by country and state; check yours with a lawyer.
For your own systems and lawful use only. Don't use these guides or tools on servers, networks or accounts you don't own or aren't authorized to run.
Want us to set it up for you? See Secure Remote Access for HR Systems.