fbpx

When installing Ubuntu, or any Linux distro, a root user will be created. However, there are many times when you need to create additional users with root or sudo privileges. Thankfully, Linux makes this fairly simple to do using the adduser and usermod commands for creating new users and adding them to specific group assignments.

Let’s take a look new users to Linux machines and granting them sudo permissions!


Create a User in Linux using the adduser Command

The adduser command is used to create a new user in Linux. The simplest usage of this command is to call it by passing in the username of the new user you would like to create. Calling this command must be done as root, you can do this with the help of sudo.

sudo adduser <username>

Tip: You can run the shell as root by using the sudo -s command. This will run the shell with elevated root privileges until you type the exit command. This can help if you are running multiple commands that will need root privileges so you don’t need to prefix every command with sudo.

When calling the adduser command, you will be prompted for additional information for the user account you are creating. The required thing to enter will be the Password for the user, but it will also prompt for additional user metadata that is optional.

Add New Root User to Ubuntu Linux Server using Bash 1
Use adduser command to create a new user on Linux

Give Linux User Root / Sudo Permissions

Now that we have a new user created on our Linux machine, this user will also need to be assigned root permissions so they are made an administrator on this machine. This can be done by on Ubuntu by adding the user to the sudo group. With default configuration of Ubuntu, being a member of the sudo group will grant the user sudo privileges.

The usermod command can be used to assign the desired user to the sudo group within Ubuntu Linux. Keep in mind this command must be run with sudo privileges, so you’ll need to use sudo to run it.

sudo usermod -aG sudo <username>

Notice the -aG flag is used with the usermodcommand. This flag will append this user to the specified group. Then the command must be given the <username> of the User you will be modifying. In this case we are specifying the sudo group to append the specified user to.

Add New Root User to Ubuntu Linux Server using Bash 2
Add Linux user to the sudo group

Check if Logged in User has sudo Privileges

Once a user have been added to the sudo group on Ubuntu they will have sudo privileges. If you are logged into an Ubuntu machine and don’t know if you have sudo privileges, then you can use the sudo command with the -l flag to check.

sudo -l

This command will list out all sudo privileges you have. If you do not have any, then it will return a message that reads “Sorry, user <username> may not run sudo on <machine-name>.”

Add New Root User to Ubuntu Linux Server using Bash 3
Check if currently logged in user has sudo privileges on Linux

Lookup All Users in sudo Group

In Linux, the /etc/group file is used to determine the groups that users belong to. The contents of this file will list out all group assignments on a particular Linux machine. You can look within this file to check if a given user is a member of a group; like checking if the user is in the sudo group.

An easy way to check if a user is in the sudo group, is to lookup what users are members of the sudo group as listed within the /etc/group file using the cat command to output to the console, and the grep command to search within the file before outputing to the console. You can do this with the following command:

sudo cat /etc/group | grep sudo
Add New Root User to Ubuntu Linux Server using Bash 4
Look up all users within the sudo group

Another related query to this, is to use grep to search for the users name instead of sudo. This would enable you to lookup all the groups the specified user is a member of using this same command.

cat /etc/group | grep chris
Add New Root User to Ubuntu Linux Server using Bash 5
Lookup all groups a user is assigned to

Wrap Up

Once this command is run successfully, the specified user will now be a part of the sudo group on your Ubuntu machine. They will now essentially be an administrator with root permissions on the machine. Use the adduser and usermod commands, you can create new admin accounts for additional users and grant them sudo privileges on your Ubuntu Linux machines. Additionally, checking the /etc/group file will enable you to lookup what groups different users are already assigned to.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect