Skip to main content

Command Palette

Search for a command to run...

How to Create a User with Sudo Permissions

Published
3 min read
How to Create a User with Sudo Permissions

Sudo is short for Super User Do. It's a Linux command that allows you to run commands as an administrator or root. Any user who is a part of the sudo group can execute commands as though they were the root user.

In this guide, our goals include the following:

  1. Create a new user

  2. Add the user to sudo group

  3. Add the user to docker group

The third goal is optional and only intended for those who want to create a non-root user for Docker. That way, you can run your docker commands without sudo.

Let's get started.

Create a New User

To create a new user, we need to switch to root. We'll do that using this command

$ sudo su

We've switched to root. The next step is to create a new user. We can do that by either using adduser or useradd. Let's use the first option.

adduser precious

Replace 'precious' with your username.

After creating a new user, you'll be prompted to add a password. Remember to use a password you won't easily forget.

Enter new password:
Retype new password:
passwd: password updated successfully

Note that when entering your password, it will not show on the terminal.

Additionally, you'll be prompted to add other pieces of information, such as room number and work phone. You can press Enter to leave them blank.

Changing the user information for precious
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]: y

Add New User to the sudo Group

We'll use the usermod command to add our newly-created user to the sudo group.

usermod -aG sudo precious

Let's switch to the new user account:

su - precious

Add New User to the docker Group

This section is for anyone who wants to execute Docker commands without typing sudo.

Add the newly created user to the docker group using the following command:

sudo usermod -aG docker ${USER}

${USER} shows that we are adding the currently logged-in user.

If you are not logged in, you'll have to explicitly mention the user by replacing ${USER} with your username.

We've successfully added our user to the docker group.

Make Changes Effective

To make the group membership effective, you need to log out from the current server/terminal and log back in. You may also run the following command:

su - ${USER}

Enter the user's password when prompted.

Confirm that the current user has been added to the docker group by checking the group membership using groups command.

groups

You can now run docker commands as a non-root user!

Conclusion

That's all about creating a new user with sudo privileges. Creating a non-root user is straightforward and you can create as many users as necessary by repeating the steps above. It's best practice to run maintenance tasks and server administration as a non-root user.

If you have suggestions or other topics you’d like me to write on, feel free to reach out. I’m on LinkedIn and Twitter. I would love to hear from you.