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.
| Target artifact | Services 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 behaviour | A 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 state | After 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 Linux | ip -br addr show dev wg0, or tailscale ip -4 |
| Check on Linux | sudo ss -tlnp |
| Check after a reboot | sudo journalctl -b | grep -i 'cannot assign requested address' |
| Fix with WireGuard | sudo systemctl edit ssh.service, then After=wg-quick@wg0.service and Wants=wg-quick@wg0.service under [Unit] |
| Fix for any program | net.ipv4.ip_nonlocal_bind = 1 in /etc/sysctl.d/, then sudo sysctl --system |
| Fix with Tailscale's rules | leave 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
ip -br addr show dev wg0
tailscale ip -4WireGuard'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
ListenAddress 127.0.0.1
ListenAddress PRIVATE_IPsudo sshd -t
sudo systemctl daemon-reload
sudo systemctl restart ssh.socketOn 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
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
bind 127.0.0.1 PRIVATE_IPThen 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:
| Option | Works with | Trade-off |
|---|---|---|
| Order after wg-quick | WireGuard set up with wg-quick | One drop-in per service |
| FreeBind= on a socket | Socket units, such as SSH on Ubuntu 24.04 | Socket units only |
| ip_nonlocal_bind | Any program, IPv4 | Affects every program on the host |
| Allow the interface | Any program; Tailscale's guide | The 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.
sudo systemctl edit ssh.service[Unit]
After=wg-quick@wg0.service
Wants=wg-quick@wg0.serviceAfter= 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:
systemctl cat ssh.socket | grep -i freebindIn 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.
echo 'net.ipv4.ip_nonlocal_bind = 1' | sudo tee /etc/sysctl.d/60-nonlocal-bind.conf
sudo sysctl --systemWith 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:
sudo ufw allow in on tailscale0
sudo ufw default deny incoming
sudo ufw delete allow 22/tcp
sudo ufw reloadIf 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
sudo ss -tlnp
sudo journalctl -b | grep -i 'cannot assign requested address'
sudo grep -i 'could not bind' /var/log/postgresql/*.log
systemctl --failed| Result | Meaning |
|---|---|
The service on 127.0.0.1 and PRIVATE_IP | Bound as intended |
The service on 127.0.0.1 only | The 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.
Related guides and lookups
Sources
- Ubuntu manual: sshd_config, ListenAddress
- Ubuntu Discourse: sshd socket activation, addresses from a generator on 24.04
- Ubuntu manual: systemd.socket, FreeBind=
- Ubuntu manual: systemd.service, Type=oneshot and RestartPreventExitStatus=
- Ubuntu manual: systemd.unit, After= and Wants=
- systemd: Running services after the network is up
- Linux manual: IP_FREEBIND
- Linux manual: wg-quick
- PostgreSQL: connection settings, listen_addresses
- Ollama: Linux installation
- Tailscale: use UFW to lock down an Ubuntu server
- Tailscale: how Tailscale assigns IP addresses