Dev Notes

How to Disable Root Login over SSH and Harden sshd_config (Ubuntu, Debian, RHEL)

Disable Root Login over SSH: Harden sshd_config without locking yourself out
Dev Notes February 6, 2024 Tamir Khan No Comments

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 deploy

On AlmaLinux, Rocky Linux or RHEL:

sudo adduser deploy
sudo passwd deploy
sudo usermod -aG wheel deploy

Step 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-ip

If 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_keys

Step 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.conf

Add:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deploy

Replace 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.

Talk to my team WhatsApp me

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 sshd

On 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?

MeasureWhyHow
Restrict by IPBlocks the internet from even reaching SSHCloud security group or ufw allow from 203.0.113.10 to any port 22
Fail2banBans IPs after failed attemptssudo apt install fail2ban with the sshd jail enabled
Keep OpenSSH updatedPatches serious vulnerabilitiesEnable unattended security updates
Use a bastion or VPNNo public SSH at allAWS Systems Manager Session Manager, Tailscale, WireGuard
Changing the portReduces log noise only, not real securityOptional; 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.

Tamir Khan: business growth and digital strategy consultant in Mumbai. Since 2011 he has built websites, SEO, Google Ads, cloud and automation systems for businesses in India, the UK, Canada, the USA and the UAE. Strategy comes from Tamir; delivery is by the NexGeno team. About Tamir · LinkedIn

Last updated: 25 November 2026

Related Posts

Quick Links

Newsletter

I am the Founder and Chief Technology Officer at NexGeno Technology Pvt Ltd. With 12+ years of experience, I specialize in digital strategy, business automation, AI integration, web development, and marketing.I've worked with startups, SMEs, and global clients, offering tailored strategies that deliver measurable results.

© 2025 Tamir Khan - All Rights Reserved | Managed by NexGeno