SaferComputing.com

Reference · Network exposure

WireGuard and Tailscale addresses: bind SSH, databases and AI servers to them and keep them after a reboot

Bind SSH, PostgreSQL, Redis and Ollama to a wg0 or tailscale0 address instead of 0.0.0.0, and keep them listening after a reboot with systemd ordering, FreeBind=, net.ipv4.ip_nonlocal_bind or Tailscale's interface rules.

Published
Last checked
Applies to
Ubuntu 22.04 LTS and 24.04 LTS with WireGuard (wg-quick) or Tailscale; OpenSSH, PostgreSQL, Redis, Ollama and Docker
Technical specification
Target artifactServices bound to the address of a WireGuard (wg0) or Tailscale (tailscale0) interface: SSH ListenAddress, PostgreSQL listen_addresses, Redis bind, OLLAMA_HOST, and Docker -p ADDRESS:PORT:PORT.
Default behaviourA program can bind only to an address the host already has. A service that starts before the interface has its address gets Cannot assign requested address; Ubuntu 24.04's ssh.socket avoids this with FreeBind=yes.
Safe stateAfter a reboot, ss -tlnp shows each service on 127.0.0.1 and the private address, not on 0.0.0.0 or [::]; the journal shows no bind errors; the provider's console still works as a second way in.
Check on Linuxip -br addr show dev wg0, or tailscale ip -4
Check on Linuxsudo ss -tlnp
Check after a rebootsudo journalctl -b | grep -i 'cannot assign requested address'
Fix with WireGuardsudo systemctl edit ssh.service, then After=wg-quick@wg0.service and Wants=wg-quick@wg0.service under [Unit]
Fix for any programnet.ipv4.ip_nonlocal_bind = 1 in /etc/sysctl.d/, then sudo sysctl --system
Fix with Tailscale's rulesleave the service on all addresses and allow the interface: sudo ufw allow in on tailscale0

Stop Keep another way in

  • Before SSH listens only on a private address, make sure you can reach the server's console through your hosting provider's web panel. If the tunnel does not come up, the console is the only way in.
  • Keep your current session open until a second one has signed in over the private address.
  • Tailscale's guide suggests turning off key expiry for a server you reach only over Tailscale.

Step 1: find the private address

Linux, on the server
ip -br addr show dev wg0
tailscale ip -4

WireGuard's address is the Address = line in /etc/wireguard/wg0.conf; wg-quick up assigns it when it brings the interface up. Tailscale gives each device an address in the 100.x.y.z range that stays the same wherever the device moves. The examples use PRIVATE_IP for it.

Step 2: bind each service

SSH
Add to /etc/ssh/sshd_config.d/00-hardening.conf
ListenAddress 127.0.0.1
ListenAddress PRIVATE_IP
Ubuntu 24.04
sudo sshd -t
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

On Ubuntu 22.04, run sudo sshd -t and sudo systemctl restart ssh. Several ListenAddress lines are allowed; on 24.04 a generator copies them into ssh.socket. sshd -t does not check that the address exists.

PostgreSQL
Line in /etc/postgresql/VERSION/main/postgresql.conf
listen_addresses = 'localhost,PRIVATE_IP'

Then run sudo systemctl restart postgresql. PostgreSQL starts if it can listen on at least one address and only warns about the others: in a test with PostgreSQL 16, a missing address gave could not bind IPv4 address "100.101.102.103": Cannot assign requested address, and the server ran on 127.0.0.1 alone.

Redis
Line in /etc/redis/redis.conf
bind 127.0.0.1 PRIVATE_IP

Then run sudo systemctl restart redis-server. In a test with Redis 7.0, a missing address stopped Redis: Failed listening on port ... (TCP), aborting. Writing it as -PRIVATE_IP lets Redis start without it, as the comments in redis.conf explain, on 127.0.0.1 alone.

Ollama

Run sudo systemctl edit ollama.service, add Environment="OLLAMA_HOST=PRIVATE_IP:11434" under [Service], then sudo systemctl restart ollama. The Ollama exposure check lists the other places that can set OLLAMA_HOST.

Docker containers

