How do I create a user in Linux sudo?

Granting sudo (superuser) privileges to a user in Linux allows them to execute commands with administrative rights, which is essential for performing system-wide changes. This guide will walk you through the process of creating a new user and granting them sudo privileges on a Linux system.

Why Create a Sudo User?

Having a sudo user is important for several reasons:

  • Security: Reduces the risk of accidental system changes by limiting the number of users with root access.
  • Convenience: Allows trusted users to perform administrative tasks without logging in as the root user.
  • Best Practices: Enhances security by promoting the principle of least privilege.

Prerequisites

Before you begin, ensure you have:

  1. Root or existing sudo user access.
  2. Access to the terminal.

Step-by-Step Guide to Create a Sudo User

1. Open the Terminal

You can open the terminal by searching for “terminal” in your applications menu or using the shortcut Ctrl + Alt + T.

2. Create a New User

To create a new user, use the adduser command followed by the username you want to create. For example, to create a user named newadmin:

sudo adduser newadmin

You will be prompted to enter and confirm the new user’s password, and optionally, provide additional information such as the user’s full name.

3. Add the User to the Sudo Group

Once the new user is created, add them to the sudo group using the usermod command:

sudo usermod -aG sudo newadmin

This command modifies the user account to append (-a) the user to the sudo group (-G).

4. Verify the User’s Sudo Privileges

To verify that the new user has sudo privileges, switch to the new user account:

su - newadmin

Then, run a command with sudo to check if it prompts for a password and executes correctly. For example:

sudo whoami

If the setup is correct, the output should be root.

Additional Tips

  • Granting Specific Sudo Permissions: You can provide specific sudo permissions by editing the sudoers file with the visudo command. For example, to allow newadmin to run a specific command without a password:
  sudo visudo

Add the following line:

  newadmin ALL=(ALL) NOPASSWD: /path/to/command
  • Removing Sudo Privileges: To remove sudo privileges from a user, use the following command:
  sudo deluser newadmin sudo

Conclusion

Creating a sudo user in Linux is a straightforward process that involves creating a new user and adding them to the sudo group. By following this guide, you can efficiently manage administrative privileges on your Linux system, ensuring both security and convenience. Properly managing sudo users helps maintain a secure and well-administered system, preventing unauthorized or accidental changes.