brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ListenAddress makes sshd answer on one address only

Network and access
~/tips/tip-sshd-listenaddress

ListenAddress 192.0.2.10

Keep the SSH server off the public interface and listening only where you want it.

By default sshd listens on every address the machine has. When a server has a public interface and a private one, I’d want SSH only on the private side.

In /etc/ssh/sshd_config:

ListenAddress 192.0.2.10
ListenAddress [2001:db8::10]:22

You can repeat the line for several addresses, with or without a port. Without a port, sshd uses the address with each Port line. Check the syntax and restart:

sudo sshd -t && sudo systemctl restart ssh

Now ss -tlnp | grep sshd should show only those addresses, not 0.0.0.0:22.

The trap is timing. If the address does not exist yet when sshd starts, for example a VPN or Tailscale address that comes up a few seconds later, sshd fails to bind and you have no SSH at all after a reboot. For an address like that, either make the ssh service wait for that interface, or keep a second ListenAddress on something that is always there, like the LAN address. And test from a second terminal before you close the one you are working in.

A firewall rule gets you a similar result. ListenAddress is nice because the port is simply not open on the other interface, even if someone flushes the firewall.

Further reading: sshd_config man page

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link