How To Create a Sudo User on Linux | Ubuntu Server
Create a New User
- Log in to your server as the
root
user.
# ssh root@server_ip_address
- Use the
adduser
command to add a new user to your system.
Be sure to replace username with the user that you want to create.
# adduser username
- Set and confirm the new user’s password at the prompt. A strong password is highly recommended!
Set password prompts:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
- Follow the prompts to set the new user’s information. It is fine to accept the defaults to leave all of this information blank.
User information prompts:Changing the user information for username
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
There are Two ways to to make a user as a Sudo User:
1. Add the user to sudo group
- Use the
usermod
command to add the user to thesudo
group.
# usermod -aG sudo username
By default, on Ubuntu, members of the sudo
group have sudo privileges.
- Test sudo access on new user account
- Use the
su
command to switch to the new user account.
# su - username
- To ensure that the user has sudo privileges, run the
whoami
command:
# sudo whoami
You will be prompted to enter the password. If the user has sudo access, the command will print “root”:
Output
root
2. Adding User to the sudoers File
The users’ and groups’ sudo privileges are defined in the /etc/sudoers
file. Adding the user to this file allows you to grant customized access to the commands and configure custom security policies.
- Run visudo command:
# visudo
- Scroll down to the end of the file and add the following line:
/etc/sudoers |
username ALL=(ALL) ALL |
Test the user using whoami command.
All the Best!