How to Add User in Ubuntu [Including Sudo User & Groups ]?

Introduction

When you install Ubuntu, the first thing you would need is to add users. Because when installing Ubuntu, you’ll be having only root User Created.

Its dangerous to have and work with root user due to its highest privilege which have destructive capabilities. Hence, its a best practice to add users in Ubuntu for your day to day work.

In this article, you will learn

Prerequisites

To add user, you need to have the root user privilege. If you’ve a non root user, you should have an account with the Sudo Privileges.

Difference Between Useradd and Adduser Commands

useradd is the binary compiled within your Ubuntu installation whereas the adduser command is a perl script which uses the useradd command in the backend along with some additional methods. Those additional methods make the adduser command more user friendly and interactive to customize the new user creation.

Adding User

In this section, you’ll learn how to add user in Ubuntu using adduser command.

adduser command internally uses the perl script which is an interactive script which will ask for the password to be set. It also creates the home directory for the newly created user.

When you’re logged in to the system using the root user, type the following command to add user.

adduser username

When you’re logged in to the system using the non root user with the sudo privileges, type the following command to add user from ubuntu terminal.

sudo adduser username
  • sudo – Keyword to execute the command with admin privileges
  • adduser – command to add a new user
  • username – Your desired new user name

Press Enter.

You’ll be asked to enter the password.

Enter the password.

You’ll be asked with other questions which are optional. Press enter if you would like to skip it.

Finally, the information you’ve entered will be displayed for your confirmation. Press ‘Y’ if you would like to confirm.

The command will create user with home directory and copy the files from the /etc/skel directory to the users home directory. Within the home directory, the user can write, edit, and delete files and directories.

Adding User in Ubuntu With Home Directory

In this section, you’ll learn how to add user in Ubuntu with the home directory using useradd command. This command will use the binary available in the Ubuntu system.

To create a home directory while adding a user, you need to use -m along with the command useradd. Otherwise, home directory will not be created.

Use the below command to add user with home directory.

sudo useradd -m username
  • useradd – Command to create user
  • -m – Option to specify that home directory for the user must be created
  • username – Your desired new user name

If you want to create a home directory in a custom location rather in the default location, you specify the directory path along with the option -d. Use the below command to create user with custom home directory location.

useradd -m -d /Your/Custom/FolderPath username
  • adduser – Command to create user
  • -m – Option to specify that home directory for the user must be created
  • -d – Option to specify the custom location for the home directory
  • /Your/Custom/FolderPath – Desired path of your home directory
  • username – Your desired new user name

The command will create user and copy the files from the /etc/skel directory to the specified users home directory. Within the home directory, the user can write, edit, and delete files and directories.

Now, you’ve created user with the home directory in Ubuntu. Next you’ll learn how to add a created user to a group.

Adding User to Group

First, you’ll create a new group. Use the below command to create a new group.

sudo addgroup groupname
  • sudo – Keyword to execute the command with admin privileges
  • addgroup – command to add a new group
  • groupname – Your desired new group name

Now you can add user to group by using the below command. This command will create new user in ubuntu and add it to the group specified.

sudo adduser username groupname
  • sudo – Keyword to execute the command with admin privileges
  • adduser – command to add a new user
  • username – Your desired new user name
  • groupname – Group name to which the new user should be added

Adding Existing User to Existing Group

If you have user already created a user without adding to any group, you can add the existing user to group by using the usermod command. usermod command is used to modify the user.

sudo usermod -aG groupName username
  • sudo – Keyword to execute the command with admin privileges
  • usermod – Command to modify the user settings
  • -aAppend flag(Mandatory). To append the new group information. If this is not used, the user will be removed from the groups not specified in the above command.
  • -G – Flag to denote the group names that the user needs to be added to.
  • groupname – Group name to which the user needs to be added. Multiple groups can be specified and separated with comma (,).
  • username – Username to add to the group specified.

Adding User to Sudo Group

If you want to add a user to Sudo Group, you can use the below command.

sudo usermod -aG sudo username

If you want to add the current logged in user to a specific group, use the below command.

sudo usermod -aG sudo ${USER}

Here ${USER} variable will be replaced with the currently logged in user.

Adding Users to Sudoers File

Users and groups sudo privileges are defined in the file called sudoers. This file is located in /etc/sudoers.

You can add the users to this file to make the users as sudo user.

It is always recommended to use the visudo editor to edit this file. Because this command checks for any syntax errors when you modify and save the file.

Now use the below command to oped and edit the sudoers file.

sudo visudo /etc/sudoers

File will be opened. Scroll down to the end of the page and add the below line. Replace the username variable with your desired username to be added to sudoers.

username  ALL=(ALL) NOPASSWD:ALL

Now the file will look like below.

#
# This File MUST Be Edited With the 'visudo' Command as Root.
#
# Please Consider Adding Local Content in /etc/sudoers.d/ Instead Of
# Directly Modifying This File.
#
# See the Man Page for Details on How to Write a Sudoers File.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host Alias Specification

# User Alias Specification

# Cmnd Alias Specification

# User Privilege Specification
root    ALL=(ALL:ALL) ALL

# Members of the Admin Group May Gain Root Privileges
%admin ALL=(ALL) ALL

# Allow Members of Group Sudo to Execute Any Command
%sudo   ALL=(ALL:ALL) ALL

# See Sudoers(5) for More Information on "#include" Directives:

#Includedir /etc/sudoers.d

#Username Will Be Added to the Sudoers File
username  ALL=(ALL) NOPASSWD:ALL

Press Ctrl + O to write out the file. This will check the syntax errors and save the files successfully if there in no syntax errors.

Now press Ctrl + X to exit the editor. You have succesfully added the user to the sudoers file.

Check if the User Is Added to the Sudo Privilege

After adding the user to Sudo group or the Sudoers file, you may need to confirm if the user is succesfully given the sudo privileges. You can check it by using the below command.

sudo -l -U username

This will display the details of the sudo privileges available to the user as shown below.

Matching Defaults entries for ubuntu on ip-158-31-87-230:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User ubuntu may run the following commands on ip-158-31-87-230:
    (ALL : ALL) ALL
    (ALL) NOPASSWD: ALL

For e.g. the NOPASSWD keyword shows, the user will be able to execute the commands or switch to the root user privilege without specifying the password.

If you want to check if the current user is a sudo user, then you can use the below command.

sudo whoami

You’ll see the below output if the current user is a sudo user.

Output

root

Conclusion

To summarize, you have learnt how to

  • Add user in Ubuntu
  • Add user with home directory
  • Create a user and add group in a single command
  • Adding existing user to the existing group
  • Add user to Sudo group
  • Add user to sudoers file
  • Check if the user is granted with the sudo privilege

Your user is created and ready for use. You shall login with the password you’ve selected during user creation.

What Next?

How to execute shell commands from Python?

Leave a Comment

Share via
Copy link
Powered by Social Snap