Multi-interface Linux servers can expose SSH beyond their management network. Binding SSH to a local management IP limits its listener to the intended address, while IPv4 and IPv6 socket checks reveal any unintended bindings.
OpenSSH uses ListenAddress to choose the local addresses it listens on, and listens on every address when none is set. Each ListenAddress line adds another address, so a new line does not cancel an existing one. Ubuntu 24.04 and later start SSH from a systemd socket, which changes how the new address is applied.
The address must already belong to the server and be reachable from the computer you connect from. A working SSH login to a systemd-managed Linux server and an account with sudo access are required. ListenAddress sets which server address accepts connections, not which clients may connect; restricting remote client IP addresses is a separate task.
A wrong address can block new SSH logins. Keep your current session open, and have console access available, until a new login works.
Steps to make SSH listen on a specific IP address in Linux:
- Find the server IP address SSH should listen on.
$ ip -brief address lo UNKNOWN 127.0.0.1/8 ::1/128 ##### snipped ##### mgmt0@if13 UP 192.0.2.40/24 2001:db8:1::40/64 fe80::14c2:dcff:fea8:63f2/64 public0 UNKNOWN 198.51.100.40/24 2001:db8:2::40/64 fe80::2490:b0ff:fec6:53a9/64
The examples use 192.0.2.40. It must be a fixed address on this server, not the address of the computer you connect from.
- Back up the SSH configuration file.
$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
- Open the SSH configuration file.
$ sudoedit /etc/ssh/sshd_config
- Add a ListenAddress line for the chosen address.
ListenAddress 192.0.2.40
The line belongs above any Match block. Any other active ListenAddress line stays in effect, including lines in /etc/ssh/sshd_config.d/, so unwanted ones need changing or removing. More than one address takes one line each, such as ListenAddress 2001:db8:1::40.
- Save the file.
sudoedit writes the change when the editor closes.
- Test the SSH configuration.
$ sudo sshd -t
No output means the configuration is valid.
Related: How to test SSH server configurationDo not continue until any errors are fixed, because SSH may refuse to start.
- Reload systemd so Ubuntu picks up the new address.
$ sudo systemctl daemon-reload
Ubuntu 24.04 and later set the ssh.socket address from ListenAddress during this reload. Other distributions do not need it, and it does no harm there.
Source: Ubuntu release notes - Restart SSH to apply the new address.
$ sudo systemctl restart ssh.socket
Where ssh.socket is not in use, such as on Debian, use sudo systemctl restart ssh. On AlmaLinux, RHEL and similar systems, use sudo systemctl restart sshd.
Related: How to manage the SSH server service with systemctl - Check that SSH listens only on the chosen address.
$ ss -tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 4096 192.0.2.40:22 0.0.0.0:* LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 4096 127.0.0.54:53 0.0.0.0:*
The Local Address column shows where SSH listens on port 22, or on your own SSH port. 0.0.0.0:22 or [::]:22 means SSH still listens on every address. If an unwanted address remains, remove its ListenAddress line, or on Ubuntu any ListenStream override in /etc/systemd/system/ssh.socket.d/, then repeat the reload and restart.
- Log in from a second terminal using the chosen address.
$ ssh admin@192.0.2.40 ##### snipped ##### admin@ssh-server:~$
A shell prompt means SSH accepts new logins on that address. If the login fails, use your open session to restore the backup with sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config, then repeat the reload and restart.