Linux User Management Explained with Examples

This tutorial explains basic user and group management in Linux step by step with practical examples. Learn essential user and group management operations (add, create, manage, edit, remove and delete) including associated configuration files and necessary commands with parameter and options in detail.

Linux uses following files for user and group management.

/etc/shadowStore all the Linux password in MD5 encryptions format

/etc/passwdStore all user related information's

/etc/groupStore all group related information's

Back-up Files

In this tutorial we will modify these files. So it's better to take back-up before doing this assignment because your little mistake can crash Linux system.

#mkdir /backup
#cp /etc/passwd /backup
#cp /etc/group /backup
#cp /etc/shadow /backup 

linux User managements

Create user

useradd is used to create user. Several options are used with useradd command but you will learn about them in our next assignments. In this assignment you are going to learn what exactly happen in these files when a new user is added. First watch carefully last lines of these files.

#cat /etc/passwd |more
#cat /etc/shadow |more
#cat /etc/group |more 

Add a user.

#useradd vinita
#passwd vinita 

linux User managements

Now read these files again with cat command or you can use |grep switch to filter the output

#cat /etc/passwd |grep vinita
#cat /etc/shadow |grep vinita
#cat /etc/group |grep vinita
# cd /home
#ls –ld vinita 

linux User managements

User's entry in passwd

All these files are changed when a user is created in passwd file entries are done in following format separated by :

vinita users login name x password required to login 503 unique user id 504 unique group id /home/vinita users home directory /bin/bash user shell 

In shadow file entry is straight forward. Whatever showing beside the user name is the password of user vinita in MD5 encrypt format.

User's entry in group

Whenever you create a normal user, users primary group form same name is automatically created. As you can verify by looking in /etc/group. 504 is the unique group id.

User's home directory

Same as group, users home directory is also created in /home partition and user get the ownership of this directory.

How to create a user without password.

linux User managements

To create a user without password use –d switch .

#useradd nikki
#passwd -d nikki 

How to create a group.

To create group use groupadd commands. Group created by this command is called secondary group.

#groupadd test
#cat /etc/group |grep test 

How to add user in groups

To add user in this group use usermod commands

#usermod –G test vinita 

This command will make vinita user to member of test group.

How to delete secondary group

You can delete a group by groupdel commands

#groupdel test
#cat /etc/group |grep test 

You cannot delete users primary group until user exist for example

 #groupdel nikki 

How to delete User

userdel command is used to delete user. When a user is deleted user's primary group will automatically be deleted.

#userdel nikki
#groupdel nikki groupdel: group nikki does not exist. 

linux User managements

Whenever you delete user with userdel command. entry of user will be removed from these files. But users home folder and mail folder will not be deleted. As you can see in image. If you want completely remove user including his home folder and mail folder use –r switch with userdel commands.

To add a new user, use the useradd command. The basic syntax is

 # useradd [username] 

The username is the only information required to add a new user; however, for exam prospective you should know some additional command-line arguments for useradd. The useradd command creates the account, but the account is locked.

To unlock the account and create a password for the user, use the command passwd [username]. By default, the user's home directory is created and the files from /etc/skel/ are copied into it.

The two exceptions are if the –M option is used or if the home directory already exists.

We have already discussed about these two basic commands in our last article. If you haven't completed our last assignments we suggest you to review it before going with this article as it's the sequential of last assignments.

Create a user with additional command-line arguments.In this example you are going to assign home directory on other locations so first create it and same as create first desired user's secondary group.

#mkdir /test
#groupadd example
#useradd –u 700 –d /test/user1 –g example –c “testing user” –s /bin/sh –m user1
#passwd user1 

useradd command

-c [fullname] Full name of the user (or a comment about the user). If more than one word is needed, place quotation marks around the value.
-d [directory] Home directory for the user. The default value is /home/[username]/.
-g [group] Default group for the user specified as a group name or group ID number. The group name or GID must already exist. The default is to create a private user group. If a private user group is not created, the default is the users group.
-m Create a home directory for the user if it doesn't exist. Files from /etc/skel/ are copied into the home directory.
-s [shell] Specify the user login shell for the user. The default shell if not specified is /bin/bash.
-u [uid] Integer to use for the user ID. Must be unique unless -o is used. Values less than 500 are reserved for system users. -M Do not create a home directory for the user. By default, a home directory is created unless this option is used or unless the directory already exists.

Now login form this user. And check where did this user logged in and why its shell prompt is looking different.

 $pwd /test/user1 

useradd command

By default user gets bash sell prompts. But we modified this by –s switch and given user to /bin/sh shell. Now change user shell again

 #usermod –s /bin/bash user1 

useradd command

Verify by login again from user1

useradd command

How to manage bulk users

Consider a situation where you need to create more then thousand user. It will be really tedious task if you will do it by simple useradd command. Here you have to switch to Linux shell script.

loop for creating user

# for USER in _ _ _ _ _ _ _ _ _ _ _
> do
>useradd $USER
>echo _ _ _ _ |passwd --stdin $USER
>done 

Example
(replace users vinita nikkita niddhi sumit shweta vickey kaushal manoj jai to your users)

# for USER in vinita nikkita niddhi sumit shweta vickey kaushal manoj jai
> do
>useradd $USER
>echo friends |passwd --stdin $USER
>done 

This simple for loop will create 9 users and set their defaults passwords to friends.

useradd for loop

Loop for creating groups

Now create 3 groups named sales market productions using for loop

#for GROUP in sales market productions
> do
>groupadd $GROUP
>done

Verify by cat and grep commands

groupadd command linux

For loop for deleting bulk users

Now remove all the user which we created in pervious example.

#for USER in vinita nikkita niddhi sumit shweta vickey kaushal manoj jai
>do
>userdel -r $USER
>done 

userdel linux command

For loop for deleting bulk users

Remove groups which we create in pervious example

#for GROUP in sales market productions
> do
>groupdel $GROUP
>done 

groupdel linux command

By the end of this assignments you have learnt that

  • Which files are responsible for user and group managements
  • How can you create a normal user
  • How to create user without password
  • How to create bulk users and groups
  • How to delete bulk user and groups

In our next article you will learn about the files those are responsible for user profiles and how can you grant root privilege to a normal user.

ComputerNetworkingNotes RHCE 7 Study Guide Linux User Management Explained with Examples