To add a user, set a password for that user, and grant them sudo privileges on a Linux system, follow the steps below. For this tutorial, let’s assume you’re adding a user named “newuser”.
- Add a User: First, you’ll need to open a terminal and gain superuser privileges. Typically, you would do this by entering
sudo su
or just prefixing the following commands withsudo
. Add the user with the following command:
adduser newuser
This command will prompt you to set a password and other optional details for the user.
- Set/Change Password: If you need to reset or change the password for the user at a later time, you can use the following command:
passwd newuser
You’ll be prompted to enter the new password twice for confirmation.
- Add User to sudoers: For a user to be able to execute commands as the superuser, they need to be added to the
sudo
group (on many modern Linux distributions) or be explicitly listed in the/etc/sudoers
file. Here’s how to do both:
Add user to the sudo
group (preferred method for systems where the sudo
group exists):
usermod -aG sudo newuser
Directly edit the /etc/sudoers
file: It’s advised to use the visudo
command to edit the sudoers file as it checks for syntax errors:
visudo
Once inside the editor, you can add the following line to grant the user full sudo privileges:
newuser ALL=(ALL:ALL) ALL
After adding the line, save and exit the editor. If you’re unfamiliar with the vi
editor that visudo
defaults to, remember to press i
to enter insert mode, make your changes, then press Esc
followed by :wq
and Enter
to save and quit.