Publish on the private address, for example -p PRIVATE_IP:8080:80. In a test with Docker Engine 29.9.2, a missing address failed with failed to bind host port ... cannot assign requested address, and the container stayed in the Created state.

Step 3: make the start-up order safe

On Ubuntu 24.04 the PostgreSQL and Redis units, and on 22.04 ssh.service, are ordered after network.target, not after WireGuard or Tailscale. Choose one option per service:

OptionWorks withTrade-off
Order after wg-quickWireGuard set up with wg-quickOne drop-in per service
FreeBind= on a socketSocket units, such as SSH on Ubuntu 24.04Socket units only
ip_nonlocal_bindAny program, IPv4Affects every program on the host
Allow the interfaceAny program; Tailscale's guideThe firewall alone does the filtering

The last three do not depend on start-up order, so they also cover Tailscale.

Order after wg-quick

wg-quick@wg0.service is a oneshot service: systemd counts it as started only when wg-quick up wg0 has finished, and units ordered after it wait for that.

Ubuntu, as a user with sudo
sudo systemctl edit ssh.service
Lines to add
[Unit]
After=wg-quick@wg0.service
Wants=wg-quick@wg0.service

After= sets the order; Wants= starts the tunnel but, on its own, does not. Add the same lines to postgresql@VERSION-main.service, redis-server.service or ollama.service.

FreeBind= on systemd sockets

FreeBind=yes lets a socket unit bind an address that is not configured yet; systemd's manual recommends it "for robustness reasons" whenever a socket binds a specific address. Ubuntu 24.04's ssh.socket sets it, so SSH on a private address survives a reboot there while socket activation stays on:

Ubuntu 24.04
systemctl cat ssh.socket | grep -i freebind

In a test, a socket bound this way before its address existed accepted connections once the address was added, without a restart. With socket activation turned off, sshd binds the port itself, as on 22.04.

net.ipv4.ip_nonlocal_bind

The system-wide equivalent of the socket option behind FreeBind=: every program may bind IPv4 addresses the host does not have yet.

Ubuntu, as a user with sudo
echo 'net.ipv4.ip_nonlocal_bind = 1' | sudo tee /etc/sysctl.d/60-nonlocal-bind.conf
sudo sysctl --system

With it set, sshd and Docker both started on a missing address in tests. Because it covers every program, a mistyped address no longer fails at start-up; the service waits on an address that never arrives.

Allow the interface instead of binding

Tailscale's guide to locking down an Ubuntu server does not change where services listen: they stay on all addresses, which are always available at boot. UFW denies incoming connections by default, allows everything arriving on tailscale0, and the public SSH rule goes:

Ubuntu, connected over Tailscale, as a user with sudo
sudo ufw allow in on tailscale0
sudo ufw default deny incoming
sudo ufw delete allow 22/tcp
sudo ufw reload

If UFW is not on yet, run sudo ufw enable instead of the last line. The guide writes the third command as sudo ufw delete 22/tcp, which ufw 0.36.2 on Ubuntu 24.04 rejects with ERROR: Invalid syntax. If your SSH rule uses limit or another port, delete it by number from sudo ufw status numbered. For WireGuard, use sudo ufw allow in on wg0. UFW does not filter ports Docker publishes: see Docker published ports bypass UFW.

Step 4: check after a reboot

Linux, after a reboot
sudo ss -tlnp
sudo journalctl -b | grep -i 'cannot assign requested address'
sudo grep -i 'could not bind' /var/log/postgresql/*.log
systemctl --failed
ResultMeaning
The service on 127.0.0.1 and PRIVATE_IPBound as intended
The service on 127.0.0.1 onlyThe address was missing at start-up; PostgreSQL, and sshd with two ListenAddress lines, run without it
Bind to port 22 on ... failed: Cannot assign requested address.sshd started before the address existed
ssh.service failed after Cannot bind any address.sshd exited with status 255, which Ubuntu's unit does not restart

From another device on the private network, nc -vz -w 5 PRIVATE_IP 22 should succeed; from a device outside it, nc -vz -w 5 PUBLIC_IP 22 should fail.

Sources