Skip to main content

Web servers and networking · Intermediate · 18 min

Reach a private server network from your laptop with WireGuard

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · WireGuard tools 1.0.20250521 · nftables 1.1.6

You will turn one Ubuntu 26.04 server into a WireGuard gateway that lets your laptop reach a private network over a split tunnel and sends your phone's traffic through a full tunnel, and you will know how to cut off a lost device.

On this page

Your database, internal dashboards and staging servers live on a private network with no public addresses, which is how they should be. The awkward part is getting to them yourself: from home, from a client's office, from a phone on mobile data. Opening port 5432 to the internet is not the answer, and jumping through SSH tunnels for every tool gets old quickly.

A WireGuard gateway fixes this. One server that sits on both the internet and the private network runs WireGuard. Your laptop and phone each get a key and a small config file. When the tunnel is up, 10.10.0.21 works from your laptop as if you were plugged into the same switch, and the only thing exposed to the internet is one UDP port that ignores every packet not signed by a key you issued.

This tutorial is about you reaching servers. If you want two servers to talk to each other over a tunnel, that is a site-to-site setup, covered in /tutorials/connect-two-servers-with-a-wireguard-vpn.

Diagram: a laptop with WireGuard address 10.66.0.2 and a phone with 10.66.0.3 connect across the internet on UDP 51820 to gateway gw1, which has eth0 203.0.113.20, wg0 10.66.0.1/24 and eth1 10.10.0.5, and forwards to db1 10.10.0.21, app1 10.10.0.22 and dns1 10.10.0.2 on the private network
The gateway forwards tunnel traffic to the private network and masquerades it as its own private address.

The examples use these values. Replace each one with yours:

| What | Example | Where yours comes from |
|---|---|---|
| Gateway public interface and address | eth0, 203.0.113.20 | ip -br addr on the gateway |
| Gateway private interface and address | eth1, 10.10.0.5 | ip -br addr on the gateway |
| Private network | 10.10.0.0/24 | Your provider's private network or VPC settings |
| Internal DNS server, if you have one | 10.10.0.2 | Your network admin, or leave it out |
| WireGuard network | 10.66.0.0/24 | Pick any private range that clashes with nothing you use |
| WireGuard port | 51820/udp | Any free UDP port |

Pick the WireGuard network carefully. Home and office routers often use 192.168.0.0/24, 192.168.1.0/24 or 10.0.0.0/24; if your tunnel or private network uses the same range as the Wi-Fi you are sitting on, the laptop cannot tell which one you mean.

Every command block below says which machine it runs on. Most run on the gateway, gw1.

Step 1 · Install WireGuard and create the gateway's keys

The WireGuard code itself is part of the Linux kernel on Ubuntu 26.04. What you install is the userland: wg for keys and status, and wg-quick for bringing an interface up from a config file. You also need qrencode for the phone, and nftables for the forwarding rules (Ubuntu Server usually has it already).

On gw1:

Bash
sudo apt update
sudo apt install -y wireguard-tools qrencode nftables
Bash
wg --version

Now make the gateway's key pair. A WireGuard private key is the whole of its identity: anyone who holds it can be the gateway. Create the file private from the first byte, with umask 077 inside the same root shell that writes it:

Bash
sudo install -d -m 700 /etc/wireguard
sudo sh -c 'umask 077; wg genkey > /etc/wireguard/server.key'
sudo sh -c 'wg pubkey < /etc/wireguard/server.key > /etc/wireguard/server.pub'
sudo ls -l /etc/wireguard
sudo cat /etc/wireguard/server.pub

Check it worked

wg --version prints something like wireguard-tools v1.0.20250521. The listing shows server.key as -rw------- owned by root, and server.pub holds one line of 44 characters ending in =. That public key is safe to share; it goes into every device's config.

Step 2 · Let the gateway forward packets

By default Linux drops packets that arrive on one interface and are meant for another machine. A gateway exists to pass them on, so turn on IPv4 forwarding in a drop-in file that survives reboots.

On gw1:

Bash
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-wireguard-gateway.conf

Load it now, and read the value back:

Bash
sudo sysctl --system
sysctl net.ipv4.ip_forward

