Bots try to log in on port 22 of every server, all day. Moving SSH to another port does not make the server secure by itself, but it keeps most of this noise out of your logs. On AlmaLinux and Rocky Linux, SELinux lets SSH use only the ports meant for it, so a new port needs one more command than on Ubuntu. The steps were tested in test machines with AlmaLinux 10 and Rocky Linux 9 and on a dedicated server with AlmaLinux 9; run the commands as root.

⚠ Do not lock yourself out

Keep port 22 open until you have logged in on the new port, and keep your current SSH window open while you work. If you do get locked out, the console in the Client Area or the rescue system gets you back in.


1. Add the New Port

Choose a port between 1024 and 65535 that no other program uses, for example 2222. Create the file /etc/ssh/sshd_config.d/05-port.conf with both ports for now:

Port 22
Port 2222

Check the configuration; it prints nothing when it is fine:

sshd -t

2. Allow the Port in SELinux

semanage port -a -t ssh_port_t -p tcp 2222
semanage port -l | grep ssh_port_t

The second line must show ssh_port_t tcp 2222, 22. If the command semanage is not found, install it with dnf install policycoreutils-python-utils. If SELinux is switched off (getenforce answers Disabled, as after our installation), the command changes nothing until you switch SELinux on, and gives no error.

ℹ Without this step

SSH restarts without an error message but keeps listening on port 22 only. The reason is in its log (journalctl -u sshd): Bind to port 2222 on 0.0.0.0 failed: Permission denied. More about such blocks: SELinux on AlmaLinux and Rocky Linux.

3. Open the Port in the Firewall

Allow the new port in firewalld before you switch (Protect Your AlmaLinux or Rocky Linux Server With firewalld and Fail2ban):

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

With Fail2ban, tell its SSH jail about the port: in /etc/fail2ban/jail.local, under [sshd], add port = 2222 and run systemctl restart fail2ban.

4. Apply It

systemctl restart sshd
ss -ltn | grep -E ':22 |:2222 '

The second command shows that SSH now listens on both ports, for IPv4 and IPv6.

5. Log In on the New Port

In a new window on your computer, keeping the old one open:

ssh -p 2222 root@your-server-IP

In PuTTY, enter the port in the Port field; in FileZilla and WinSCP, in their port field. At the first login on the new port, your SSH program may ask again whether you trust the server: the server is the same, answer yes.

6. Close Port 22

When the login on the new port works, remove Port 22 from /etc/ssh/sshd_config.d/05-port.conf, so that only Port 2222 is left, and restart SSH:

systemctl restart sshd

Then close the old port in the firewall (if you allowed SSH only from your addresses, also remove that rule; see the firewall guide):

firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload

Back to Port 22

Delete the file 05-port.conf, allow SSH in the firewall again and remove the extra port:

rm /etc/ssh/sshd_config.d/05-port.conf
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --remove-port=2222/tcp
firewall-cmd --reload
semanage port -d -t ssh_port_t -p tcp 2222
systemctl restart sshd

SSH then listens on port 22 only.


Related Guides

View all AlmaLinux and Rocky Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)