How to Disable Root Login via SSH: Step-by-Step Guide (2026)

How to disable root login via SSH
How to disable root login via SSH
By HOC Team  |  Last updated: September 08, 2026  |  Read time: ~12 min

How to Disable Root Login via SSH: Step-by-Step Guide (2026)

If you check the authentication logs on any Linux server connected to the public internet, you will see a terrifying reality: automated bots are constantly trying to brute-force the root account.

By default, many Linux distributions allow the root user to log in directly via SSH. While this is convenient when you first provision a server, it is a massive security liability. The username "root" is universal. An attacker only needs to guess the password (or crack the key) to gain absolute, unrestricted control over your entire system.

Also read: Linux Kernel Published 440 Security Advisories— In 24hrs Here’s Why

Disabling direct root SSH login is one of the most critical, high-impact security hardening steps you can take. It forces attackers to guess both the username and the credential, instantly neutralizing 99% of automated brute-force attacks.

This guide will walk you through the exact, safe process of disabling root SSH access. We will cover the crucial prerequisite (creating a sudo user so you don't lock yourself out), the exact configuration changes, and how to verify the setup.

📊 The Reality of SSH Attacks in 2026

Average Brute Force Attempts: 5,000+ per day on a newly provisioned public IP.
Targeted Username: "root" accounts for ~65% of all SSH brute-force attempts.
Time to Disable Root: Less than 3 minutes.
Security Impact: Eliminates the majority of automated credential-stuffing attacks.
Industry Standard: Required by CIS Benchmarks, PCI-DSS, and SOC 2 compliance frameworks.

1. Why You Must Disable Root SSH Access

Before we change the configuration, let's understand the security principles behind this action.

  • Principle of Least Privilege: Users should operate with the minimum level of access required. You should log in as a standard user and only elevate to root privileges (via sudo) when a specific task requires it.
  • Accountability and Auditing: If five administrators share a server and all log in as "root", the logs will only show "root did X." If they log in as "alice", "bob", and "charlie" and use sudo, the logs will show exactly who executed the command.
  • Brute-Force Mitigation: Automated botnets scan the internet 24/7 trying to guess the password for the "root" user. By disabling root login, you make their job exponentially harder—they now have to guess both the username and the password.

2. CRITICAL PREREQUISITE: Create a Sudo User

🔴 STOP AND READ: If you disable root login and you do not have an alternative user account with sudo (or wheel) privileges, you will permanently lock yourself out of the server. Do not proceed to Step 1 until you have completed this section and verified your new user can execute administrative commands.

If you already have a non-root user with sudo access, you can skip to Step 1. If you are currently logged in as root and this is your only user, follow these steps to create a safe administrative account.

For Ubuntu / Debian Systems:

# 1. Create the new user (replace 'newadmin' with your desired username) adduser newadmin # 2. Add the user to the 'sudo' group usermod -aG sudo newadmin # 3. Switch to the new user to test su - newadmin # 4. Verify sudo access (it will ask for the new user's password) sudo whoami # Output should be: root

For CentOS / RHEL / AlmaLinux / Rocky Linux:

# 1. Create the new user adduser newadmin # 2. Add the user to the 'wheel' group (the sudo equivalent in RHEL) usermod -aG wheel newadmin # 3. Switch to the new user to test su - newadmin # 4. Verify sudo access sudo whoami # Output should be: root
🎯 Pro Tip: While still logged in as your new user, set up SSH Key-Based Authentication. Passwords can be brute-forced; SSH keys cannot. Once keys are set up, we will disable password logins entirely in the hardening section.

3. Step 1: Backup Your SSH Configuration

Before modifying any critical system configuration, always create a backup. If you make a syntax error in the SSH config, you could break the SSH daemon and lock yourself out.

Log in as your new sudo user (or root, if you haven't switched yet) and run:

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F)

This creates a timestamped backup of your current configuration.

4. Step 2: Edit the sshd_config File

Now we will modify the SSH daemon configuration file to disable root access.

sudo nano /etc/ssh/sshd_config

Use the search function in nano (Ctrl + W) to find the line that starts with PermitRootLogin. It might look like one of the following:

#PermitRootLogin yes #PermitRootLogin prohibit-password PermitRootLogin yes

Change this line so it reads exactly:

PermitRootLogin no

📋 Understanding the Options

Directive What It Does Recommendation
PermitRootLogin yes Allows root to log in via password or SSH key. Never use. Highly insecure.
PermitRootLogin prohibit-password Allows root to log in, but only via SSH keys (blocks passwords). Acceptable if you strictly use SSH keys and need direct root access for automated backups.
PermitRootLogin no Completely blocks the root user from logging in via SSH. Recommended. The most secure option.

Save the file and exit nano (Ctrl + O, Enter, then Ctrl + X).

5. Step 3: Restart the SSH Service

For the changes to take effect, you must restart the SSH daemon. The command varies slightly depending on your Linux distribution.

# For Ubuntu, Debian, and modern systems using systemd: sudo systemctl restart sshd # OR on some older/Debian systems: sudo systemctl restart ssh
⚠️ CRITICAL WARNING: DO NOT CLOSE YOUR CURRENT TERMINAL WINDOW. If you made a mistake in the configuration file or forgot to create a sudo user, restarting the SSH service with a bad config will lock you out. Keep your current session open to fix any errors.

6. Step 4: Verify the Configuration (Do Not Skip!)

This is the most important step. You must verify that the change worked before you close your current terminal session.

  1. Open a brand new terminal window on your local computer.
  2. Attempt to log in as root:
    ssh root@your_server_ip
    You should immediately receive a Permission denied or Access denied error. This confirms root login is successfully disabled.
  3. Attempt to log in as your new sudo user:
    ssh newadmin@your_server_ip
    You should successfully log in. Run sudo whoami to confirm you still have administrative privileges.

If both tests pass, congratulations! Your server is now significantly more secure. You can safely close your old terminal window.

7. Additional SSH Hardening Best Practices

Disabling root login is a great first step, but it's not the only thing you should do. To truly secure your SSH daemon, implement these additional controls:

⚡ The Ultimate SSH Hardening Checklist

  1. Disable Password Authentication: Passwords can be brute-forced. SSH keys cannot. Once you have SSH keys set up for your sudo user, open /etc/ssh/sshd_config and set PasswordAuthentication no. This makes brute-force attacks mathematically impossible.
  2. Install Fail2Ban: Fail2Ban monitors your SSH logs and automatically updates your firewall to ban IP addresses that show malicious signs (like too many failed password attempts). Command: sudo apt install fail2ban
  3. Change the Default Port: SSH runs on port 22 by default. Changing it to a non-standard port (e.g., 2222 or 49152) won't stop a determined attacker, but it will eliminate 99% of automated, dumb botnet scans. Directive: Port 49152
  4. Restrict SSH Access by IP: If you only access your server from a specific office or home IP, use your firewall (UFW, iptables, or AWS Security Groups) to only allow SSH traffic from that specific IP address.

8. Troubleshooting: "I locked myself out!"

It happens to the best sysadmins. You disabled root login, closed your terminal, and realized your new user doesn't have sudo access, or you made a typo in the sshd_config file.

How to recover access:

  • Cloud Servers (AWS, DigitalOcean, Linode, Vultr): Log into your cloud provider's web dashboard. Almost all of them offer a "Web Console," "VNC," or "Rescue Mode" feature. This gives you direct, browser-based terminal access to the server, bypassing SSH entirely. Log in as root, fix the sshd_config file, and restart the service.
  • Physical Servers: Plug in a monitor and keyboard, or use the IPMI/iLO/DRAC out-of-band management interface to access the console directly.
  • Prevention: Always keep a secondary "break-glass" SSH key or console access method documented in your company's password manager.

9. Frequently Asked Questions

Why should I disable root login via SSH?

Disabling root login via SSH is a critical security best practice. It forces attackers to guess both the username and the password (or key) to gain access, drastically reducing the success rate of brute-force attacks. It also improves auditability, as you can track exactly which administrative user performed a specific action via sudo logs.

What happens if I disable root login and don't have a sudo user?

If you disable root SSH login and do not have an alternative user with sudo (or wheel) privileges, you will permanently lock yourself out of the server via SSH. You will need to use your cloud provider's web console (VNC/Rescue mode) or physical access to the server to revert the changes in the sshd_config file.

How do I create a sudo user in Linux before disabling root?

On Ubuntu/Debian, run: sudo adduser newadmin followed by sudo usermod -aG sudo newadmin. On CentOS/RHEL/AlmaLinux, run: sudo adduser newadmin followed by sudo usermod -aG wheel newadmin. Always test the new user's sudo access in a new terminal window before closing your root session.

What is the difference between 'PermitRootLogin no' and 'prohibit-password'?

PermitRootLogin no completely blocks the root user from logging in via SSH, regardless of the authentication method. PermitRootLogin prohibit-password allows the root user to log in, but only using SSH keys (blocking password-based logins). For maximum security, no is highly recommended.

How do I verify that root login is disabled?

Open a new terminal window and attempt to SSH into the server as root (e.g., ssh root@your_server_ip). You should receive a "Permission denied" or "Access denied" error. Then, log in using your new sudo user to ensure administrative access still works.

How do I re-enable root login if I need it temporarily?

Log in as your sudo user, open the SSH config file with sudo nano /etc/ssh/sshd_config, change PermitRootLogin no back to yes (or prohibit-password), save the file, and restart the SSH service using sudo systemctl restart sshd.

About the author
Written by the HOC Team at Hackers Online Club — a cybersecurity community trusted by Linux system administrators, DevOps engineers, and security professionals since 2010. 15+ years of practical server hardening guides, infrastructure security tutorials, and enterprise compliance resources. Learn more about HOC →

Join Our Club

Enter your Email address to receive notifications | Join over Million Followers

Previous Article
what is sandbox in cybersecurity

What is a Sandbox in Cybersecurity?

Related Posts