How to Create and Add User in Sudo Group - Centos, Ubuntu & Linux
☰ In this chapter, you will learn
- What is
sudo
command? - How to create a user and add them in a sudoers group on Ubuntu/CentOS?
What is sudo command?
sudo
stands for 'Super User Do,' and it functions similarly to 'Run as administrator' on Windows. In Linux, to execute commands at the administrative level, a user needs to have administrator privileges, which can be granted using the sudo
command.
The sudo
command precedes the main command and requires the user's password for executing commands as administrators.
Note
It's important to note that this password is not the root password, but the user's own password used for authentication.
For a user to utilize sudo, they must be a member of the sudoers group and have an entry in the /etc/sudoers
file.
Some Example
sudo apt-get update
$sudo yum update
$sudo systemctl restart apache2
$sudo adduser newuser
Creating a User and adding them to Sudo group
In this tutorial, I will guide you through the process of creating a new user and adding them to the sudo group. This allows the user to harness the power of the sudo command, enabling them to execute commands with elevated privileges.
Ubuntu ↓
To create a new user, you need to run the sudo adduser <username>
command as follows:
sudo adduser smith
jack@ubuntu:~$ sudo adduser smith Adding user 'smith' ... Adding new group 'smith' (1002) ... Adding new user 'smith' (1002) with group 'smith' ... New password: Retype new password: passwd: password updated successfully Changing the user information for smith Enter the new value, or press ENTER for the default Full Name []: Smith Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y
To add a new user to sudo group in ubuntu, execute the sudo usermod -aG sudo <username>
command in terminal.
sudo usermod -aG sudo smith
Now, switch the account to smith and run the some command with sudo
privilege.
Switch to smith account
jack@ubuntu:~$su smith
Enter password: smith@ubuntu:~$ smith@ubuntu:~$sudo apt-get update
Centos ↓
To create a new user, you need to run the sudo adduser <username>
command as follows:
sudo adduser smith
sudo passwd smith
Follow the prompt and create password for user smith.
To add a new user to sudo group in ubuntu, execute the sudo usermod -aG sudo <username>
command in terminal.
usermod –aG wheel smith
visudo
editor to enable wheel group.- Type
sudo visudo
in terminal. It will open the sudo file. - Scroll down to find following line.
# %wheel ALL=(ALL) ALL
- Remove # from the beginning of the line.
- To save and exit → Press Esc button → type :wq → Press Enter.
su smith
to switch user and then test this user by running some command using sudo privilege.Option 2: Adding user to Sudo group.
If above trick won't work for you, then you can directly add the user into visudo
file to give sudo access.
visudo
in terminal.root ALL=(ALL:ALL) ALL
smith ALL=(ALL:ALL) ALL
Summary
In this tutorial, we taught you how to create a user in Ubuntu/CentOS and add them in sudo
group.