The gateway in this tutorial forwards IPv4 only. The tunnel addresses are IPv4, and the private network is IPv4, so there is nothing to forward over IPv6. To see where other kernel settings come from and how drop-ins are ordered, read /tutorials/tune-kernel-parameters-with-sysctl.

Check it worked

sysctl --system lists /etc/sysctl.d/99-wireguard-gateway.conf among the files it applied, and the second command prints net.ipv4.ip_forward = 1. If it prints 0, a file that sorts later in /etc/sysctl.d or /usr/lib/sysctl.d sets it back; sudo grep -r ip_forward /etc/sysctl.d /usr/lib/sysctl.d finds it.

Step 3 · Write the forwarding and masquerade rules

Forwarding on its own would let any device on the tunnel reach anything the gateway can reach, including the internet. These rules narrow that down, and they masquerade tunnel traffic so the private servers see it coming from the gateway's private address, 10.10.0.5. That means db1 and the others need no new route back to 10.66.0.0/24: they reply to the gateway, and the gateway hands the reply to the right device.

The trade-off is that logs on db1 show 10.10.0.5, not which device connected. If you need the device addresses in those logs, drop the eth1 masquerade and add a route for 10.66.0.0/24 via 10.10.0.5 on your private router instead.

The rules live in their own table, wg_gateway, in a file that wg-quick loads when the tunnel comes up and removes when it goes down. That keeps them away from your main firewall file. Replace eth0 and eth1 with your interface names.

On gw1:

Bash
sudo tee /etc/wireguard/wg-gateway.nft > /dev/null <<'EOF'
#!/usr/sbin/nft -f
# Loaded by wg-quick PostUp, removed by PostDown.
table inet wg_gateway
delete table inet wg_gateway

table inet wg_gateway {
    # Devices allowed to use the gateway as their way to the internet.
    set full_tunnel {
        type ipv4_addr
        elements = { 10.66.0.3 }
    }

    chain forward {
        type filter hook forward priority filter; policy accept;

        # Tunnel devices may reach the private network.
        iifname "wg0" oifname "eth1" ip saddr 10.66.0.0/24 ip daddr 10.10.0.0/24 accept
        # Only full-tunnel devices may reach the internet through the gateway.
        iifname "wg0" oifname "eth0" ip saddr @full_tunnel accept
        # Anything else from the tunnel, including device to device, stops here.
        iifname "wg0" drop

        # Into the tunnel: replies only, nothing new from outside.
        oifname "wg0" ct state established,related accept
        oifname "wg0" drop
    }

    chain postrouting {
        type nat hook postrouting priority srcnat; policy accept;
        ip saddr 10.66.0.0/24 oifname { "eth0", "eth1" } masquerade
    }
}
EOF

What the parts do:

  • The table then delete table pair at the top makes the file safe to load twice: the first line creates an empty table if there is none, the second removes it, and the definition below starts clean.
  • policy accept on the forward chain means traffic that has nothing to do with wg0, such as Docker's, is left to whatever other rules you have.
  • full_tunnel holds the addresses allowed out to the internet. The laptop, 10.66.0.2, is not in it, so even if someone edits its config to send everything through the tunnel, the gateway drops its internet traffic.
  • masquerade rewrites the source address to that of the outgoing interface.

Check the file before it goes near the kernel:

Bash
sudo nft -c -f /etc/wireguard/wg-gateway.nft

If your server already runs a firewall with a forward chain whose policy is drop, such as UFW, remember that nftables runs every base chain on a hook: an accept here does not override a drop there. Step 5 shows the UFW rules you need.

Check it worked

nft -c prints nothing and returns to the prompt. A typo gets an error naming the file, line and column, with a caret under the problem.

Step 4 · Create keys for the laptop and the phone

Each device gets its own key pair, so you can revoke one without touching the others. Make them on the gateway in a private folder in your home directory, owned by you rather than root. You delete the private keys from the gateway once the devices have their configs.

On gw1:

Bash
mkdir -m 700 ~/wg-clients
cd ~/wg-clients
(umask 077; for device in laptop phone; do wg genkey > "$device.key"; wg pubkey < "$device.key" > "$device.pub"; done)
sudo cat /etc/wireguard/server.pub > ~/wg-clients/server.pub
ls -l ~/wg-clients

