How to Enable root login Over ssh in Ubuntu
How to Enable Root Login via SSH on Ubuntu
By default, Ubuntu disables root login over SSH for security reasons. This restriction helps protect servers from unauthorized access, as the root account has full control over the system. However, in certain situations—such as troubleshooting or performing advanced administrative tasks—it may be necessary to enable root login via SSH. This guide walks you through the process of enabling root SSH login on Ubuntu and highlights important security considerations.
Important Security Considerations
Before enabling root login over SSH, be aware that this can significantly increase security risks. Consider the following precautions:
- Use a strong password: Ensure the root password is complex and difficult to guess.
- Enable a firewall: Useor another firewall to allow SSH access only from trusted IP addresses.
ufw - Use SSH keys: It is strongly recommended to use SSH key-based authentication instead of relying solely on passwords.
- Disable root login after use: Once administrative tasks are complete, disable root SSH login to reduce potential attack vectors.
Prerequisites
- A user account withprivileges.
sudo - SSH access to your Ubuntu server.
Step 1: Set or Enable the Root Password
If the root account does not have a password set, you must define one. Ubuntu disables direct root login by default by not setting a root password. To set or change the root password, run the following command:
sudo passwd rootYou will be prompted to enter and confirm a new root password. Choose a secure password. Once completed, the root account will be enabled.
Step 2: Edit the SSH Configuration File
To allow root login over SSH, you need to modify the SSH daemon configuration file.
- Open the SSH configuration file using your preferred text editor, for example:
sudo nano /etc/ssh/sshd_config- Locate the following line:
PermitRootLogin prohibit-passwordThis setting allows root login only via SSH keys and disables password-based authentication.
- Change the line to:
PermitRootLogin yesThis change allows the root user to log in using a password.
Save the file and exit the editor. In
nanoCTRL + OEnterCTRL + XStep 3: Restart the SSH Service
To apply the changes, restart the SSH service:
sudo systemctl restart sshAlternatively, you can use:
sudo service ssh restartThis will apply the new configuration and enable root login via SSH.
Step 4: Test Root SSH Login
After enabling root login, test the configuration:
- Open a terminal or SSH client.
- Connect to your server as root:
ssh root@your_server_ip- Enter the root password you configured earlier.
If everything is configured correctly, you should now be logged in as the root user.
Step 5: Revert Changes After Use (Recommended)
For security reasons, it is best to disable root SSH login once your tasks are complete. To do this:
- Open the SSH configuration file again:
sudo nano /etc/ssh/sshd_config- Change thesetting back to:
PermitRootLogin
PermitRootLogin prohibit-password- Restart the SSH service:
sudo systemctl restart sshThis will disable direct root SSH access again, ensuring that administrative access is performed through user accounts with
sudo