How to Enable Root Login via SSH in Ubuntu ⋆ ALexHost SRL
Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
07.10.2024

How to Enable Root Login via SSH in Ubuntu

By default, Ubuntu disables root login over SSH for security reasons. This restriction helps to protect servers from unauthorized access, as the root account has full control over the system. However, in certain situations, such as troubleshooting or performing administrative tasks, you may need to enable root login via SSH. In this guide, we’ll walk through the process of enabling root login over SSH in Ubuntu while emphasizing some important security considerations.

Telegram Premium Account Giveaway

Join Now
Telegram Premium Star Star

Important Security Considerations

Before enabling root login via SSH, keep in mind that doing so can expose your system to significant security risks. Here are a few precautions you should take:

1️⃣ Use a Strong Password ?
Ensure that your root password is complex, incorporating uppercase and lowercase letters, numbers, and special characters. Avoid using easily guessable words or patterns. Consider using a password manager to generate and store a secure password.

2️⃣ Enable a Firewall ?
Configure a firewall such as UFW (Uncomplicated Firewall) or iptables to restrict access to critical ports. Limit SSH access only to trusted IP addresses and block unnecessary services to reduce potential attack vectors. Regularly update firewall rules based on security needs.

3️⃣ Use SSH Keys for Authentication ?️
Instead of relying on passwords, use SSH key authentication, which provides an extra layer of security. Generate a key pair using ssh-keygen, store the private key securely, and only add the public key to your server. Disable password authentication altogether to prevent brute-force attacks.

4️⃣ Change the Default SSH Port ?
By default, SSH runs on port 22, making it a common target for automated attacks. Change the port to a non-standard one (e.g., 2222 or 5822) in the SSH configuration file (/etc/ssh/sshd_config). Ensure the new port is open in your firewall settings.

5️⃣ Disable Root Login After Setup ?
Once initial configuration and administrative tasks are complete, disable direct root login to minimize security risks. Instead, create a separate user with sudo privileges. Modify /etc/ssh/sshd_config and set PermitRootLogin no to prevent unauthorized access attempts.

6️⃣ Enable Fail2Ban for Intrusion Prevention ?
Install Fail2Ban to monitor SSH logs and automatically block IP addresses after multiple failed login attempts. This helps protect against brute-force attacks and unauthorized access. Customize Fail2Ban rules for better security.

7️⃣ Keep Your System Updated ⚙️
Regularly update the operating system, installed software, and security patches using apt update && apt upgrade (for Debian/Ubuntu) or yum update (for CentOS/RHEL). Outdated software can contain vulnerabilities that attackers exploit.

8️⃣ Implement Two-Factor Authentication (2FA) ?
For an added layer of security, enable 2FA for SSH access using tools like Google Authenticator or Duo Security. This ensures that even if credentials are compromised, unauthorized access is still blocked without the second authentication factor.

By implementing these measures, you significantly enhance your VPS security, protecting it from brute-force attacks, unauthorized logins, and potential vulnerabilities. ?

Prerequisites

  • A user account with sudo privileges.
  • SSH access to your Ubuntu server.

Step 1: Enable Root User Password

If you haven’t already set a password for the root user, you need to do so. By default, Ubuntu disables root login by not setting a password for the root account. To set or change the root password, run the following command:

sudo passwd root

You will be prompted to enter a new password for the root user. Make sure to choose a strong password. After you confirm the new password, the root account will be enabled.

Step 2: Edit the SSH Configuration File

To enable root login via SSH, you need to modify the SSH daemon configuration file, sshd_config.

  1. Open the SSH configuration file using your preferred text editor. For example:
    sudo nano /etc/ssh/sshd_config
  2. Look for the following line in the configuration file:
    PermitRootLogin prohibit-password

    This line means that root login is disabled for password authentication but may be allowed with SSH keys.

  3. Change this line to:
    PermitRootLogin yes

    This change will allow the root user to log in using a password.

  4. Save the changes and exit the editor. If you are using nano, you can save by pressing CTRL + O, then press Enter and exit with CTRL + X.

Step 3: Restart the SSH Service

For the changes to take effect, you need to restart the SSH service:

sudo systemctl restart ssh

Alternatively, you can use:

sudo service ssh restart

This command will apply the new settings and enable root login over SSH.

Step 4: Test Root SSH Login

Now that you have enabled root login, it’s time to test it:

  1. Open an SSH client or terminal.
  2. Connect to your server using the root username:
    ssh root@your_server_ip
  3. Enter the root password that you set earlier.

If everything is configured correctly, you should be able to log in as the root user.

Step 5: Revert Changes After Use (Recommended)

For security reasons, it’s best to disable root login over SSH once you’ve completed the necessary tasks. To do this:

  1. Open the SSH configuration file again:
    sudo nano /etc/ssh/sshd_config
  2. Change the PermitRootLogin option back to:
    PermitRootLogin prohibit-password
  3. Restart the SSH service:
    sudo systemctl restart ssh

This will disable root login again, allowing access only through user accounts with sudo privileges.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills