By default, Ubuntu disables root login via SSH for security reasons. However, in some cases, you may need to enable it for administrative purposes. This guide will walk you through the steps to enable root login via SSH securely in Ubuntu.

Step 1: Switch to Root User

Before enabling root login, ensure that you have root access. You can switch to the root user with:

sudo -i

If you haven’t set a root password yet, you can create one using:

sudo passwd root

Enter and confirm the new root password.

Step 2: Edit the SSH Configuration File

To allow root login via SSH, you need to modify the SSH configuration file.

  1. Open the SSH configuration file in a text editor:
    sudo nano /etc/ssh/sshd_config
  2. Locate the following line:
    PermitRootLogin prohibit-password
  3. Change it to:
    PermitRootLogin yes
  4. Save the file by pressing CTRL + X, then Y, and Enter.

Step 3: Restart the SSH Service

Apply the changes by restarting the SSH service:

sudo systemctl restart ssh

Step 4: Allow SSH Root Access in Firewall (If Applicable)

If you have UFW (Uncomplicated Firewall) enabled, allow SSH traffic:

sudo ufw allow ssh
sudo ufw reload

Step 5: Test Root Login via SSH

Now, try logging in as root from another system using:

ssh root@your-server-ip

Enter the root password when prompted.

Step 6: Secure Root Login (Optional but Recommended)

For security reasons, consider restricting root login by allowing only specific IPs. Edit the SSH configuration file again:

sudo nano /etc/ssh/sshd_config

Add the following line:

AllowUsers root@your-trusted-ip

Save the file and restart SSH:

sudo systemctl restart ssh

Conclusion

Enabling root login via SSH in Ubuntu should be done cautiously due to security risks. It is recommended to use SSH key authentication and restrict access to trusted IP addresses. Following these steps, you can safely enable and manage root SSH access as needed.