What another port changes
| Port 22 | Your own port, e.g. 48731 | |
|---|---|---|
| bot login attempts | hundreds an hour on a fresh server | a handful |
| protection with passwords | none | none: once found, the port gets guessed the same way |
| protection with key-only login | guessing is useless | guessing is useless |
| cost | — | -p 48731 or Port in ~/.ssh/config on every device; a firewall rule; some hotel and office networks only let known ports out |
With key-only login and CrowdSec, the log on 22 is hundreds of Connection closed by … [preauth] lines, each a
bot that didn’t get in. If that makes the log hard to read, move the port. If it doesn’t bother you, stay on 22.
How to move it on Ubuntu 24.04 and 26.04
Pick a random number from 1024 to 65535, not 2222 or 2200: scanners try those right after 22. The order is
strict: firewall first, then sshd, with the old session kept open (Keep the old session open).
sudo ufw allow 48731/tcp
echo 'Port 48731' | sudo tee /etc/ssh/sshd_config.d/01-port.conf
sudo sshd -t && sudo systemctl daemon-reload && sudo systemctl restart ssh.socket
ss -tlnp | grep -E ':(22|48731) '
# LISTEN 0 4096 0.0.0.0:48731 0.0.0.0:* users:(("sshd",…),("systemd",pid=1,…))
From a second window: ssh -p 48731 [email protected]. Once in, remove the old rule: sudo ufw delete allow OpenSSH.
If the server runs CrowdSec, the sshd scenario parses auth.log, so the port doesn’t matter.
If it doesn’t work
After the port change SSH is still on 22, and the new one refuses
- Cause
- On Ubuntu 24.04 and 26.04 the port is held by
ssh.socket, which rereadsPortonly onsystemctl daemon-reload. - Fix
sudo systemctl daemon-reload && sudo systemctl restart ssh.socket.
After the port change — Connection timed out
- Cause
- The new port is closed in ufw or in the provider’s firewall.
- Fix
- From the old open session:
sudo ufw allow 48731/tcp. Order: firewall first, thensshd.