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
sudo privileges. Thankfully, Linux makes this fairly simple to do using the
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
Create a User in Linux using 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 adduser <username>
Tip: You can run the shell as
rootby using the
sudo -scommand. This will run the shell with elevated root privileges until you type the
exitcommand. This can help if you are running multiple commands that will need root privileges so you don’t need to prefix every command with
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.
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
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>
-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.
Check if Logged in User has
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.
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>.”
Lookup All Users in
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
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
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
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
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.