linux tux

Learning how to manage users in linux is crucial when you have to deal with accounts in your organization or even just users in your personal lab or system!

In this short tutorial we are going to deal with basic users manipulation, including creation and deletion, exploring some quick modification commands to deal with day-by-day users management.

1. How to create an user in linux

2. How to modify an user in linux

3. How to delete an user in linux

4. How to manage groups in linux

1. How to create an user in linux

Someone got hired in your company, so your boss needs to give the new employee an user to access systems in your organization, but your fellow sysAdmin is on vacation.

No panic! Adding users is one of the quickest and easiest activities you could be asked to accomplish.

An user is identified by an UID and that you can assign it a principal group and many additional groups, all you need to know is:

  • An username
  • One or more groups where it has to be put into
  • Whether its home directory has to be created or not

1.1 useradd command and its options

The command you are going to use is useradd and the main options that you could need are:

-c COMMENT –> Free text field, it allows to set the display name of the user, usually name and surname, as it will be shown during login.

-d  HOME_DIR –> To set the home directory location, it doesn’t need the directory to exist, but it doesn’t create it as well

-g  GROUP –> To assign the user a specific principal group. Default is the same as the username.

-G GROUP1[,GROUP2,…[,GROUPN]]] –> To assign the user one or more additional groups, for instance managers, consultants, employees etc.

-m –> Option needed for creating user’s home directory upon account creations

-u  UID –> Allows to assign a specific numeric UID to the user, otherwise an incremental counter is used to generate one.

-r –> Specifies that created user will be a system user, whose UID will be 100 to 499 but can vary depending on linux distribution

-s SHELL –> Specifies the shell that will be the default for the user, if none specified, it will just be assigned an interactive shell, usually /bin/sh

Default values, like SHELL and many others can be added by editing /etc/default/useradd file.

1.2 Setting a password for the user

If the user needs to log-in into  the systems, he needs a password. To set it we will use the utility passwd (as root) followed by the name of the user we have created:

Copy to Clipboard

1.3 Example

Let’s say that we need to create the user alex00198 with the following specifications:

  • It needs to belong to group employees as primary group
  • It needs to belong to bi_viewers group
  • Its home directory needs to be /home/alexr and must be created upon user creation
  • He needs to have BASH as its default shell.
  • Its display name is varslashblog.com

The command that will allow us to reach our goal is:

You can check that everything was ok by using id command:

Additionally, you can inspect the file /etc/passwd and see that its display name and shell are correct:

2. How to modify an user in linux

Once you created an user in linux, you could need to make modifications to it, like changing its home directory, setup a new display name, adding or removing groups and so on.

The command we are going to use is usermod whose options are mostly the same as useradd:

-a –> Append, used to add supplementary groups without removing existing ones, can only be used with -G option.

-d  HOME_DIR –> To set the home directory new location, can be used with -m option to move it to the new location

-g  GROUP –> To assign the user a new specific principal group. Default is the same as the username.

-G GROUP1[,GROUP2,…[,GROUPN]]] –> To assign the user one or more additional groups, for instance managers, consultants, employees etc.

-m –> Take the meaning of “move home directory” to the location specified with -d option.

-u  UID –> Allows to assign a new specific numeric UID to the user, otherwise an incremental counter is used to generate one.

-s SHELL –> Specifies the new shell that will be the default for the user.

2.1 Example

You need to modify the user that we previously created:

  • Needs to be part of the managers group
  • We need to change its uid to 12345
  • Its display name should be “varslashblog.com rocks!”
  • Its new home directory should be /home/varslashblog

The command that will allow us to do it is:

As usual, we can check our work by using id:

Let’s check /etc/passwd too:

3. How to delete an user in linux

3.1 The hardcore way

Basicly the most quick (and dirty) way to remove an user is using the userdel

command, whose main options are:

-r –> Removes the user dir and all its content

-f –> Forces the operation, and forces the -r operation too.

3.1.1 Example

Let’s delete the user dummy!

As you can see, userdel looks for mail and home directories to remove them, let’s check if the operation went smooth

3.2 The soft (and safe) way

There is another utility, called chage that allows a sysadmin to manage details related to expiry of accounts, password change period, inactivity. We will not go further in the description, but we will just outline that we can use the following statement to permanently disable an account, that will remain in an inactive state, keeping its UID.

And if you try to switch to that user or logging in this is what you will receive:

Copy to Clipboard

4. How to manage groups in linux

Groups are a great way to segregate access to files and folders, rather than giving access to particular processes only to predefined entries.

Similarly to users, groups can be added, modified and deleted, and that’s what we are going to see now!

4.1 Creating groups

When creating groups, we are going to use (guess what!) groupadd whose useful options are:

-f –> Allows to exit with a success return code when trying to create a group that already exists, very useful in scripts that check RC

-g GID –> Allows to assign a particular groupID to the group. If used with -f and the specified GID already exists, it will choose another GID.

-r –> Specifies if the group is a system group

In order to check if the groups has been correctly added, you can check the /etc/group file

4.2 Modifying groups

The utility that we are going to use is groupmod, and its options are similar to groupadd:

-g GID –> Allows to assign a particular groupID to the group.

-n NEW_GROUP –> Allows to specify a new name for the group

4.3 Deleting groups

In this case, we are going to use groupdel to delete a group. Be careful that if a group is a primary group for any user, it is necessary to firstly remove the group from the user, then it will be possible to remove the group.

4.4 Examples

In the following example we are going to create a ‘dummy’ group, with GID 1234, checking that the group has been correctly created:

Once the groups is created we are going to rename it as ‘dummy_master’ with GID 1999, and check that everything is ok:

Since no users will ever want to use that group, we are going to remove it and assure that it is not present anymore:

Conclusions

Now you know how to add, modify and delete groups and users! If you liked the article, please share with your friends and colleagues!