How to Add and Delete Users on Ubuntu 22.04

Published on by Dasun Tharanga

1 min read

Please ensure that you have root access to your server before proceeding.

👦 Adding a New User

To create a new user, you type:

1sudo adduser newuser

🔓 Granting Sudo Privileges

To give this new user special powers to do things like a superuser (root), you add them to the sudo group.

First, you can check which groups the new user is part of by typing:

1groups newuser

By default, a new user is in their own group, which has the same name as the user. To add them to the sudo group, you use the usermod command:

1usermod -aG sudo newuser

Now, you can run commands with the powerful privileges of the root user, but each time it asks for a password, you'll need to provide the password of the newuser.

For more convenience, you can allows executing sudo commands without prompting for the user's password.

Be careful, doing this can be risky.

Let's do it!

To create a new file named newuser in the directory /etc/sudoers.d/, you can execute:

1sudo touch /etc/sudoers.d/newuser

Next, open the newuser file located at /etc/sudoers.d/ and insert the following line:

1newuser ALL=(ALL) NOPASSWD:ALL

This line grants the user newuser permission to execute any command with root privileges without being prompted for a password.

❎ Deleting a User

To delete a user account, you can use the following command:

1sudo deluser newuser

If you also want to delete the user's home directory along with the account, execute this command:

1sudo deluser --remove-home newuser

I hope this helps! See you in the next post!

Get notified for the next blog post? Sign up for the newsletter.