facebook youtube pinterest twitter reddit whatsapp instagram

What is Linux adduser and addgroup commands

The adduser and addgroup commands are Linux utilities that are used to create new user and group accounts on a Linux system. The adduser command is used to create a new user account, while the addgroup command is used to create a new group.

The adduser command is typically used by system administrators to create new user accounts for individuals who need access to the system. When you create a user account with the adduser command, you can specify a username and password for the user, as well as other information such as the user's real name and home directory.

The addgroup command is used to create new groups on a Linux system. Groups are used to manage access to resources on the system, such as files and directories. When you create a group with the addgroup command, you can specify a group name and a group ID number, as well as other information such as the group's default user and group settings.

Both the adduser and addgroup commands have several options that you can use to customize the user and group accounts that are created.

For example, you can use the -d option with the adduser command to specify a custom home directory for the user, or you can use the -g option with the addgroup command to specify a different group ID number for the group.

Note: You can learn more about adduser and addgroup, the below is a summary:

Adding & Deleting Users

Adding a user is relatively easy, but I'll want you to pay close attention to this section as there are some useful nuggets for you.

There are two ways you can add a user account, which is  useradd and adduser

While this command performs the same task, they achieve it in a different way, so, let's start with useradd:

Say we plan on adding faruq:

sudo useradd -d /home/faruq -m faruq

You see what I did, I started with sudo, this is because the command I am planning to run requires root privilege, so using sudo would grant me access, followed by useradd: which means add the user, but wait!

What is the use of the -d? This means I want the home directory /home/faruq for my new user, whenever you see -(letter), they are called flags or arguments, which is useful for giving more information about what you'd like to do (it's like customizing), in my case, I am telling the command I want the home directory to be /home/faruq

What about the -m? I want you to note this, whenever you are creating a user account with the useradd the home directory would not be created: /home/faruq, that home won't be created, so specifying with the -m flag tells the system that you would like to create a home directory during the process.

faruq - this is the name of the user I want to create. Look at the code again:

sudo useradd -d /home/faruq -m faruq

Let's list the home directory to confirm if a folder has been created for our new user:

ls -l /home

You should see the folder you created listed, see the below image for example:

1. list the folder for the new user you just created. in ubuntuYou can see we've created a new home directory and it shows the owner as seen in the image above

ls: lets you list all the files and folders in a given directory.

-l: this argument lets you ls in a long print file in a long listing format as shown in the above image, it would also display the file permission, and the file owner (you can see it display faruq in the above). and others.

So, basically ls -l /home is saying to list the file inside the home directory in a long list file format, while also displaying the owner and other info of the file.

Now, let's create a password for our new user, use the below command:

sudo passwd faruq

You will be prompted to enter the password of the current user, once, you've entered it Create Your New Password and Confirm The New Password by entering it, and you should receive "password updated successfully"

This is one way of adding a user account, the second way of adding a user account is using the adduser command, this is the recommended way of adding a user because it's easy and it does the heavy lifting for you, for example:

sudo adduser ahmed

You should see the following output:

3. add user using adduser command

You would immediately see this is the best option, it added the '/home/ahmed' directory for us and added the necessary file. it added the User ID(UID) and Group ID(GID) of 1002, and it also copied files from /etc/skel into our new user's home directory.

Go ahead and input your new password, you'll be asked for your full name, and other info, I'll leave it as default by Pressing the Enter Key. The system would ask if the info is correct, just type Y and hit Enter, and your new user should be in place.

4. User info ubuntu adduser

You can see the adduser command is recommended because it is easier to add a new user account in the sense that it prompts you for various options for the user, unlike the useradd where more commands are required.

Note that some GNU/Linux distros do not support the adduser command, so, make sure you understand the concept of the useradd in the situation whereby you find yourself using a GNU/Linux distro that isn't Ubuntu.

Deleting Users

Be careful! You just don't remove an account without having a solid reason or an account removal policy. If you are only using the server for basic tasks such as hosting a small application or a small website, then most times, you will have a root and a single sudo account for performing high-privilege tasks, you won't most likely remove that, you would only remove on a rare occasion, where maybe, an account as been compromised or you want to host two different application on a server with two distinct users.

In an organization, there are removal policies, for example, whenever a user leaves the company, their data are usually stored in an archive, which would be useful for future reference, or for new employees to continue where the former employees left off. So, it basically depends on the policy of the organization. Let's see ways we can remove a user.

To delete a user account, use the following syntax:

sudo userdel username

where username would be the username you wish to delete, the below image shows I am deleting the user ahmed:

5. Deleting user using userdel

Alright, let's ls the users in our home directly:

sudo ls -l /home

