Using sudo for Temporary Root Access
One common practice is to use sudo to execute individual commands with root privileges. When you prefix a command with sudo, it runs as if the command were executed by the root user. For example, to list files in the /root directory:logout.
Switching to a Full Root Shell
If your account lacks sudo privileges but you know the root password, you can log in directly as the root user. Use any of the following commands to start a full root shell:su -su -lsu --login(long form)
Handling Locked Root Accounts
In some Linux systems, the root account may be locked by default. When the root account is locked, you cannot log in using a password; however, you can still obtain root privileges via sudo.When the root account is locked, attempting to log in with
su - will fail because it requires a valid root password. Always verify that your user account retains sudo privileges before making changes.Setting or Unlocking the Root Password
If you want to enable password-based logins for the root account, you have two options:- Assign a new password if the root account never had one set.
- Unlock the account using the password unlock command if it was previously locked.
su - and enter the new password.
Locking the Root Account for Added Security
If you decide that direct root logins pose a security risk, you can disable them by locking the root account. Locking the account prevents password-based logins without affecting alternative login methods such as SSH keys.Before locking the root account, ensure that your user account has sudo privileges. Losing this access could prevent you from making essential system changes.