Is changing the SSH port worth doing?
Yes, as a noise filter, not as a lock. A machine with a public IPv4 address receives automated login attempts on port 22 within minutes of going online. Moving sshd to another port makes most of those scripts miss, so your logs stay readable and real problems stand out. It does nothing against an attacker who scans all ports, which is why key-only login remains the real protection. Treat the new port as housekeeping on top of good authentication.
Which port should I choose?
Pick something between 1024 and 49151 that no other service on the box uses, such as 2207 or 4622. Avoid well-known alternates like 2222, which scanners try right after 22. Confirm the port is free before you start:
ss -Htln 'sport = :2207'
Empty output means nothing listens there yet. Ports below 1024 also work, but they offer no advantage, and some corporate networks only allow a short list of outbound ports, so check you can reach your choice from the networks you work from.
Why open the firewall before editing sshd?
Because the safest order is the one where every intermediate state still lets you in. Our machines have no out-of-band console in the offer, so a box that refuses SSH can only be recovered by a reinstall from the panel, which erases the disks. Allow the new port first:
| System | Firewall command |
|---|---|
| Ubuntu with ufw | ufw allow 2207/tcp comment 'ssh new' |
| Rocky Linux, AlmaLinux, CentOS Stream | firewall-cmd --add-port=2207/tcp --permanent; firewall-cmd --reload |
| Debian with nftables | nft add rule inet filter input tcp dport 2207 accept |
| FreeBSD with pf | add 2207 to the pass rule for ssh, then pfctl -f /etc/pf.conf |
The Enterprise Linux images also run SELinux in enforcing mode, which only lets sshd bind to ports labeled for it. Add the label, or sshd will fail to start on the new port:
dnf -y install policycoreutils-python-utils semanage port --add --type ssh_port_t --proto tcp 2207 semanage port -l | grep ssh_port_t
Where do I set the new port?
In a drop-in file, so package updates never overwrite your change. Current OpenSSH packages on Ubuntu, Debian and the Enterprise Linux images include /etc/ssh/sshd_config.d/*.conf near the top of the main file. Keep port 22 during the switch, so two ports listen at once:
printf 'Port 22\nPort 2207\n' > /etc/ssh/sshd_config.d/10-port.conf sshd -t && echo config-ok
sshd -t checks the syntax and prints nothing when it is happy. A typo caught here costs seconds; a typo caught after the restart can cost the machine.
How do I restart sshd on each system?
Ubuntu 24.04 starts SSH through systemd socket activation. Its generator reads the Port lines from your configuration, so reload systemd and restart the socket rather than only the service:
systemctl daemon-reload systemctl restart ssh.socket
On Debian 13 and Debian 12 run systemctl restart ssh. On Rocky Linux, AlmaLinux and CentOS Stream the unit is called sshd, so use systemctl restart sshd. FreeBSD 14 uses service sshd restart. In every case, check that both ports are listening with ss -tlnp | grep -E ':(22|2207) ', or sockstat -4l | grep sshd on FreeBSD.
How do I test before closing port 22?
Leave your current session open and start a fresh terminal on your own computer:
ssh -p 2207 -o ConnectTimeout=8 admin@203.0.113.25
Only when that login works should you remove Port 22 from the drop-in, restart again and delete the old firewall rule. To skip typing -p forever, add the port to a host block in ~/.ssh/config, explained in The SSH config file.
What else needs to know about the new port?
- fail2ban: set
port = 2207in the sshd jail, or it will ban on the wrong port. - Backup jobs and deploy tools: rsync, Ansible inventories and CI runners all carry the port somewhere.
- Monitoring checks that probe SSH reachability.
- Team documentation, so nobody spends an hour on a "connection refused" that is really an old port.
Keep Reading
Harden SSH on Linux
A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.
OpenThe SSH config file
Turn long ssh command lines into short aliases with ~/.ssh/config: hosts, ports, keys, jump hosts, keepalives and connection sharing.
OpenFix "Connection refused"
Work out why port 22 says no: sshd stopped, wrong port, a firewall reject rule, a loopback-only listener or a machine still installing.
OpenRocky Linux
Rocky Linux 9, a free Red Hat compatible rebuild focused on staying bug for bug identical to its upstream.
OpenFAQ
Quick Answers
It reduces automated noise but does not stop a targeted attacker. Combine it with key-only login and a firewall.
SELinux only allows ports labeled ssh_port_t. Add the label with semanage port -a -t ssh_port_t -p tcp followed by your port number.
Ubuntu 24.04 uses socket activation. Run systemctl daemon-reload and then systemctl restart ssh.socket.
There is no out-of-band console in the offer, so the fix is a reinstall from the panel, which erases the disks. Keep a session open until the new port is tested.
Yes. Add one Port line per port. Keeping 22 open during the switch is the safest way to move.