Most home servers start as one flat network. Every laptop, phone, smart plug, and camera sits on the same subnet as the NAS and the Docker host. Over time, a few port forwards get added so you can reach a dashboard from work, and nobody remembers why port 8123 is open to the internet. A single compromised IoT device then has a clear path to everything else you run.
This guide is for sysadmins and enthusiasts running Linux servers or containers at home. In an afternoon, you will split the network into VLANs with a default-deny firewall between them, replace inbound port forwarding with a WireGuard VPN, harden SSH, make sure Docker is not quietly publishing ports around your firewall, and audit what is exposed. The steps are vendor-neutral. The examples use a Linux router running nftables and netplan, but the same ideas apply to pfSense, OPNsense, or consumer routers with VLAN support.
Before you start
- A managed switch that supports 802.1Q VLAN tagging, or a router with VLAN-capable ports. Consumer mesh systems often do not expose this, so check first.
- A Linux host to act as router and firewall, with two or more NICs (or one NIC plus a tagged trunk). Ubuntu Server and Debian both work with the commands below.
- Console, IPMI, or physical access to the router. Some steps can cut off your remote session, and you need a way back in.
- A list of every device and service on your network. If you do not have one, do step 1 first.
- A domain name or dynamic DNS hostname that points at your home public IP. Replace YOUR_DOMAIN with it throughout.
1. Inventory what is actually running
You cannot secure what you have not listed. On each server, record the listening ports and the process behind them:
sudo ss -tulpnOn Docker hosts, also list published ports:
docker ps --format 'table {{.Names}}\t{{.Ports}}'For every entry, write down three things: what it is, who needs to reach it (you only, your family, the internet, nobody), and whether you still use it. Anything in the last category should be turned off. Unused services are the cheapest attack surface to remove.
2. Plan your VLANs
Group devices by trust level, not by physical location. A practical starting layout for a home network looks like this:
- Management (VLAN 10): the router, switch admin interfaces, hypervisor and IPMI. Only you reach this.
- Trusted (VLAN 20): your laptops, desktops, and phones.
- Servers (VLAN 30): the NAS, Docker host, and self-hosted apps.
- IoT and guests (VLAN 40): cameras, smart plugs, TVs, visitor devices. These get internet access and nothing else.
Pick subnets that do not overlap with anything your VPN clients use. The examples below use 10.10.10.0/24, 10.10.20.0/24, 10.10.30.0/24, and 10.10.40.0/24.
3. Create the VLAN interfaces on the router
On the switch, make the port connected to the router a trunk that carries all four VLANs tagged. Then, on the Linux router, define the subinterfaces with netplan. Replace eno1 with your trunk interface name and adjust the addresses to match your plan:
# /etc/netplan/01-vlans.yaml
network:
version: 2
ethernets:
eno1:
dhcp4: false
vlans:
vlan10:
id: 10
link: eno1
addresses: [10.10.10.1/24]
vlan20:
id: 20
link: eno1
addresses: [10.10.20.1/24]
vlan30:
id: 30
link: eno1
addresses: [10.10.30.1/24]
vlan40:
id: 40
link: eno1
addresses: [10.10.40.1/24]Apply it with sudo netplan try. The try form rolls back automatically if you lose connectivity, which is worth the extra few seconds. Run a DHCP server (dnsmasq, Kea, or your router’s built-in service) on each VLAN so clients receive addresses in the correct range.
4. Write a default-deny firewall between VLANs
Routing between VLANs is the default behavior, which defeats the purpose of segmentation. Set the forward chain to drop everything, then allow only the flows you actually need. The WAN interface name below is wan0, and the VLAN interfaces are the ones created above.
# /etc/nftables.conf (forward and input portion)
table inet segment {
chain input {
type filter hook input priority 0; policy drop;
iif lo accept
ct state established,related accept
ct state invalid drop
# Management VLAN can reach the router for SSH and DNS
iifname vlan10 tcp dport 22 accept
iifname vlan10 udp dport { 53, 67 } accept
iifname vlan10 tcp dport 53 accept
# DHCP and DNS for all client VLANs
iifname { vlan20, vlan30, vlan40 } udp dport { 53, 67 } accept
iifname { vlan20, vlan30, vlan40 } tcp dport 53 accept
# WireGuard listener on WAN
iifname wan0 udp dport 51820 accept
# ICMP for diagnostics
ip protocol icmp accept
}
chain forward {
type filter hook forward priority 0; policy drop;
ct state established,related accept
ct state invalid drop
# Trusted clients reach servers on web and SSH ports
iifname vlan20 oifname vlan30 tcp dport { 22, 80, 443 } accept
# Trusted clients and VPN peers reach everything in the server VLAN
iifname vlan20 oifname vlan30 accept
# Internet access for all client VLANs
iifname { vlan20, vlan30, vlan40 } oifname wan0 accept
# IoT cannot initiate connections to trusted or servers VLANs
# (no rule present, so the default drop applies)
}
}Load it with sudo nft -c -f /etc/nftables.conf to check syntax, then sudo nft -f /etc/nftables.conf to apply. Two notes. First, the input chain must allow your management access before you apply it, or you will lock yourself out of the router. Run the change from a session that will survive, or from the console. Second, you still need NAT (masquerade) on wan0 for client VLANs to reach the internet, and that is covered by a separate postrouting rule in most setups.
Note that the IoT VLAN has no rule allowing it to reach the other VLANs, so connections from IoT devices to your servers are dropped. Replies to connections you started are still permitted because of the established,related rule.
5. Replace port forwarding with a WireGuard VPN
Port forwarding exposes a service to the entire internet, and every forwarded port is a standing invitation to scanners. A VPN inverts that model. The only inbound port is WireGuard’s UDP listener, which does not respond to unauthenticated packets, so it is effectively invisible to scanners. You reach your services only after connecting.
Install WireGuard on the router and the client devices, then generate a key pair on each side:
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee client1.key | wg pubkey > client1.pubServer configuration on the router, saved as /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
[Peer]
# client1 (your laptop)
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32Client configuration on your laptop:
[Interface]
PrivateKey = CLIENT1_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 10.10.10.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_DOMAIN:51820
AllowedIPs = 10.10.10.0/24, 10.10.20.0/24, 10.10.30.0/24
PersistentKeepalive = 25Bring the tunnel up on the server with sudo wg-quick up wg0, and enable it at boot with sudo systemctl enable wg-quick@wg0. Because AllowedIPs lists only the internal subnets, traffic to the internet from your laptop continues to go directly, which is what you want for a remote-access tunnel. Add a firewall rule so the 10.8.0.0/24 range is treated like the trusted VLAN if you want VPN clients to reach servers.
If you prefer not to run a VPN at all, a zero-trust tunnel service works too. These typically run a lightweight connector on your network that makes outbound connections only, then authenticate users through an identity provider before proxying traffic to specific internal hosts. The security principle is the same: nothing listens on the internet, and access is granted per user and per application. Whichever you choose, the rule is that inbound port forwards to your admin interfaces should be removed.
6. Lock down SSH
SSH is the most-scanned service on any public address. Key-only authentication and no root login remove nearly all of the practical risk from password guessing. Generate a modern key on your client if you do not already have one:
ssh-keygen -t ed25519 -C "YOUR_USER@laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub YOUR_USER@10.10.20.10Confirm that key login works in a second terminal before changing anything. Then create a drop-in configuration so your main sshd_config stays untouched:
# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers YOUR_USER
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding noAlways validate before restarting, so a typo does not end your session:
sudo sshd -t && sudo systemctl restart sshOn some distributions the service is named sshd rather than ssh. Keep your existing session open while you test a new login from a separate terminal. If the test fails, you still have a working session to fix the config.
7. Stop Docker from bypassing your firewall
This is the most common surprise on self-hosted servers. When Docker publishes a port, it writes its own iptables rules, and those rules are evaluated before the ones managed by UFW or your host firewall. A container published on 0.0.0.0 can be reachable from the network even if your firewall says it should be blocked. Check what your containers are bound to:
docker ps --format 'table {{.Names}}\t{{.Ports}}'Anything showing 0.0.0.0 should be rebound to the address you actually need. For a service that only you use, bind to loopback and reach it through the VPN, or bind to the server VLAN address:
# docker-compose.yml excerpt
services:
dashboard:
image: YOUR_IMAGE
ports:
- "10.10.30.10:8080:8080" # server VLAN address only
admin:
image: YOUR_ADMIN_IMAGE
ports:
- "127.0.0.1:9000:9000" # loopback only, reach via SSH tunnelContainers that only need to talk to each other should share a Docker network and publish no ports at all. Reverse proxies belong on the server VLAN, with the VPN as the only path in.
8. Audit exposure from outside
Internal checks tell you what is listening. An external check tells you what the internet can actually reach, which is the question that matters. Start with your router: confirm that UPnP is disabled and that the port-forward table is empty except for the WireGuard port. Then scan your own public address from a network you do not control, such as a phone on cellular data. Only scan addresses you own:
nmap -Pn -p- --open YOUR_PUBLIC_IPThe output should list at most one open port, the WireGuard UDP port (UDP scans need the -sU flag and are slower, so check that separately if you want full coverage). Any open TCP port is a finding. Work through the list and either close it, move it behind the VPN, or document why it must stay public. Repeat this check after every router firmware update, after adding a container, and once a quarter.
Keep a short checklist alongside your inventory from step 1:
- Every listening port has a name, an owner, and a reason to exist.
- No admin interface (router, NAS, hypervisor, IPMI, dashboards) is reachable from the internet.
- Container ports are bound to a specific address, not 0.0.0.0, unless they are intentionally public.
- UPnP and automatic port mapping are disabled on every router.
- IoT devices cannot initiate connections to the trusted or server VLANs.
- SSH accepts keys only, and root login is disabled.
- Firmware on the router, switch, and hypervisor is current.
Verify it worked
Do not trust the configuration until you have tested the behavior. Each check below should produce the result shown.
- VLAN isolation: from a device on VLAN 40 (IoT), try to reach a server on VLAN 30. Run
nc -vz 10.10.30.10 22. It should time out or be refused. Then confirm internet access still works withcurl -I https://example.com. - Trusted access: from VLAN 20, the same SSH test to 10.10.30.10 should succeed.
- Firewall state: run
sudo nft list ruleseton the router and confirm the forward and input chains showpolicy drop. - VPN path: disconnect from home Wi-Fi, bring up the WireGuard tunnel, and run
ssh YOUR_USER@10.10.30.10. Then runsudo wg showon the router and confirm a recent handshake for your client. - SSH hardening: run
ssh -o PubkeyAuthentication=no YOUR_USER@10.10.20.10. It should fail with a permission error, with no password prompt. - Exposure: rerun the external nmap scan from step 8 and confirm only the WireGuard port is open.
Common mistakes
- Locking yourself out of the router. Applying a default-drop input chain without a management allow rule ends remote access instantly. Always test with a console or a second session open, and keep a rollback command ready, such as scheduling
nft flush rulesetto run in five minutes until you confirm the change works. - Forgetting DNS and DHCP. Clients on a new VLAN cannot get an address or resolve names if the firewall blocks UDP 53 and 67 to the router. Allow them explicitly.
- Assuming Docker respects your firewall. Published container ports bypass UFW rules. Check the bindings, not just the firewall status.
- Leaving UPnP on. Applications and game consoles can open inbound ports without telling you, and they will not show up in your forward table. Disable it and verify with the external scan.
- Making the VPN a bridge into everything. Giving VPN clients full access to the IoT VLAN, or to the management VLAN for convenience, undoes much of the segmentation. Grant access by need.
- Keeping old keys. Remove VPN peers and authorized SSH keys for devices you no longer own, such as a retired laptop. Review them when you audit.
- Auditing once. Exposure changes whenever you install something new. Schedule the external scan and the inventory review, or they will drift out of date.
Takeaway
A homelab does not need enterprise tooling to be defensible. Split the network by trust level, make the firewall deny by default, let the VPN be the only door in, and keep SSH to keys. Then check the result from the outside rather than assuming it. Once you have done this once, the afternoon becomes a quarterly routine of inventory, scan, and review, and that routine catches far more problems than any single configuration change.
