Create a Sudo User in Ubuntu/Debian

What Is sudo?

sudo is a command enables the user to run commands with the security privileges of another user, by default the root user.

When you create a new Linux instance from a hosting provider, most of the time, they will create only one account which is the root user account. The best practice is to create a sudo user account first.

This guide will show you how to create a new user with sudo access to the system.

Login As root Your Linux System

If you create a Linux instance from a hosting provider, 0pen up your SSH client and log in as root. Replace the IP address with your IP address.

ssh [email protected]

Create A New User

Create a new user account with the command adduser. Replace myusername with the user name that you want to create.

adduser myusername

Enter a new password for this user. Make sure the password is a strong secure password.

Adding user `myusername' ...
Adding new group `myusername' (1000) ...
Adding new user `myusername' (1000) with group `myusername' ...
Creating home directory `/home/myusername' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully

After the password setup, you will be prompt to enter the particular details of the new user. You can leave all the information blank and just press ENTER to accept the empty details.

Changing the user information for myusername
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]

Giving The User sudo Access

Only the members of the group sudo are allowed to use the sudo command. To add the user to the sudo group, use the usermod command. Replace myusername with the username you just created.

usermod -aG sudo myusername

Testing the sudo Access

Logout of the root user and login to your Linux system with the newly created user.

# logout
Connection to 192.168.1.100 closed.

Now, ssh with the new user.

ssh [email protected]

Do a simple check with the whoami command. This command will execute as root.

sudo whoami

The output:

root

Changing User Password

To change the current logged in user password, use the command passwd.

passwd

To change another user account password, you need to use sudo at the front and include the user name after the command. Replace username with the account user name.

sudo passwd username

Delete User Account

To delete a user account, use the command userdel. Replace  username with the account name you want to delete.

userdel username

Conclusion

You have learned how to create a new user with sudo access, change a user account password and delete a user account.

Now, you can start using the new user account with sudo access. Stay tune for the upcoming post on how you can securely lockdown your Linux system.