Most home lab owners eventually want to reach their services from work, a phone on cellular data, or a hotel network. The common shortcut is to port-forward a web UI or SSH straight to the internet, and that shortcut is how many home networks get scanned, brute-forced, or compromised. A better pattern is to expose exactly one thing, a WireGuard endpoint, and reach everything else through the encrypted tunnel. This guide is for self-hosters and home lab operators running a Debian or Ubuntu server on their home network. By the end you will have WireGuard as your only inbound path, key-only SSH, a private DNS zone that is invisible outside the tunnel, and a port audit that confirms the result.
Before you start
- A Linux server on your LAN running Debian or Ubuntu, with sudo access. The examples assume the server is at 192.168.1.10 and your LAN is 192.168.1.0/24. Adjust these to match your network.
- Admin access to your router so you can create one port forward and disable UPnP.
- A public IPv4 address on your connection. If your ISP uses carrier-grade NAT (CGNAT), inbound connections will not reach you, and you will need a small relay host or a different approach.
- A client device (laptop or phone) that you want to connect from. Linux examples are shown, with notes for macOS and Windows.
- Optional: a dynamic DNS name (for example, a hostname such as YOUR_DOMAIN) if your public IP changes.
- A second terminal session to the server, opened before you change any firewall or SSH settings. This is your lifeline if something locks you out.
1. Set a default-deny firewall first
Start with the firewall so that every later change happens behind a known policy. The rules below deny all inbound traffic by default, allow SSH only from your LAN for now, and open the single UDP port WireGuard will use.
sudo apt update
sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp
sudo ufw allow 51820/udp
sudo ufw enable
sudo ufw status verboseThe SSH rule is scoped to your LAN, so your existing session keeps working. You will tighten it to the tunnel in step 4.
2. Install WireGuard and generate keys
WireGuard uses public-key cryptography for every peer. Each side has a private key that never leaves the device and a public key that you share. Generate the server keys on the server, restricting file permissions so only root can read the private key.
sudo apt install -y wireguard
umask 077
sudo sh -c 'wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key'
sudo cat /etc/wireguard/server_public.keyOn the client, generate its own key pair the same way. Keep the client private key on the client and copy only the client public key to the server.
umask 077
wg genkey | tee client_private.key | wg pubkey > client_public.key
cat client_public.key3. Configure the WireGuard server and router
Create the server interface on the 10.8.0.0/24 tunnel subnet. Replace the placeholder values with your real keys, and replace eth0 with your LAN interface name (find it with ip route show default). The PostUp and PostDown lines masquerade tunnel traffic so LAN devices do not need a return route to the tunnel subnet.
sudo nano /etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
[Peer]
# laptop
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32Enable IP forwarding so the server can route between the tunnel and the LAN, then bring the interface up at boot.
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system
sudo chmod 600 /etc/wireguard/wg0.conf
sudo systemctl enable --now wg-quick@wg0
sudo wg showOn your router, create one port forward: UDP port 51820 to 192.168.1.10. Forward nothing else. Then disable UPnP on the router. UPnP lets devices open ports without your knowledge, which undermines the whole design.
Now create the client configuration. The Endpoint can be a dynamic DNS hostname, which WireGuard resolves when it connects. Note that AllowedIPs lists only the tunnel and LAN subnets, so this is a split tunnel: general internet traffic stays on the client’s normal route. Do not add a DNS = line here, because on wg-quick that setting redirects all DNS through the tunnel, which is not what split DNS wants.
[Interface]
Address = 10.8.0.2/24
PrivateKey = CLIENT_PRIVATE_KEY
PostUp = resolvectl dns %i 10.8.0.1; resolvectl domain %i ~home.arpa
PostDown = resolvectl revert %i
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_DOMAIN:51820
AllowedIPs = 10.8.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25The PostUp line is the split DNS piece for systemd-resolved systems, and it is explained in step 5. Save this as /etc/wireguard/wg0.conf on the client, or import it into the WireGuard app on macOS or Windows (in which case, remove the PostUp and PostDown lines and handle DNS as shown in step 5). Bring it up with sudo wg-quick up wg0 and confirm the handshake with sudo wg show. A recent latest handshake timestamp means the tunnel is working.
4. Lock SSH down to keys and to the tunnel
Generate an Ed25519 key on the client. Protect it with a passphrase, and store it only on devices you control.
ssh-keygen -t ed25519 -C laptop-YOUR_NAME
ssh-copy-id -i ~/.ssh/id_ed25519.pub YOUR_USER@192.168.1.10Confirm that key login works before you disable passwords. Then create a drop-in configuration file. Modern OpenSSH on Debian and Ubuntu reads files in /etc/ssh/sshd_config.d/, so you do not need to edit the main file.
sudo nano /etc/ssh/sshd_config.d/90-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
AllowUsers YOUR_USER
MaxAuthTries 3
X11Forwarding noValidate the configuration before restarting, so a typo does not take SSH down.
sudo sshd -t && sudo systemctl restart sshNext, make SSH reachable only through the tunnel. Add a rule for the wg0 interface first, test from the client, and only then remove the LAN rule if you do not need LAN SSH.
sudo ufw allow in on wg0 to any port 22 proto tcp
# after you have verified access over the tunnel (step 6):
sudo ufw delete allow from 192.168.1.0/24 to any port 22 proto tcpOn the client, add a host entry so connecting is one short command.
Host lab
HostName 10.8.0.1
User YOUR_USER
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes5. Set up split DNS so internal names stay internal
Split DNS means that queries for your internal names go to your own resolver, and everything else uses the normal resolver. Use home.arpa as the internal suffix. It is reserved for home networks by IETF special-use naming (RFC 8375), so it will not collide with a real public domain. Install dnsmasq on the server and bind it only to the tunnel address, so it never answers on your LAN or the internet.
sudo apt install -y dnsmasq
sudo nano /etc/dnsmasq.d/homelab.conf
listen-address=10.8.0.1
bind-interfaces
no-resolv
local=/home.arpa/
domain=home.arpa
address=/nas.home.arpa/192.168.1.20Because there is no upstream server configured, dnsmasq answers only for names it knows and refuses everything else, which is what you want for a private resolver. Add one systemd override so dnsmasq waits for the tunnel interface at boot, since it cannot bind to 10.8.0.1 before wg0 exists.
sudo systemctl edit dnsmasq
[Unit]
After=wg-quick@wg0.service
Wants=wg-quick@wg0.service
sudo systemctl restart dnsmasq
sudo ufw allow in on wg0 to any port 53On the client, the PostUp line in step 3 sends only home.arpa queries to 10.8.0.1 on systemd-resolved systems. The ~ prefix makes it a routing-only domain, so it does not become the default resolver for all lookups. On macOS, create a resolver file for the suffix instead.
sudo mkdir -p /etc/resolver
echo nameserver 10.8.0.1 | sudo tee /etc/resolver/home.arpaOn Windows, add a name resolution policy rule from an elevated PowerShell prompt.
Add-DnsClientNrptRule -Namespace '.home.arpa' -NameServers '10.8.0.1'6. Verify it worked
Check each layer in order, starting from the client with the tunnel up.
- Tunnel: run
sudo wg showon both ends. The client should show a recent handshake, and the server should list the client peer with its tunnel address. - Reachability: run
ping -c 3 10.8.0.1andping -c 3 192.168.1.10. Both should answer over the tunnel. - Key-only SSH: run
ssh lab. You should log in with your key and no password prompt. On the server, runsudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin'and confirm the values areno. - Password rejection: run
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password YOUR_USER@10.8.0.1. It should fail with Permission denied and never ask for a password. - Split DNS: run
resolvectl query nas.home.arpaon Linux, ordig @10.8.0.1 nas.home.arpafrom anywhere in the tunnel. Then rundig @1.1.1.1 nas.home.arpaand confirm the name does not resolve publicly. Your internal names should not leak.
7. Audit listening ports
A hardened firewall is only as good as the services behind it. On the server, list every socket that is listening and the process that owns it.
sudo ss -tulpnFor each entry, ask whether it should be reachable from the LAN, from the tunnel only, or only from the host itself. Services that should be private should bind to 127.0.0.1 or 10.8.0.1, not 0.0.0.0 or ::. Many self-hosted apps have a bind-address setting, and an explicit bind is safer than relying on the firewall alone. Remove any service you no longer use.
Then test from outside your network, using a phone on cellular data or a cloud shell you control. Scan only your own public address.
nmap -Pn -sT -p 1-65535 YOUR_PUBLIC_IP
nmap -sU -p 51820 YOUR_PUBLIC_IPThe TCP scan should report every port as filtered or closed. The UDP scan should show 51820 as open or open|filtered. If anything else responds, check your router’s port forwards and UPnP table, because that is the most common source of an unexpected open port.
Common mistakes
- Setting a DNS line in the client config. This sends all DNS through the tunnel and breaks split DNS for everything else. Use the routing-only domain approach instead.
- Using a full tunnel without intending to. If AllowedIPs is 0.0.0.0/0, all client traffic goes through your home connection. That is fine for some use cases, but do it on purpose.
- Locking yourself out. Always keep a working session open while you change SSH or firewall rules, and add the wg0 SSH rule before removing the LAN rule.
- Forwarding more than WireGuard. A web UI port forward for convenience defeats the design. Reach web UIs through the tunnel instead.
- Leaving UPnP enabled. Applications can silently open ports on your router. Turn it off and verify with the external scan.
- Ignoring the boot order. If dnsmasq or sshd binds to 10.8.0.1 before wg0 exists, the service fails at startup. The systemd ordering in step 5 prevents this.
- Assuming a dynamic IP is fine without a name. If your public IP changes and the client Endpoint is a fixed address, the tunnel silently stops working. Use a dynamic DNS hostname.
- Sharing or copying private keys. Each device gets its own key pair. If a laptop is lost, remove its peer from the server config and restart wg-quick@wg0.
- Skipping the password test. Confirm that password login is refused. A setting that looks right in a file is not always the active setting, so check with
sshd -T.
Takeaway
You now have one door, UDP 51820, that leads into a WireGuard tunnel, and everything else sits behind it. SSH accepts only keys, and only from the tunnel. Your internal names resolve only for connected clients. The port audit gives you a repeatable check you can rerun after every new service. Put the verification steps in a short checklist, and rerun the external scan whenever you change your router or add a service.
