Knowledgebase

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.

Important: keep your current SSH session open the whole time. Test the new settings in a second window before you close the first one. If something goes wrong, you can still fix it from the open session.

Before you start

  1. 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.
  2. Pick a new port between 1024 and 65535 that no other service uses, for example 2222 (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_IN in /etc/csf/csf.conf, then run csf -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

  1. Back up the file:
    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  2. Open it with nano /etc/ssh/sshd_config and set (or add) these lines:
    Port 2222
    PermitRootLogin prohibit-password
    PasswordAuthentication no
  3. prohibit-password lets root log in with a key only. If you log in as a normal user and use sudo, you can use PermitRootLogin no instead.
  4. Also check the folder /etc/ssh/sshd_config.d/. On AlmaLinux 9 a file there can override your settings, for example a line PasswordAuthentication 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.d overrides your setting. Run sshd -T | grep -i passwordauth to 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.

Need help? Open a support ticket or message us on WhatsApp +20 127 113 3655.

  • ssh port, disable root login, sshd_config, server security, almalinux, ssh keys

Was this answer helpful?

0 Users Found This Useful