You will notice you still see the user you just deleted, for example, I deleted ahmed and it still shows its home directory:

drwxr-xr-x 2   1002   1002 4096 Jan 14 22:31 ahmed {This}
drwxr-xr-x 2 faruq  faruq  4096 Jan 14 17:23 faruq
pascal@devsrealm:~$

the userdel command doesn't remove the content of the user's home directory, but we can still remove it by using an argument together with the command userdel, which I'll show you in a bit.

Before we do, let me show you how you can move  the content of the user to any location of your choice in the hope you might want to use the content for future reference, use the following syntax:

sudo chown -R user:user /home/username/
sudo mkdir /home/archived_users/
sudo mv /home/username /home/archived_users/

Let me explain the above commands, starting with the first line:

Chown -R: The chown command is used to change the ownership and group ownership of files and directories. The -R argument is called recursive, which means that the command should work with the contents of directories, and if a directory has subdirectories and files, the command works on those files too, to put it in one word, recursively. the user is the user I want to give ownership of the directories /home/username. for example sudo chown -R pascal:pascal /home/ahmed means to change the owner of the home/ahmed directory to pascal, I hope that makes sense.

sudo mkdir /home/archived_users/ - this means, make a directory of /home/archived_useraccount, this is the place I would be storing the user contents. You can store the archives anywhere,

e.g sudo mkdir -p /company/archived_users/

The -p flag simply creates the parent directory(e.g company) if it didn't already exist, you don't need the -p flag when you are storing in the home directory since that already existed.

sudo mv /home/username /home/archived_users/ - this means move(mv) the directory /home/username to /home/archive_users, an example sudo mv /home/ahmed /home/archived_users/

If you've moved the file, try using this syntax to see if the archive_users directory has been created:

sudo ls -l /home

This is my output:

6. move deleted user into archived directory

If you cd (change directory) into the archived_users you would see the content of the user has been moved, for clarity, let's cd into the archived_users:

cd /home/archived_users

Now, ls the file in that folder using ls, this is my output:

7. Cd into archived_user

Go back to your home directory using cd ~ and let's proceed.

If you don't wanna store the deleted user directory, you can remove it using

sudo userdel -r user

this would remove the account and at the same time, remove the home directory.

To remove the home directory after the user has been removed, which means you didn't use the -r before, then use the below syntax:

sudo rm -r /home/user

I want you to do a simple task for me, run this command: cd /

You probably won't see anything, that command changed the directory into the system files, you don't believe me, try listing the file using ls, you should see the below image:

8. System Directory

The reason why I am telling you to cd into the system directory is because of the last command we used, I meant this one:

sudo rm -r /home/user, consider you mistakenly use this sudo rm -r / home/user, you see that the space between the / and the home is your system directory, if you run that command, your whole system is down, which is why sudo is recommended, if you use sudo, at least, you will be prompted the password, so, luckily, you can see the mistake before you entered the password.

That said, Change the directory back to the home directory cd ~

Creating a New Group 

Creating a group could help in categorizing what a user could do, say you have some clerks, you can a group clerk for them or you have Directors, you can create a group director. This would better help in categorizing what various users could do, you get the idea.

To create a group you use the command groupadd, followed by the name of the group, for example

sudo groupadd directors

Now, cat the /etc/group and you would see the new group is added

To remove a group, you can use the groupdel command:

sudo groupdel directors

Learn more about adduser and addgroup

Related Post(s)

  • The ac Command in Unix & Linux

    The ac command is a Unix utility that is used to display statistics about users' connect time. It displays the total connect time for all users or for a specific user. The connect time is the amount o

  • What is alias and How Do I Use It in Linux?

    The alias command can be used to represent a longer command or series of commands in Linux. Aliases allow you to type a shorter and easier-to-remember name instead of the full command, which can save

  • What is unlias & How Do I Use It in Linux?

    The unalias command is a Linux utility that is used to remove an alias that has been defined on the system. To use the unalias command, you simply need to specify the name of the alias that you want

  • What is apt-get and How Do I Use It?

    apt-get is a command line tool used for managing packages on a Debian-based Linux system. It is a part of the Advanced Packaging Tool (apt) system, which is used to manage the installation, removal, a

  • What is ar Command in Linux and How Do I Use It?

    ar is a command line tool used for creating, modifying, and extracting files from archives. An archive is a single file that contains multiple files, often in a compressed format. GNU ar is part of the GNU Compiler Collection (GCC) and is commonly us

  • What is as Command in Linux and How Do I Use It?

    The as command in Linux is a tool for assembling (compiling) assembly language source code into machine code that can be executed on a computer. Assembly language is a low-level programming language