Enable Root Login via SSH on Ubuntu

Secure Shell (SSH) is a widely used protocol that allows secure remote access to dedicated Linux servers, including Ubuntu. By default, Ubuntu disables direct root login over SSH for security reasons. Instead, users typically log in with a regular user account and use sudo to perform administrative tasks. However, there may be scenarios where enabling root login via SSH is necessary, such as automated scripts or specific administrative requirements.

This guide covers how to enable root login via SSH on Ubuntu, the potential security risks involved, and best practices to keep your server secure.

Table of Contents

  1. Why Is Root Login Disabled by Default?

  2. Risks of Enabling Root SSH Login

  3. Preparing Your Ubuntu Server

  4. Steps to Enable Root Login via SSH

  5. Testing SSH Root Access

  6. Best Security Practices When Using Root SSH Access

  7. Troubleshooting Common Issues

  8. Conclusion

1. Why Is Root Login Disabled by Default?

Ubuntu disables direct root SSH login by default to reduce the risk of unauthorized access. The root account has unrestricted access to the system, so if an attacker gains access to it, they can fully control your server. Instead, Ubuntu encourages using a regular user account with sudo privileges, which limits exposure and provides an audit trail of commands run with elevated rights.

Disabling root login adds an important layer of security, making it harder for attackers to guess the root password or exploit vulnerabilities.

2. Risks of Enabling Root SSH Login

Enabling root login over SSH can introduce significant risks, including:

  • Brute force attacks: Attackers often target root accounts with common passwords.

  • Privilege escalation: Once root access is compromised, the entire server is vulnerable.

  • Lack of audit trails: Actions performed via direct root login are harder to track to a specific user.

Because of these risks, it's critical to implement strong security measures when enabling root SSH access, such as using SSH key authentication and disabling password login.

3. Preparing Your Ubuntu Server

Before enabling root SSH login, ensure:

  • You have physical or console access to your server in case you get locked out.

  • Your user account has sudo privileges to make configuration changes.

  • Your server is updated with the latest security patches:


sudo apt update && sudo apt upgrade -y


  • SSH service is installed and running:


sudo systemctl status ssh


If not installed, you can install it via:


sudo apt install openssh-server


4. Steps to Enable Root Login via SSH

Follow these steps carefully to enable root login via SSH on Ubuntu:

Step 1: Set a Password for the Root User (if not set)

By default, Ubuntu disables the root user password. Set a strong password for root by running:


sudo passwd root


Enter and confirm the new password.

Step 2: Backup the SSH Configuration File

Before making changes, back up the existing SSH daemon configuration:


sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak


Step 3: Edit SSH Daemon Configuration

Open the SSH configuration file in a text editor, for example:


sudo nano /etc/ssh/sshd_config


Find the following line:

plaintext


PermitRootLogin prohibit-password


Or it might be set to:

plaintext


PermitRootLogin no


Change this line to:

plaintext


PermitRootLogin yes


Note: To improve security, consider using PermitRootLogin without-password or PermitRootLogin prohibit-password, which allows root login only with SSH keys, not passwords.

Step 4: (Optional) Enable Root Login with SSH Keys Only

For enhanced security, disable root password login but allow SSH keys by using:

plaintext


PermitRootLogin prohibit-password


This way, root login is permitted only with SSH key authentication, which is far more secure than password-based login.

Step 5: Save and Close the File

  • In Nano, press CTRL+O, then Enter to save.

  • Press CTRL+X to exit the editor.

Step 6: Restart the SSH Service

Apply the changes by restarting the SSH service:


sudo systemctl restart ssh


5. Testing SSH Root Access

Test the root SSH login from your local machine:

ssh root@your-server-ip


  • If password authentication is enabled, enter the root password you set earlier.

  • If using SSH key authentication, ensure your public key is added to /root/.ssh/authorized_keys on the server.

If the login is successful, you now have direct root SSH access.

6. Best Security Practices When Using Root SSH Access

If you must enable root login via SSH, follow these best practices to reduce risks:

Use SSH Key Authentication

Avoid using passwords; configure SSH keys for root login:

  • Generate SSH key pair on your local machine:

ssh-keygen -t rsa -b 4096


  • Copy the public key to the root user's authorized keys:

sudo mkdir -p /root/.ssh

sudo nano /root/.ssh/authorized_keys

# Paste your public key here and save

sudo chmod 600 /root/.ssh/authorized_keys

sudo chmod 700 /root/.ssh


Disable Password Authentication

Edit /etc/ssh/sshd_config:

PasswordAuthentication no


Restart SSH service:


sudo systemctl restart ssh


Change the Default SSH Port

Change the default SSH port (22) to a custom port to reduce automated attacks:

Edit /etc/ssh/sshd_config:

Port 2222


Restart SSH.

Use Firewall Rules

Limit SSH access with ufw:

sudo ufw allow 2222/tcp

sudo ufw enable


Use Fail2ban or Similar

Install Fail2ban to block IPs after repeated failed login attempts:

sudo apt install fail2ban

sudo systemctl enable fail2ban

sudo systemctl start fail2ban


7. Troubleshooting Common Issues

Unable to Login as Root

  • Check SSH configuration syntax:

sudo sshd -t


  • Ensure root password is set if using password authentication.

  • Verify that the SSH service was restarted.

  • Confirm firewall rules allow SSH traffic on the correct port.

  • Check /var/log/auth.log for SSH login errors:

sudo tail -f /var/log/auth.log


Locked Out After Configuration Change

If locked out due to configuration errors:

  • Access your server via console or out-of-band management (e.g., hosting provider's control panel).

  • Restore SSH config backup:

sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config

sudo systemctl restart ssh


8. Conclusion

Enabling root login via SSH on Ubuntu is straightforward but should be approached with caution due to security risks. When properly configured-especially with SSH key authentication and other security best practices-it can be a useful tool for system administrators. Always weigh the convenience of root SSH login against potential vulnerabilities and ensure you have alternative access methods in place.

For most users, continuing to use a sudo-enabled regular user account remains the safest approach to managing Ubuntu server hosting.

Was this answer helpful? #0 #0
 

Did We Miss Out on Something?

Relax, we have you covered. At Go4hosting, we go the extra mile to keep our customers satisfied. We are always looking out for opportunities to offer our customers “extra” with every service. Contact our technical helpdesk and we’d be more than happy to assist you with your Cloud hosting, Colocation Server, VPS hosting, dedicated Server or reseller hosting setup. Get in touch with us and we’d cover all your hosting needs, however bizarre they might be.

Related Questions

Submit your Query

  • I'm not a robot

Browse by ServicesBrowse by Services

Resource Library

What is Cloud Computing

Understand the term cloud computing, the ongoing trend, its playing field, future growth and how industry...

Myths about Cloud Computing

Cloud computing, in the recent years, has become a subject of significant discussion among the industry experts.

Download Now

Did We Miss Out on Something?

Relax, we have you covered. At Go4hosting, we go the extra mile to keep our customers satisfied. We are always looking out for opportunities to offer our customers “extra” with every service. Contact our technical helpdesk and we’d be more than happy to assist you with your Cloud hosting, Colocation Server, VPS hosting, dedicated Server or reseller hosting setup. Get in touch with us and we’d cover all your hosting needs, however bizarre they might be.

Submit Query

Please fill in the form below and we will contact you within 24 hours.