Learn step-by-step how to create a new user with sudo privileges in Linux. This tutorial covers adding the user, granting sudo access, and configuring password-less sudo for seamless command execution. Perfect for system administrators and Linux enthusiasts looking to streamline user management.
Step 1: Log in as Root or a User with Sudo Privileges
To add a new user, you need to be logged in as the root user or another user with sudo privileges. If you are not logged in as such a user, log in now.
Step 2: Add the New User
Use the adduser
command to add a new user. Replace username
with the desired username for the new user.
sudo adduser username
You will be prompted to set a password and fill in additional information for the new user.
Step 3: Add the User to the Sudo Group
In many Linux distributions, adding a user to the sudo
group will grant them sudo privileges. Use the following command to add the new user to the sudo
group:
sudo usermod -aG sudo username
Step 4: Verify the User’s Sudo Privileges
Switch to the new user to verify that they have sudo privileges.
su - username
Once switched, use the following command to check if the user can use sudo:
sudo whoami
If the user has sudo privileges, the output will be root
.
Step 5: (Optional) Configure Sudoers File for Specific Permissions
For more specific control over what the user can do with sudo, you can edit the /etc/sudoers
file using the visudo
command. This file allows you to define specific permissions and restrictions for sudo users.
Open the sudoers file for editing:
sudo visudo
Add a line at the end of the file to give the user specific sudo privileges. For example, to allow the user to run all commands with sudo, add:
username ALL=(ALL:ALL) ALL
Allow a user to run sudo commands without entering a password, you need to add a line for the user to allow them to run sudo commands without a password. Replace username
with the actual username.
username ALL=(ALL) NOPASSWD:ALL
Save and exit the file.
Verification
To verify that the changes have been applied correctly, switch to the new user and run a sudo command without a password:
su - username
sudo apt-get update
The command should execute without prompting for a password, and the output should be root
.
Summary
Here are the commands summarized:
- Add a new user:
sudo adduser username
- Add the user to the sudo group:
sudo usermod -aG sudo username
- Switch to the new user and verify sudo privileges:
su - username sudo whoami
By following these steps, you will successfully add a new user with sudo permissions in Linux.