Hey guys! So, you've just installed Rocky Linux and are ready to dive in, but you're stuck on one of the most basic hurdles: figuring out the default root password. Don't sweat it, it's a common hiccup! Unlike some other distributions, Rocky Linux doesn't set a default root password during installation. Instead, it encourages you to set a root password or configure user access with sudo for security reasons. Let's walk through how to get root access sorted out so you can start tinkering with your new system. We'll cover everything from setting the root password initially to recovering it if you've forgotten it. We’ll also touch on best practices for managing root access to keep your system secure.

    Initial Setup and Root Access

    When you first install Rocky Linux, the installer prompts you to create a user account. This initial user account is typically granted sudo privileges, which allows you to perform administrative tasks. Here’s how it usually goes down:

    1. User Creation: During the installation, you're asked to create a user. Let’s say you create a user named john.
    2. sudo Privileges: This user john is usually added to the wheel group (or a similar administrative group), which grants them the ability to use sudo. This means you can run commands as root by prefixing them with sudo.

    So, technically, there isn't a default root password. Instead, you use your user account with sudo to perform administrative tasks. For example, to update your system, you would use:

    sudo dnf update
    

    This command uses your user password to authenticate and run the update as root. However, if you prefer to enable the root account and set a password, here’s how you can do it.

    Setting the Root Password

    If you want to enable the root account and set a password, you can do so via the terminal. Here’s the command:

    sudo passwd root
    

    Let's break this down:

    • sudo: This ensures you have the necessary permissions to change the root password.
    • passwd: This is the command-line utility for changing passwords.
    • root: This specifies that you want to change the password for the root user.

    When you run this command, you'll be prompted to enter a new password for the root user. Make sure it’s a strong one! Once you've entered and confirmed the new password, the root account will be enabled with the password you set. Now, you can log in as root using the username root and the password you just created.

    Logging in as Root

    After setting the root password, you can log in as root in a few different ways:

    • Via the Terminal: You can switch to the root user in the terminal using the su command. Just type su and enter the root password when prompted.

      su
      Password:
      
    • Via SSH: To log in as root via SSH, you may need to modify the SSH configuration. Open the SSH configuration file:

      sudo nano /etc/ssh/sshd_config
      

      Look for the line PermitRootLogin and change it to yes:

      PermitRootLogin yes
      

      Then, restart the SSH service:

      sudo systemctl restart sshd
      

      Warning: Enabling root login via SSH can introduce security risks. It's generally recommended to disable direct root login and use SSH keys or sudo for administrative tasks.

    Recovering a Lost Root Password

    It happens to the best of us—you forget the root password. Don’t panic! Here’s how you can recover it:

    1. Reboot the System: Restart your Rocky Linux system.

    2. Interrupt the Boot Process: During boot, interrupt the process by pressing Esc, or Shift keys. This should bring you to the GRUB menu.

    3. Edit the GRUB Configuration: In the GRUB menu, select your Rocky Linux kernel and press e to edit the boot parameters.

    4. Find the linux Line: Look for a line that starts with linux or linuxefi. Add the following parameter to the end of the line:

      rd.break enforcing=0
      

      This tells the system to drop into an emergency shell before the normal boot process.

    5. Press Ctrl+x: Press Ctrl+x to boot with the modified parameters.

    6. Remount the Root Filesystem: In the emergency shell, remount the root filesystem with read-write permissions:

    mount -o remount,rw / ```

    1. Change the Root Password: Now, you can change the root password using the passwd command:

    passwd root ```

    Enter the new password and confirm it.
    
    1. Re-enable SELinux (if applicable): If SELinux was enabled, you need to relabel the filesystem. Create the .autorelabel file:

    touch /.autorelabel ```

    1. Exit and Reboot: Type exit twice to continue the boot process. The system will automatically reboot, and SELinux will relabel the filesystem if necessary.

    After the reboot, you should be able to log in as root with the new password.

    Security Best Practices for Root Access

    While having root access can be convenient, it’s crucial to follow security best practices to protect your system. Here are some tips:

    • Disable Direct Root Login via SSH: As mentioned earlier, allowing direct root login via SSH can be a security risk. Disable it by setting PermitRootLogin no in /etc/ssh/sshd_config.
    • Use sudo: Instead of logging in as root, use sudo to perform administrative tasks. This provides an audit trail and reduces the risk of accidental damage to the system.
    • Strong Passwords: Always use strong, unique passwords for all user accounts, including the root account. A strong password should be a mix of uppercase and lowercase letters, numbers, and symbols.
    • Regular Updates: Keep your system up to date with the latest security patches. Use the dnf update command regularly.
    • Monitor Root Activity: Keep an eye on root activity by reviewing logs and audit trails. This can help you detect and respond to suspicious behavior.
    • Limit sudo Access: Only grant sudo access to users who need it. Avoid giving sudo access to every user on the system.

    Alternatives to Root Login

    Besides using sudo, there are other alternatives to logging in as root:

    • su - Command: The su - command allows you to switch to the root user with the root user's environment. This can be useful when you need to perform multiple administrative tasks.

      su -
      
    • Graphical Tools: Many graphical tools require administrative privileges. When you run these tools, they will prompt you for your user password, and then execute with root privileges.

    Conclusion

    So, there you have it! Rocky Linux doesn't come with a default root password, pushing you to use sudo for admin tasks right from the get-go. If you decide to set a root password, just use the sudo passwd root command, but always remember to keep security in mind. If you ever forget the root password, the recovery process involves interrupting the boot sequence and resetting it via the command line. By following the security best practices, you can ensure that your Rocky Linux system remains secure while still having the necessary access to administer it. Keep your system updated, use strong passwords, and be mindful of who has sudo access. With these tips, you'll be well on your way to mastering your Rocky Linux server. Happy tinkering!