Skip to main content

NetBird with UFW-Docker

I wanted to be able to publish a Docker port for my NetBird peers and leave it closed to the internet. I use UFW-Docker for the public ports and manage peer access in the NetBird dashboard. That way, when another peer needs access, I only have to change the NetBird policy.

This setup uses Traefik for HTTP and HTTPS and the NetBird server for STUN. PostgreSQL is the private service in the examples below. A peer with the TCP 5432 policy should be able to connect using the host's NetBird address. The same port should time out on the public IP.

Prerequisites​

  • A Linux host with UFW enabled and NetBird running as a systemd service. The examples use kernel routing, the wt0 interface and IPv4.
  • Docker bridge networking with Docker's iptables backend. Run iptables -S FORWARD and check for a jump to DOCKER-USER. Docker's native nftables backend uses a different setup.
  • A root shell and a direct SSH connection or console. Restarting the NetBird client will briefly disconnect peers from this host.

How the firewall layers fit together​

Where to configure accessWhat it controls
UFWConnections to services listening on the host
UFW-DockerAccess to ports Docker forwards to containers
NetBird dashboardWhich peers can connect to a private service over NetBird

When you connect to a published port, Docker rewrites the destination to the container's IP. This is destination NAT (DNAT). The packet goes through FORWARD, so a rule in the host's INPUT chain alone won't protect the container. UFW-Docker checks the forwarded connection in DOCKER-USER. Docker firewall chains.

Allow the public NetBird services​

I prefix my container names with netbird- and use a Compose network called netbird_netbird. Check the names on your host and substitute them in the commands below.

docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker network ls
PortPublic service
80/tcp and 443/tcpTraefik for the dashboard, API, relay and HTTPS ingress
3478/udpNetBird STUN

Keep the host rules your installation uses for these services.

ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 3478/udp

If you use QUIC, keep the host allowance for UDP 443 as well. Check your listeners and any IPv6 path before removing an existing host rule.

Install UFW-Docker​

If UFW-Docker is already installed and its service is active, continue to the container rules. For a new install, follow the UFW-Docker installation instructions, then install the systemd service and load the rules.

ufw-docker install-service
ufw reload

Allow the published containers​

Allow the ports published by Traefik and the NetBird server.

ufw-docker allow netbird-traefik 80/tcp netbird_netbird
ufw-docker allow netbird-traefik 443/tcp netbird_netbird
ufw-docker allow netbird-server 3478/udp netbird_netbird

Check the rules against the container addresses. For example, the allowed destination for UDP 3478 should match the NetBird server's IP in Docker's DNAT rule.

ufw-docker status
iptables -S DOCKER-USER
iptables -t nat -S DOCKER
docker inspect --format '{{range $name, $net := .NetworkSettings.Networks}}{{$name}} {{$net.IPAddress}}{{"\n"}}{{end}}' netbird-server

Optional: NetBird Proxy direct transport​

If you use fixed-port direct transport, publish the proxy's UDP port in Docker and allow it through UFW-Docker. This example uses NB_PROXY_WG_PORT=51820. Set NB_PROXY_EXTERNAL_IP_MAP to the public address of the host. NetBird proxy P2P configuration.

ufw-docker allow netbird-proxy 51820/udp netbird_netbird

Keep sensitive services private​

Publish the service on the Docker host so peers can connect to the host's NetBird address. Leave the port out of the public UFW-Docker rules. For PostgreSQL, configure the policy in the NetBird dashboard as follows.

  1. Add the peer that needs database access to the source group.
  2. Add the Docker host's peer to the destination group.
  3. Allow TCP 5432 from the source group to the destination group.
  4. Check for any other policy or route that already gives peers broader access.
note

ufw-docker allow netbird-db 5432/tcp creates a from any allowance. Adding that rule can expose the published database port to the internet.

Pass NetBird-authorized traffic through UFW-Docker​

NetBird marks an allowed packet on wt0 before Docker changes its destination to the container. If UFW-Docker checks the packet first in the filter chain, it can still drop the connection. Here is an example from the kernel log using sample addresses.

[UFW DOCKER BLOCK] IN=wt0 OUT=br-... SRC=100.64.0.10 DST=172.20.0.10 ... DPT=5432 ... MARK=0x1bd20

IN=wt0 shows that the connection came over NetBird. DPT=5432 is the database port. The UFW hook below checks the MARK value so the connection can continue. Allowing everything on wt0 would also allow traffic without that mark.

The order can change after a restart. On a routing peer, NetBird's native nftables backend can put an interface-wide accept before DOCKER-USER. I use the host client's iptables backend so NetBird checks for the mark before Docker's filter rules run. The UFW hook handles the case where DOCKER-USER sees an approved packet first.

Configure the NetBird host client​

  1. Check iptables --version on the Docker host. The iptables-nft frontend reports (nf_tables). Check the host client's existing settings with systemctl cat netbird.service.
  2. If the client already has NB_SKIP_NFTABLES_CHECK=true, continue to the mark lookup. Otherwise, run install -d -m 0755 /etc/systemd/system/netbird.service.d to create the drop-in directory. Save the following as /etc/systemd/system/netbird.service.d/firewall-backend.conf. NetBird client environment variables.
[Service]
Environment=NB_SKIP_NFTABLES_CHECK=true
  1. Reload systemd and restart the host client from your direct connection. Check that management and signal reconnect.
systemctl daemon-reload
systemctl restart netbird.service
netbird status

Find NetBird's authorization mark​

  1. List the mangle PREROUTING rules and follow the jump to NETBIRD-RT-PRE.
iptables -t mangle -S PREROUTING
iptables -t mangle -S NETBIRD-RT-PRE
  1. Find the rule for the allowed peer or source set, a local destination and the published port. For TCP 5432, the end of the rule on my host looks like this.
-j MARK --set-xmark 0x1bd20/0xffffffff

The value before the slash, 0x1bd20, is the mark to use in the hook. The value after it, 0xffffffff, is the mask.

  1. Check the corresponding ACL and forwarding rules.
iptables -S NETBIRD-ACL-INPUT
iptables -t mangle -S FORWARD
iptables -S FORWARD

In NETBIRD-ACL-INPUT, look for an ACCEPT for the same source and port. In filter FORWARD, look for the rule that accepts the mark. If you use explicit deny policies, test a fresh connection from a denied peer as well. NetBird peer ACL rules.

In mangle FORWARD, the established-connection rule should come before the DNAT drop.

-A FORWARD -i wt0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wt0 -m conntrack --ctstate DNAT -m mark ! --mark 0x1bd20 -j DROP

The second rule drops new, unmarked connections that arrive on wt0 and have been destination-translated. It applies across ports. If a peer connects directly to a routed container IP, check the routing policy and test that path separately. NetBird forwarding rules.

Add the UFW-Docker integration hook​

  1. Check the existing UFW hook and systemd drop-ins. These steps assume a stock after.init and no existing netbird.conf. If you've customized either one, back it up and merge the changes.
cat /etc/ufw/after.init
systemctl cat ufw-docker.service
ls -l /etc/systemd/system/ufw-docker.service.d/
  1. Back up the current hook and rules. Keep the backup path printed below and use the same root shell for the remaining install steps.
backup_dir="/root/ufw-netbird-backup-$(date -u +%Y%m%dT%H%M%SZ)"
install -d -m 0700 "$backup_dir"
cp -a /etc/ufw/after.init "$backup_dir/after.init"
iptables -S DOCKER-USER > "$backup_dir/docker-user.rules"
printf 'Backup directory: %s\n' "$backup_dir"
  1. Create the hook in the backup directory. Set NETBIRD_AUTHORIZED_LOCAL_SERVICE_MARK to the mark from your host. The last two commands check the script and install it.
cat > "$backup_dir/after.init.new" <<'EOF'
#!/bin/sh
#
# after.init: if executable, called by ufw-init. See 'man ufw-framework' for
# details. Note that output from these scripts is not seen via the
# the ufw command, but instead via ufw-init.
#
# Copyright 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -eu

NETBIRD_INTERFACE=wt0
# Recheck the mark against NetBird's loaded rules after upgrades.
NETBIRD_AUTHORIZED_LOCAL_SERVICE_MARK=0x1bd20

# UFW start/reload and the Docker lifecycle can invoke this concurrently.
exec 9>/run/lock/ufw-netbird-policy.lock
/usr/bin/flock -x 9

netbird_rule() {
/usr/sbin/iptables -w 5 "$@" \
-i "$NETBIRD_INTERFACE" \
-m mark --mark "$NETBIRD_AUTHORIZED_LOCAL_SERVICE_MARK/0xffffffff" \
-m conntrack --ctstate NEW,ESTABLISHED,RELATED \
-m comment --comment "NetBird-authorized local service" \
-j RETURN
}

case "${1:-}" in
start)
# Do not add firewall state when UFW is disabled.
if ! /usr/bin/grep -Eq '^[[:space:]]*ENABLED=yes[[:space:]]*$' /etc/ufw/ufw.conf; then
exit 0
fi
# The Docker service post-start hook retries if this chain is not ready.
if ! /usr/sbin/iptables -w 5 -n -L DOCKER-USER >/dev/null 2>&1; then
exit 0
fi
if ! netbird_rule -C DOCKER-USER >/dev/null 2>&1; then
netbird_rule -I DOCKER-USER 1
fi
;;
stop|flush-all)
# Remove only our exact rule, including any historical duplicates.
while netbird_rule -C DOCKER-USER >/dev/null 2>&1; do
netbird_rule -D DOCKER-USER
done
;;
status)
if netbird_rule -C DOCKER-USER >/dev/null 2>&1; then
echo "NetBird authorized-service forwarding: installed"
else
echo "NetBird authorized-service forwarding: absent"
fi
;;
*)
echo "Usage: $0 {start|stop|flush-all|status}" >&2
exit 2
;;
esac
EOF

