Quick answer
To disable root login over SSH, first create a normal user with sudo rights and confirm you can log in as that user with an SSH key. Then set PermitRootLogin no (and PasswordAuthentication no) in /etc/ssh/sshd_config or a file in /etc/ssh/sshd_config.d/, check the file with sudo sshd -t, and reload SSH with sudo systemctl reload ssh (Ubuntu/Debian) or sudo systemctl reload sshd (RHEL family).
Key takeaways
- Never close your current session until a new login with the new user works.
- Modern Ubuntu and Debian read /etc/ssh/sshd_config.d/*.conf first, and the first value set wins.
- Key-based login with passwords disabled stops almost all brute-force attacks.
- Always run sudo sshd -t before reloading. A syntax error can block new logins.
- Add Fail2ban or your cloud firewall to limit SSH to known IP addresses.
Every server on the internet gets constant automated login attempts against the root account. Disabling root login over SSH, and replacing passwords with keys, removes the easiest target. This guide walks through the safe order of steps so you don’t lock yourself out, and includes a hardened configuration you can reuse.
Why should you disable root login over SSH?
- Root is a known username. Attackers only need to guess the password.
- Root has unlimited power. One compromised login means the whole server is compromised.
- Accountability. Named users with sudo leave an audit trail of who did what.
On most cloud images (AWS, DigitalOcean, Google Cloud), root password login is already off, and OpenSSH’s default is PermitRootLogin prohibit-password, which still allows root with a key. Setting it to no closes that too.
Step 1: Create a user with sudo rights
On Ubuntu or Debian:
sudo adduser deploy
sudo usermod -aG sudo deployOn AlmaLinux, Rocky Linux or RHEL:
sudo adduser deploy
sudo passwd deploy
sudo usermod -aG wheel deployStep 2: Add your SSH key for the new user
From your own computer, generate a modern key if you don’t have one, then copy it to the server:
ssh-keygen -t ed25519 -C "[email protected]"
ssh-copy-id deploy@your-server-ipIf ssh-copy-id isn’t available (for example on Windows), paste the contents of your id_ed25519.pub into /home/deploy/.ssh/authorized_keys and set permissions:
sudo mkdir -p /home/deploy/.ssh
sudo nano /home/deploy/.ssh/authorized_keys
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keysStep 3: Test the new login before changing anything
Keep your current session open. In a new terminal, run ssh deploy@your-server-ip, then sudo -v. Only continue if both work.
Step 4: Edit the SSH configuration
On Ubuntu 22.04+, Debian 12+ and recent RHEL-family releases, the main file includes /etc/ssh/sshd_config.d/*.conf near the top, and for most settings the first value found wins. Cloud images often ship a file there (such as 50-cloud-init.conf) that sets PasswordAuthentication yes. Create your own file with a low number so it is read first:
sudo nano /etc/ssh/sshd_config.d/00-hardening.confAdd:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deployReplace deploy with your username(s). AllowUsers is optional but powerful: only listed users can log in.
Rather have experts handle this?
My team at NexGeno manages servers, cloud, security and WordPress for businesses in India and abroad.
Step 5: Validate and reload SSH
sudo sshd -t
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'The first command prints nothing if the syntax is fine. The second shows the values SSH will actually use. Then reload:
# Ubuntu / Debian
sudo systemctl reload ssh
# AlmaLinux / Rocky / RHEL
sudo systemctl reload sshdOn Ubuntu 24.04, SSH may be socket-activated. If you also change the port, run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket.
Step 6: Confirm root login is blocked
From a new terminal, ssh root@your-server-ip should now fail with “Permission denied”. Your deploy login should still work.
What else should you do to secure SSH?
| Measure | Why | How |
|---|---|---|
| Restrict by IP | Blocks the internet from even reaching SSH | Cloud security group or ufw allow from 203.0.113.10 to any port 22 |
| Fail2ban | Bans IPs after failed attempts | sudo apt install fail2ban with the sshd jail enabled |
| Keep OpenSSH updated | Patches serious vulnerabilities | Enable unattended security updates |
| Use a bastion or VPN | No public SSH at all | AWS Systems Manager Session Manager, Tailscale, WireGuard |
| Changing the port | Reduces log noise only, not real security | Optional; update firewall and SELinux rules if you do |
What if I lock myself out?
Use your provider’s console access: AWS EC2 Serial Console or Session Manager, DigitalOcean Droplet Console, Google Cloud serial console, or your host’s VNC/KVM. Log in, fix the file in /etc/ssh/sshd_config.d/, run sshd -t and reload.
Frequently asked questions
What is the default PermitRootLogin setting?
Current OpenSSH defaults to PermitRootLogin prohibit-password, which blocks root password logins but still allows root with an SSH key. Many cloud images also disable root login entirely. Set it to no to block root completely.
Why does PasswordAuthentication yes still apply after I changed sshd_config?
A file in /etc/ssh/sshd_config.d/, often from cloud-init, is setting it first. For most options the first value read wins, so put your settings in a file such as 00-hardening.conf and check with sudo sshd -T.
Is it safe to restart SSH while connected?
Reloading keeps existing sessions open, and so does a restart on most systems, but always keep one session open and test a new login before closing it.
Should I use ed25519 or RSA keys?
Use ed25519 for new keys. It is fast, secure and widely supported. If you need RSA for an older system, use at least 3072 bits.