Change the SSH Port on AlmaLinux 9, Rocky Linux 9, Ubuntu 24.04 and Debian 12
Move SSH off port 22 safely, covering SELinux and firewalld on AlmaLinux 9 and Rocky Linux 9 and the ssh.socket step on Ubuntu 24.04.
Moving SSH off port 22 will not stop a determined attacker, but it removes most of the automated login attempts that fill your logs. This guide changes the port safely on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9, where SELinux and firewalld both have to be told about the new port, and on Ubuntu 24.04, Ubuntu 22.04 and Debian 12. The guides written for CentOS 6 and 7 no longer apply as they stand: both releases are end-of-life, and the service iptables save step they relied on is gone.
The examples use port 2222. Pick any unused port you like and substitute it throughout.
Before you start
- You need a user with
sudorights and an open SSH session, which you keep open until the end. - Make sure you can reach your provider's web console in case you lock yourself out.
- If your provider has a network firewall or security group in front of the server, allow the new port there as well.
- Check that nothing else is using the port. This prints nothing when it is free:
sudo ss -tln | grep ':2222 '
The safe order is always the same: allow the new port everywhere first, make SSH listen on both ports, test the new one, and only then close port 22.
AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9
1. Tell SELinux about the port
SELinux only lets sshd bind to ports labelled ssh_port_t. Without this step the service fails with a "permission denied" bind error. Install the tool that provides semanage and add the label:
sudo dnf install -y policycoreutils-python-utils
sudo semanage port -a -t ssh_port_t -p tcp 2222
Confirm it:
sudo semanage port -l | grep ssh_port_t
2. Open the port in firewalld
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
3. Make sshd listen on both ports
The main configuration file includes every *.conf file in /etc/ssh/sshd_config.d/, so put the change in its own file instead of editing sshd_config:
printf 'Port 22\nPort 2222\n' | sudo tee /etc/ssh/sshd_config.d/10-port.conf
Check the syntax, then restart the service:
sudo sshd -t
sudo systemctl restart sshd
Continue with "Test the new port" below.
Ubuntu 24.04
Ubuntu 24.04 starts SSH through systemd socket activation: ssh.socket owns the listening port and starts sshd on demand. The socket's port is generated from sshd_config, but only when systemd reloads its configuration. Restarting ssh.service alone leaves SSH on port 22, which is the usual reason a port change "does nothing" on this release.
1. Open the port in UFW
Skip this if UFW is inactive (sudo ufw status):
sudo ufw allow 2222/tcp
2. Set both ports
printf 'Port 22\nPort 2222\n' | sudo tee /etc/ssh/sshd_config.d/10-port.conf
sudo sshd -t
3. Regenerate the socket and restart it
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
To see which ports the socket now holds:
systemctl status ssh.socket
Ubuntu 22.04 and Debian 12
These releases run sshd as an ordinary service by default. Check first, because the Ubuntu 24.04 procedure applies if the socket unit is in use:
systemctl is-active ssh.socket
If the answer is inactive, open the port in your firewall (sudo ufw allow 2222/tcp if you use UFW), then set both ports and restart the service. Note that the service is called ssh here, not sshd:
printf 'Port 22\nPort 2222\n' | sudo tee /etc/ssh/sshd_config.d/10-port.conf
sudo sshd -t
sudo systemctl restart ssh
Test the new port
On the server, confirm that SSH is listening on both ports:
sudo ss -tlnp | grep -E ':(22|2222) '
From your own computer, open a new session on the new port without closing the existing one:
ssh -p 2222 deploy@203.0.113.10
If it connects, the firewall, SELinux and SSH configuration are all correct. If it does not, your original session is still open: recheck the firewall rule, any provider-level firewall, and on the RHEL family the SELinux label.
Close port 22
Once the new port works, remove the old one from the SSH configuration:
echo 'Port 2222' | sudo tee /etc/ssh/sshd_config.d/10-port.conf
sudo sshd -t
Apply it the same way as before:
- RHEL family:
sudo systemctl restart sshd - Ubuntu 24.04:
sudo systemctl daemon-reload, thensudo systemctl restart ssh.socket - Ubuntu 22.04 / Debian 12:
sudo systemctl restart ssh
Then close port 22 in the firewall. On the RHEL family:
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --reload
With UFW, list the rules and delete the ones for port 22 or OpenSSH by number:
sudo ufw status numbered
sudo ufw delete 1
Replace 1 with the number shown for the rule, and run status numbered again before each delete because the numbers shift. Open one more new session on port 2222 to confirm you can still get in before you close the original session.
Connecting afterwards
To avoid typing -p 2222 every time, add the port to ~/.ssh/config on your own computer:
Host myserver
HostName 203.0.113.10
User deploy
Port 2222
After that, ssh myserver is enough. scp takes the port with an upper-case -P, and tools such as rsync and git pick it up from the same config file. Update anything else that connects over SSH: backup jobs, deployment scripts, monitoring checks and the port setting of your Fail2ban SSH jail.
Troubleshooting
- sshd fails to start on AlmaLinux or Rocky Linux with "Bind to port 2222 failed: Permission denied": the SELinux label is missing. Run the
semanage port -acommand from step 1 and restartsshd. - Ubuntu 24.04 still listens on 22 only: you restarted
ssh.serviceinstead of runningdaemon-reloadand restartingssh.socket. - The setting is ignored:
sshduses the first value it reads for most options, butPortmay be given several times and all values are used. Look for anotherPortline withsudo sshd -T | grep -i '^port', which prints the effective ports. - Connection times out from outside but works locally: a firewall is still blocking the port, either on the server or at your provider.
Frequently asked questions
Does changing the SSH port make the server secure?
Only slightly. It hides the service from bots that scan port 22, but a full port scan finds it in seconds. Key-based logins with password authentication disabled, no direct root login, and Fail2ban do far more. Treat the port change as noise reduction on top of those.
Which port number should I choose?
Any port that no other service on the machine uses and that your own networks do not block outbound. Avoid ports assigned to software you might install later, such as 3306 or 8080.
Can I remove port 22 from SELinux as well?
There is no need. Port 22 is part of the base SELinux policy, and leaving the label in place opens nothing: with sshd no longer listening there and the firewall closed, the port is unreachable.
Will existing SSH sessions drop when I restart the service?
No. Restarting sshd or ssh.socket affects the listener for new connections; sessions that are already established keep running, which is why keeping one open is a reliable safety net.