The ( ... ) runs the loop in a subshell, so umask 077 applies to these files and does not stay set in your terminal.

If you would rather a private key never touches the gateway, the macOS and Windows apps can make a key pair for you when you add an empty tunnel; copy the public key they show into ~/wg-clients/laptop.pub instead.

Check it worked

The listing shows five files: laptop.key, laptop.pub, phone.key, phone.pub and server.pub. The two .key files are -rw-------. Running cat ~/wg-clients/laptop.pub and cat ~/wg-clients/phone.pub prints two different 44-character keys.

Step 5 · Write the gateway's wg0 config and start it

The gateway's config has one [Interface] section for itself and one [Peer] section per device. On the gateway, a peer's AllowedIPs is an access list: packets arriving from the laptop's key are accepted only if their source is 10.66.0.2, and packets for 10.66.0.2 are sent only to the laptop's key. Give each device a single /32 address and no two devices the same one.

The first command below empties any file already at /etc/wireguard/wg0.conf. Run sudo ls /etc/wireguard first: if wg0.conf is already there, for example from /tutorials/connect-two-servers-with-a-wireguard-vpn, pick another interface name such as wg1 and use it everywhere this tutorial says wg0: the config file name, the wg-quick@ unit, the iifname and oifname matches in wg-gateway.nft, and the UFW on wg0 rules.

The command creates the file private first, then writes it. The heredoc is unquoted on purpose, so the shell fills in the keys from the files; the keys go through tee's input and never appear on a command line.

On gw1:

Bash
sudo install -m 600 /dev/null /etc/wireguard/wg0.conf
sudo tee /etc/wireguard/wg0.conf > /dev/null <<EOF
[Interface]
# gw1: the gateway
Address = 10.66.0.1/24
ListenPort = 51820
PrivateKey = $(sudo cat /etc/wireguard/server.key)
PostUp = nft -f /etc/wireguard/wg-gateway.nft
PostDown = nft delete table inet wg_gateway

# laptop
[Peer]
PublicKey = $(cat ~/wg-clients/laptop.pub)
AllowedIPs = 10.66.0.2/32

# phone
[Peer]
PublicKey = $(cat ~/wg-clients/phone.pub)
AllowedIPs = 10.66.0.3/32
EOF

Keep the blank line between sections and the # name comment directly above each [Peer]; the revoke step in this tutorial relies on that layout.

Ask wg-quick to parse the file and print what it would hand to the kernel:

Bash
sudo ls -l /etc/wireguard/wg0.conf
sudo wg-quick strip wg0

Now open the port. Clients connect to the gateway, never the other way round, so the gateway needs one UDP port open and no Endpoint lines. If you use UFW, allow the port and the two kinds of forwarded traffic; UFW drops forwarded packets unless a route rule allows them:

Bash
sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on eth1 to 10.10.0.0/24
sudo ufw route allow in on wg0 out on eth0 from 10.66.0.3

If you wrote your own nftables firewall instead, add udp dport 51820 accept to its input chain. Open 51820/udp in your provider's cloud firewall or security group as well, if it has one.

Start the tunnel and make it come back after a reboot:

Bash
sudo systemctl enable --now wg-quick@wg0
sudo wg show
sudo nft list table inet wg_gateway

Check it worked

wg-quick strip prints the [Interface] section with only ListenPort and PrivateKey, then the two [Peer] sections: the Address, PostUp and PostDown lines are gone because they are wg-quick's own, and the rest is what the kernel receives. The file is -rw-------.

After starting it, sudo wg show prints interface: wg0 with listening port: 51820 and two peer: entries with their allowed ips, but no latest handshake yet, because no device has connected. nft list table inet wg_gateway prints your rules, which proves PostUp ran.

Step 6 · Write the laptop and phone configs

A device's config mirrors the gateway's: its own private key and address, and one [Peer], the gateway, with the Endpoint to dial. Here AllowedIPs works as a routing table: whatever it lists goes into the tunnel, and everything else uses the device's normal connection.

Diagram comparing a split tunnel and a full tunnel. On the laptop, AllowedIPs 10.66.0.0/24 and 10.10.0.0/24 send db1 and the gateway through the tunnel while video calls and websites use the local network. On the phone, AllowedIPs 0.0.0.0/0 and ::/0 send db1, DNS lookups and all other traffic through the tunnel
Split tunnel for reaching servers, full tunnel for untrusted networks.