sh -n "$backup_dir/after.init.new"
install -o root -g root -m 0750 "$backup_dir/after.init.new" /etc/ufw/after.init

The hook adds one RETURN rule for marked packets entering on wt0. A match returns to FORWARD so the connection can continue. The lock keeps UFW and Docker from adding the rule at the same time.

  1. Add the systemd drop-in and apply the hook. UFW runs after.init on start or reload. The ExecStartPost below reapplies the rule after UFW-Docker refreshes its chains, including when Docker starts after UFW.
install -d -m 0755 /etc/systemd/system/ufw-docker.service.d
cat > /etc/systemd/system/ufw-docker.service.d/netbird.conf <<'EOF'
[Service]
# Reapply after Docker starts and the existing ufw-docker reload finishes.
ExecStartPost=/etc/ufw/after.init start
EOF

systemctl daemon-reload
/etc/ufw/after.init start
/etc/ufw/after.init status

The status command should report NetBird authorized-service forwarding: installed.

Verify the allowed and blocked paths​

  1. Check the entry in DOCKER-USER. Running the hook a second time should still leave one NetBird-authorized local service rule ahead of the UFW-Docker checks.
iptables -nvL DOCKER-USER --line-numbers
/etc/ufw/after.init start
iptables -S DOCKER-USER
  1. Open a fresh connection from each source in the table. Use the NetBird hostname for peer tests and the host's public IP for the external test.
nc -vz -w 3 server.nb.example.com 5432
Test TCP 5432 fromConnect toExpected result
Peer in the allowed groupHost's NetBird addressConnects
Application that needs database accessHost's NetBird addressConnects
Peer outside the allowed group with no other grantHost's NetBird addressBlocked
Client on the internetHost's public addressBlocked

If the application runs in a container, run its test inside the container. Netcat checks TCP reachability. Check the database login separately.

tip

The hook counter can stay at zero when NetBird accepts an approved connection before DOCKER-USER. Check the order with iptables -nvL FORWARD --line-numbers. For a denied connection to a published port that reaches the host, check the DNAT drop counter with iptables -t mangle -nvL FORWARD --line-numbers.

To test a dashboard change, grant a peer access to a disposable published service and connect. Revoke the policy and try a new connection. The public port should stay blocked throughout.

Repeat the checks after a NetBird or Docker restart, UFW reload or reboot. Also test the public services you use, including HTTPS, STUN and the reverse proxy. If the host forwards other traffic, check the relevant routes and any exit traffic. Verify IPv6 separately if you use it.

Troubleshooting and maintenance​

Public services or SSO fail​

Check OIDC discovery from the affected container. Use your provider's discovery URL and check the DNS result, host listener and Docker forwarding rules for that address.

OIDC_DISCOVERY_URL='https://idp.example.com/.well-known/openid-configuration'
curl --fail --show-error --silent --max-time 10 "$OIDC_DISCOVERY_URL"
docker logs --tail 100 netbird-proxy
docker logs --tail 100 netbird-traefik

If discovery works from the host but fails from the proxy, check the proxy's path to the issuer before changing the public firewall rules.

A Docker update or container recreation changes access​

UFW-Docker stores the destination IP in each public allowance. It refreshes rules when Docker starts. Recreating an individual container can give it a different address between refreshes. Compare Docker's DNAT destination with the UFW-Docker rule, then rerun the affected service's ufw-docker allow command if they differ.

After a NetBird update or a change to NB_FWMARK_BASE, read the mark again. If it changed, update the hook and verify the allowed and blocked connections.

Roll back the hook​

To undo the backend change, use your direct connection and remove the drop-in created for the host client.

rm /etc/systemd/system/netbird.service.d/firewall-backend.conf
systemctl daemon-reload
systemctl restart netbird.service
netbird status

Retest peer and public access. Returning to the previous backend can make a published port reachable by ungranted peers again.

To remove the UFW hook as well, set backup_dir to the path saved during installation. Stop the current hook before restoring after.init. If you added to an existing netbird.conf, restore your copy of it in place of the rm command below.

/etc/ufw/after.init stop
cp -a "$backup_dir/after.init" /etc/ufw/after.init
rm /etc/systemd/system/ufw-docker.service.d/netbird.conf
systemctl daemon-reload
iptables -S DOCKER-USER

Check that DOCKER-USER no longer contains the commented rule and repeat the connection tests. Removing the hook can block previously approved connections when UFW-Docker runs first.

References​