How to use Sudo on a CloudRaya Linux VM

How can we help?
< All Topics
Print

How to use Sudo on a CloudRaya Linux VM

By following these tutorial, you will learn how to get things done with sudo, a safer alternative to root access. You’ll also find clear instructions for setting up sudo on your favorite operating system.

What is Root?

Root is the superuser account on Linux systems. This account has full access to the entire system, including the ability to modify configuration, install software, and run services. • When logged in as root, the terminal prompt symbol changes from $ to # .

What is Sudo?

In a nutshell, sudo is a command that lets trusted users temporarily act as the system’s top administrator, called root to perform specific tasks.

Here’s how it works:

    • Authorized users: Admins can grant the access to specific users or groups, giving them the ability to run certain commands as if they were the root user.
    • No root password sharing: Unlike the su  command, sudo doesn’t require users to know the root password. They simply use their own password to authenticate.

    Key benefits of sudo:

    • Granular control: Admins can fine-tune which commands or scripts users can run with sudo, ensuring that only necessary tasks are performed with elevated privileges.
    • Accountability: It logs all activity in the /var/log/auth.log including who ran what command and when. This helps with auditing and troubleshooting if issues arise.
    • Security: Sudo’s design helps prevent accidental or malicious actions that could compromise the system, as it avoids unnecessary exposure of full root privileges.

    Remember: Sudo is a powerful tool, so use it with caution and grant access only to trusted users.

    Key points differentiating between root and sudo:

    • Safety first: It is generally the preferred approach for most tasks to minimize the risk of accidents.
    • Root for specific cases: Use the root only when absolutely essential and with a full understanding of the potential consequences.
    • Think before you act: Always double-check commands and their implications before executing them with elevated privileges.

    Creating a Sudo User on Ubuntu, Debian, Kali and Other Apt-Based Distributions

    1. Create the User
      Use the adduser command to create the new user. For example, to create a user named newuser, run:
    				# adduser newuser
    			

    This will prompt you for information about the new user, including:

    • Username: This will be the login name for the user.
    • Password: Choose a strong password for the new user.
    • Full Name (Optional): You can enter the full name of the user, but this is optional.
    • Other Information (Optional): You can set additional information like room number, phone number, etc., but these are all optional.
    1. Add the User to the Sudo Group
      Once you’ve created the user, you need to add them to the sudo sudo group. This will grant them the ability to run commands with root privileges by prefixing them with sudo. Run the following command:
    				# adduser newuser sudo
    			
    1. Testing the new user
      Test by switching to the new user.
    				# su - newuser
    			
    				$ whoami
     newuser
    			
    				$ sudo whoami
     [sudo] password for newuser:
     root
    			

    Double-check your identity and sudo access:

    1. Confirm your current user: Use the command whoami to ensure you’re logged in as the newly created user. It should display the username you just created.
    2. Test sudo privileges: Use the command sudo whoami. This will prompt you for the new user’s password. If entered correctly, it should return “root,” confirming that the user has successfully been granted administrative privileges.

    Creating a Sudo User on RHEL-Compatible Distributions and DNF-based Distributions

    Creating a sudo user on RHEL-compatible distributions (such as CentOS, Rocky Linux, Alma Linux, etc.) and DNF-based distributions (such as Fedora, OpenSUSE, Oracle Linux, etc.) follows a similar process. Here’s how you can do it:

    1. Create the User
    				# adduser newuser
    			

    This will automatically create a home directory for the user. Next, set a password for the new user with:

    				# passwd newuser
    			
    1. Grant Sudo’s Privileges to the Users
      Now, grant the user with sudo’s privileges by adding them to the wheel group:
    				# usermod -aG wheel newuser
    			
    1. Testing the new user
      Test by switching to the new user.
    				# su - newuser
    			
    				$ whoami
     newuser
    			
    				$ sudo whoami
     [sudo] password for newuser:
     root
    			

    Conclusion

    By following these steps, you now possess the fundamental knowledge needed to operate Linux, utilizing sudo as a safer alternative to root access.

    For more tutorials on enhancing your experience and ensuring the optimal performance of your CloudRaya infrastructure, please visit our Knowledge Base or our Youtube channel.

    Table of Contents

    Ready, Set, Cloud

    Ready, Set, Cloud