The laptop gets a split tunnel: only the WireGuard network and the private network go through it. Your video calls and downloads keep using your own connection, which is faster and puts no load on the gateway. The DNS line points at an internal resolver and adds example.internal as a search domain, so db1 resolves to db1.example.internal. Replace both with yours, or delete the line if you have no internal DNS; every lookup the laptop makes goes to that server while the tunnel is up.

On gw1:

Bash
cd ~/wg-clients
install -m 600 /dev/null laptop.conf
cat > laptop.conf <<EOF
[Interface]
PrivateKey = $(cat laptop.key)
Address = 10.66.0.2/32
DNS = 10.10.0.2, example.internal

[Peer]
PublicKey = $(cat server.pub)
Endpoint = 203.0.113.20:51820
AllowedIPs = 10.66.0.0/24, 10.10.0.0/24
PersistentKeepalive = 25
EOF

The phone gets a full tunnel: 0.0.0.0/0 sends all IPv4 traffic through the gateway, which is what you want on hotel or airport Wi-Fi. ::/0 sends IPv6 into the tunnel too, where the gateway drops it, so nothing slips out around the tunnel; apps fall back to IPv4. A full tunnel needs a DNS line, or the phone keeps asking its mobile network's resolver, which may refuse queries that arrive from the gateway's address.

Bash
cd ~/wg-clients
install -m 600 /dev/null phone.conf
cat > phone.conf <<EOF
[Interface]
PrivateKey = $(cat phone.key)
Address = 10.66.0.3/32
DNS = 1.1.1.1, 9.9.9.9

[Peer]
PublicKey = $(cat server.pub)
Endpoint = 203.0.113.20:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF

PersistentKeepalive = 25 sends a tiny packet every 25 seconds so the NAT on your router or mobile network keeps the path open, and an idle SSH session does not freeze. On prepaid mobile data that is about 200 KB a day while the tunnel is on. Remember that in a full tunnel every page and video goes through the gateway, so it still counts against your bundle, and it also counts against the gateway's own bandwidth allowance with your provider. If data is tight, give the phone the split tunnel's AllowedIPs too and remove 10.66.0.3 from the full_tunnel set.

Check both files parse:

