To disable root SSH login safely, set up a working sudo account with key login first, then change one line in sshd_config and test it in a second terminal window before you log out. Root is the first account every automated scanner tries, so closing that door cuts a large share of brute-force noise. The risk is making the change before you have a tested way back in, so this guide starts with the recovery plan, then the change, then the lockout fixes.
Root login over SSH is convenient on a fresh server, and that convenience is exactly why it is a target. Bots do not know your username or password policy, but they know root exists on every Linux server. When you harden SSH on Ubuntu 24.04 with key-only login and fail2ban, disabling root is one of the highest-value steps. Keep root available on the local console for emergencies, but stop letting it log in over the network.
Before You Disable Root SSH Login: Build Your Way Back In
Do not edit sshd_config until you can answer yes to each item below.
- You have a non-root user with sudo access, and you have logged in as that user with an SSH key.
- You know how to reach your provider console, rescue mode, or VNC access, and you have tested that the login for it works.
- You have a recent snapshot or backup you can restore. A snapshot taken before the change is the fastest undo button you own.
- You have a copy of your SSH private key stored somewhere other than the server itself, with a passphrase you remember.
Open two terminal windows. Use one to make the change and the other to test a fresh login. If the test login fails, the first window still has a live root session, and you can undo the edit at once. Never make this change from your only session and then log out to test it.
If you have not set up key login yet, sort that out before you touch root access. Our guide to use SSH keys instead of passwords on your VPS covers key generation and checking that the key works before passwords are turned off. A sudo user whose key you have never tested is not a fallback at all.
Set Up a Sudo User and Test SSH Key Login
This sudo user setup takes a few minutes. Log in as root, create the user, give it sudo rights, and add your public key to its authorized_keys file.
adduser deploy usermod -aG sudo deploy mkdir -p /home/deploy/.ssh cp /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys chown -R deploy:deploy /home/deploy/.ssh chmod 700 /home/deploy/.ssh chmod 600 /home/deploy/.ssh/authorized_keys
Copying root’s authorized_keys file only works if root already logs in with a key. If root still uses a password, generate a key pair with ssh-keygen, then copy the public key with ssh-copy-id deploy@your-server-ip. Test the result before you go further: open a new terminal, run ssh deploy@your-server-ip, then run sudo whoami and confirm it prints root. If either step fails, stop here and fix it.
On a shared server, give each person their own account instead of sharing one deploy user. Our guide on how to create sudo users and disable root login on a new VPS walks through per-person accounts without sharing keys or passwords.
How to Disable Root SSH Login Step by Step
With the sudo user tested, the actual change is small. Open /etc/ssh/sshd_config and find the PermitRootLogin line. On Ubuntu 24.04 it often reads PermitRootLogin prohibit-password, which blocks password login for root but still allows key login. Change it to this:
PermitRootLogin no
If the line is commented out with a hash, remove the hash and set the value to no. Check that there is only one active PermitRootLogin line in the file, because sshd uses the first match it finds, so a stray duplicate can silently undo your edit.
Before you reload the service, check the config syntax:
sshd -t systemctl reload sshd
Use reload rather than restart. A reload applies the new config to new connections while leaving your current session alone. If sshd -t reports an error, fix the file and run the test again. Do not reload a config that fails the syntax check.
Now test from the second terminal window. Try ssh root@your-server-ip and confirm it is refused, then log in as your sudo user and confirm that still works. Only when both tests behave as expected should you close the root session. If root can still log in, check for a duplicate PermitRootLogin line and confirm the service actually reloaded. The sshd_config PermitRootLogin setting only takes effect after a reload, so a forgotten reload is the most common reason the change appears not to work.
What to Do If You Get Locked Out of VPS Access
Work through these recovery paths in order, from least destructive to most.
1. Use the provider console
Most VPS providers offer a web console, sometimes labelled VNC or emergency console, that gives you a login prompt without SSH. Log in as root there with the root password. From the console, you can edit /etc/ssh/sshd_config, fix permissions on .ssh directories, or add a missing public key. This is the fastest recovery when SSH itself is the only broken piece.
2. Boot into rescue mode and fix the disk
If the console does not let you log in either, boot the server into rescue mode from your provider panel. Rescue mode starts a small temporary system and mounts your real disk, usually under /mnt. From there you can edit the real sshd_config, reset a user password with chroot, or repair file permissions. A typical fix: mount the disk, edit /mnt/etc/ssh/sshd_config to set PermitRootLogin yes temporarily, or add your public key to /mnt/home/deploy/.ssh/authorized_keys, then reboot and log in. Set PermitRootLogin back to no once you are in and your sudo user works again.
3. Restore a snapshot
If you cannot find the fault, restore the snapshot you took before the change. You will lose any changes made after the snapshot, but it returns a known-good server in minutes. This is why the snapshot belongs in the checklist before the change, not after.
4. Rebuild only as a last resort
Rebuilding the server from scratch is the final option. It wipes the server, so only do it when you have backups of the data and site files elsewhere. After a lockout, read our walkthrough on how to audit SSH logins on your VPS with lastlog and journalctl, because the same logs that show failed root attempts will show whether anyone else tried to get in while you were locked out.
Common Lockout Causes and How to Fix Each One
Most lockouts after this change have one of four causes. Wrong permissions on the .ssh directory or the authorized_keys file are the most common: the directory must be 700 and the file 600, owned by the user. A key copied with a trailing space or a line break in the middle will also fail, so compare the key on the server with the public key on your computer. A second cause is a duplicate PermitRootLogin line further down the file. A third is forgetting to reload sshd after the edit. The fourth is a firewall rule that blocks the SSH port, which looks like a login failure but is a network problem. If you run a firewall, read our guide on how to set up UFW firewall rules that do not lock you out of your VPS before you change SSH settings.
Disabling root login does not disable the root account, and PermitRootLogin no does not block key login for other users. If you also want to stop password login entirely, set PasswordAuthentication no in the same file, but only after key login is tested for every account that needs it.
After the Change: Keep the Server Hardened
Disabling root is one layer, not a finished job. Turn on fail2ban to slow brute-force attempts against your sudo user, keep the system updated, and review login logs each week. Failed root attempts should drop to zero at the SSH level, because root can no longer log in at all, while attempts against other accounts will still appear and deserve attention.
Write down your recovery path and keep it with your server notes: provider console URL and login, rescue mode steps, snapshot schedule, and where the SSH private key is stored. Disable root SSH login once, test it properly, and the server is both harder to attack and easier to get back into, because the way back in was built before the door was closed.