How to Add User to Sudoers or Sudo Group

Sudo permissions enable users to execute commands with elevated privileges. These permissions are defined in the sudoers file to prevent accidental misuse of commands or enable users to execute administrative commands.

This tutorial will show you how to add a new user on Ubuntu and grant them sudo access.

Prerequisites

  • A system running Ubuntu (this tutorial uses Ubuntu 22.04.).
  • Access to an account with root or sudo privileges.
  • Access to the terminal.

Steps to Add Sudo User on Ubuntu

Standard user accounts have limitations that prevent them from performing sensitive tasks.

For example, to view the /root directory contents with the ls command, users need elevated permissions. Running the command without those permissions results in an error:

ls /root
ls terminal output

In cases like this, the sudo (Super User Do) command is the recommended method for managing the elevated permissions. Follow the steps below to allow an account to use the sudo command to perform administrative tasks.

Step 1: Create a New User

To create a new user:

1. Use the adduser command:

sudo adduser [name-of-user]

For instance, create a user called user1 with:

sudo adduser user1
sudo adduser user1 terminal output

The adduser command creates a new user with a group and Home directory for that user.

2. The system prompts for a password. Enter a secure password, then retype it to confirm.

add password

3. The system asks for additional information about the user, including name, phone numbers, etc. These fields are optional and can be skipped by pressing Enter.

enter additional info

4. Type y to confirm the information and complete the settings for the new user.

Step 2: Add User to Sudoers File or Sudo Group

The sudoers file and the sudo group are related to granting users administrative privileges via the sudo command.

/etc/sudoers is a configuration file that specifies which users or groups can use sudo for administrative tasks. It defines permissions, such as command execution and password requirements.

On the other hand, the sudo group is created specifically to manage sudo privileges. Users added to this group gain sudo access to run commands with elevated permissions using sudo.

To grant users sudo permission without editing the sudoers file, add them to the sudo group with the usermod command:

sudo usermod -aG sudo [name-of-user]

For example, grant user1 sudo privileges with:

sudo usermod -aG sudo user1

The command has no output, but the -aG option tells the system to append the user to the specified group.

Step 3: Verify User Belongs to Sudo Group

To confirm user1 belongs to the sudoers group, view the groups the user belongs to:

groups user1
groups user1 terminal output

The system responds by listing the username and all groups it belongs to.

Step 4: Verify Sudo Access

To verify sudo access, take the following steps:

1. Switch users with the su command. For example:

su - user1
su user1 terminal output

Enter the password when prompted.

2. Run a command that requires elevated privileges. For instance, list the /root directory contents using the ls command with sudo:

sudo ls /root
sudo ls /root terminal output

When the command provides the output, the user has sudo access.

Conclusion

After reading this article, you know how to add and create a user with sudo privileges on Ubuntu.

 

 

  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to remove a Linux user

How do I drop or remove old users account from my Linux server? I can login using the user’s SSH...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Powered by WHMCompleteSolution