Bash
ls -l ~/wg-clients/*.conf
sudo wg-quick strip ~/wg-clients/laptop.conf
sudo wg-quick strip ~/wg-clients/phone.conf

Check it worked

Both files are -rw-------. Each strip prints a [Peer] with Endpoint = 203.0.113.20:51820 and the AllowedIPs you chose, and no Address or DNS line. wg-quick complains with the line it cannot read if a key or address is malformed.

Step 7 · Put the configs on the devices and test the tunnel

Laptop. Copy the file down over SSH. Replace you with your user name on the gateway.

On your laptop:

Bash
scp you@203.0.113.20:wg-clients/laptop.conf ~/laptop.conf

In the WireGuard app on macOS or Windows, choose to import a tunnel from a file, pick laptop.conf, and activate it. Then delete ~/laptop.conf; the app keeps its own copy. On a Linux laptop, install wireguard-tools and run sudo wg-quick up ~/laptop.conf instead. wg-quick sets the DNS line through resolvconf, so if it reports that the command is missing, install openresolv or remove the line.

Test it from the laptop:

Bash
ping -c 3 10.66.0.1
ping -c 3 10.10.0.21
nc -vz 10.10.0.21 5432

Phone. Show the phone's config as a QR code in your terminal. Make the window tall enough to show the whole code.

On gw1:

Bash
qrencode -t ansiutf8 < ~/wg-clients/phone.conf

In the WireGuard app on Android or iOS, add a tunnel from a QR code, scan it, name it, and switch it on. The QR code holds the phone's private key, so do not screenshot it or show it on a shared screen.

Once both devices have connected, look at the gateway:

Bash
sudo wg show

Both peers now have a latest handshake a few seconds ago, an endpoint with the public address your device connected from, and transfer counters that go up as you use the tunnel.

When both work, remove the private keys and configs from the gateway. Keep the .pub files: you need them to revoke a device later.

Bash
rm ~/wg-clients/laptop.key ~/wg-clients/laptop.conf ~/wg-clients/phone.key ~/wg-clients/phone.conf
ls ~/wg-clients

Check it worked

From the laptop, both pings get replies and nc reports the connection to port 5432 succeeded. Websites still load at your normal speed, because only private addresses go through the tunnel. On the phone, open any page that shows your public IP address: it shows 203.0.113.20, the gateway's, while the tunnel is on, and your mobile network's address when you switch it off. ls ~/wg-clients lists only laptop.pub, phone.pub and server.pub.

Step 8 · Revoke a lost device

Phones go missing. Because each device has its own key, cutting one off means deleting one [Peer] section on the gateway; nothing changes on the other devices.

The commands below replace wg0.conf. Make a backup first, and look at it before you go on:

Bash
sudo cp -p /etc/wireguard/wg0.conf /etc/wireguard/wg0.conf.bak
sudo ls -l /etc/wireguard/wg0.conf.bak

Now write a copy without the section that contains the phone's public key. awk in paragraph mode (RS = "") reads each blank-line-separated section as one record and keeps every record that does not contain the key. The new file is created private before anything is written to it.

On gw1:

Bash
sudo install -m 600 /dev/null /etc/wireguard/wg0.conf.new
sudo awk -v key="$(cat ~/wg-clients/phone.pub)" 'BEGIN { RS = ""; ORS = "\n\n" } index($0, key) == 0' /etc/wireguard/wg0.conf | sudo tee /etc/wireguard/wg0.conf.new > /dev/null
sudo mv /etc/wireguard/wg0.conf.new /etc/wireguard/wg0.conf
sudo wg-quick strip wg0

Apply the change to the running interface. wg syncconf compares the file with the live state and removes the phone without dropping the laptop's session. It runs inside bash -c because sudo does not pass the <( ... ) file through to the command:

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

The phone's address 10.66.0.3 is still in the full_tunnel set in /etc/wireguard/wg-gateway.nft. That does no harm with no peer behind it, but take it out before you give the address to another device, or that device inherits internet access through the gateway.

When wg show confirms the change, delete the backup and the phone's public key:

Bash
sudo rm /etc/wireguard/wg0.conf.bak
rm ~/wg-clients/phone.pub

To issue a replacement phone, create a new key pair and give it a fresh address such as 10.66.0.4; never reuse the old private key. If the lost device was a laptop, also revoke anything else it held, such as SSH keys in authorized_keys on your servers.

If you ever retire the gateway, this removes only the files the tutorial created, so any other tunnel in /etc/wireguard is left alone. rmdir removes the directory only if nothing else is in it, and says so otherwise:

Bash
sudo systemctl disable --now wg-quick@wg0
sudo rm /etc/wireguard/wg0.conf /etc/wireguard/wg-gateway.nft /etc/wireguard/server.key /etc/wireguard/server.pub /etc/sysctl.d/99-wireguard-gateway.conf
sudo rmdir /etc/wireguard
rm -r ~/wg-clients

Forwarding stays on until the next reboot; after that, it is off unless another file in /etc/sysctl.d or a service such as Docker turns it on. Do not switch it off by hand with sysctl -w: Docker, another VPN or any other routing on this server may still need it.

Then delete the UFW rules for 51820/udp and wg0 (sudo ufw status numbered, then sudo ufw delete <number> for each) and close the port in your provider's firewall.

Check it worked

wg-quick strip wg0 now shows one [Peer], the laptop's. After syncconf, sudo wg show wg0 peers prints a single key, the same one as cat ~/wg-clients/laptop.pub. On the phone, the tunnel may still say it is on, but no page loads through it and the gateway shows no new handshake from it. The laptop's tunnel keeps working throughout.

Next steps

  • Link two servers in different places with a site-to-site tunnel and keep PostgreSQL off the public internet: /tutorials/connect-two-servers-with-a-wireguard-vpn.
  • Lock the gateway's own input chain down to SSH and 51820/udp: /tutorials/write-a-server-firewall-with-nftables.
  • See how sysctl drop-ins are loaded and ordered: /tutorials/tune-kernel-parameters-with-sysctl.
  • Plan address ranges that will not clash with home and office networks: /tutorials/understand-ip-addresses-subnets-and-cidr.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.