How to change the SSH port and disable root password login
This guide explains how to change the SSH port and disable root password login on an AlmaLinux VPS or dedicated server. These two changes cut down the automated password-guessing attacks that hit every public server, and they take only a few minutes.
Before you start
- Make sure you can log in with an SSH key (see How to connect to your VPS or server via SSH). If you disable password login without a working key, you will lock yourself out.
- Pick a new port between
1024and65535that no other service uses, for example2222(use your own number).
Step 1: Open the new port in the firewall first
If you skip this step, you will lose access after the restart.
- firewalld (plain AlmaLinux):
firewall-cmd --permanent --add-port=2222/tcp firewall-cmd --reload - CSF: add the port to
TCP_INin/etc/csf/csf.conf, then runcsf -r. - Imunify360 or a cloud/provider firewall: allow the port there too.
Step 2: Tell SELinux about the port (only if SELinux is enforcing)
Check with getenforce. If it prints Enforcing, run:
dnf install -y policycoreutils-python-utils
semanage port -a -t ssh_port_t -p tcp 2222
cPanel servers normally have SELinux disabled, so this step is usually not needed there.
Step 3: Edit the SSH configuration
- Back up the file:
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak - Open it with
nano /etc/ssh/sshd_configand set (or add) these lines:Port 2222 PermitRootLogin prohibit-password PasswordAuthentication no prohibit-passwordlets root log in with a key only. If you log in as a normal user and usesudo, you can usePermitRootLogin noinstead.- Also check the folder
/etc/ssh/sshd_config.d/. On AlmaLinux 9 a file there can override your settings, for example a linePasswordAuthentication yes. Edit or comment out conflicting lines.
Step 4: Test and restart
sshd -t
systemctl restart sshd
sshd -t prints nothing if the file is valid. Only restart when it is clean.
Step 5: Test from a new window
ssh -p 2222 root@203.0.113.10
If you get in with your key, everything works. You can now close the old session. If not, go back to the open session and restore the backup with cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config and restart sshd.
On a cPanel/WHM server
WHM has tools for this too: SSH Password Authorization Tweak turns password login on or off, and Manage root's SSH Keys handles keys. Remember to update any backup or monitoring tool that connects over SSH with the new port.
Common problems
- Connection timed out on the new port: the firewall or SELinux step was missed.
- Still asked for a password: a file in
sshd_config.doverrides your setting. Runsshd -T | grep -i passwordauthto see the active value. - Locked out completely: use the VNC/console of your VPS if available, or open a ticket and we will help.
For more hardening ideas, read How to secure your Linux server.
Frequently asked questions
Does changing the port really improve security?
It does not replace strong keys, but it removes most automated scans from your logs and reduces noise. Disabling password login is the bigger win.
Can EgWeb do this for me?
Yes. If you have a managed VPS or server, open a ticket and our team can apply these settings safely.