How to Create New User and Group in Ubuntu 22.04 using the Command Line

Andrey Byhalenko
DevOps Manuals and Technical Notes
4 min readJan 25, 2024

In this article, I will show you how to create a new user and add it to a specific group using the command line.

Let’s say you need to add a new Ubuntu user for a new DevOps engineer in the team.

I suggest creating a separate group for all DevOps engineers (I name it devops), providing the devops group with the necessary permissions, then adding all DevOps engineers to the devops group.

Create a devops group first.
Note: Only root may add a user or group to the system, so switch to root.

sudo su -

sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy.

su stands for “switch user.” By default, without any arguments, it switches to the root user.

minus stand for switch to user with a login shell, ensuring you get the root user’s environment and settings. In other words, this argument provides an environment similar to what the user would expect had the user logged in directly.

Now you are logged in as root, so create the devops group.

addgroup devops

Now create the user.

sudo adduser andrey

You will need to type the password you chose for this user, name, and other optional information.

Verify the user.

id andrey

As you see in the output, the unique id of user andrey is 1001, the group id is 1002, and user andrey belongs to group 1002.

Now add andrey to the sudoers group, he needs it in order to do his tasks.
Execute the following command:

sudo usermod -a -G sudo andrey

Now if you run the id andrey command, you will see that user andrey belongs to two groups now, 1002(andrey), and 27(sudo).

Switch to andrey user.

su - andrey

Now try to list the running containers by executing the docker ps command.

As you see, user andrey doesn’t have permissions to execute any Docker commands. This is because the Docker daemon binds to a Unix socket, which is owned by the root user by default, and other users can only access it using sudo. The Docker daemon always runs as the root user.

So every time user andrey wants to execute any Docker command, he will need to type a password.

You can change it if you want. You can configure sudo to never ask for a specific user password.

First, you have to log in as super. If you logged in as andrey, just execute the exit command.

Then, you need to create a docker group (probably it already exists, although I prefer to run this command anyway).

sudo groupadd docker

Add the andrey user to the docker group.

sudo gpasswd -a andrey docker

Run a newgrp docker or log out/log in to activate the changes to groups.
The newgrp command is used to change the current group ID during a login session.

newgrp docker

Now login as andrey and try to execute docker ps command.

As you see, user andrey can run the Docker commands without using sudo anymore.

Remember id andrey command?

As you see on the output, user andrey belongs to three groups now (andrey, sudo, and docker).

In this article, you discovered how to create a user, how to attach it to a group, and how to make it able to run Docker commands without sudo.

I hope you learned something new.

If you liked my articles, you can join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.

As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.

Subscribe here: https://junior-devops-hub.ck.page

--

--

Andrey Byhalenko
DevOps Manuals and Technical Notes

I'm a DevOps Engineer, Photography Enthusiast, and Traveler. I write articles aimed at junior DevOps engineers and those aspiring to become DevOps engineers.