We are going to see User and Group Management in Linux/Unix in this post.
/etc/passwd -> This file contains all the users details.
[root@server ~]# more /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
/etc/passwd file has all the user details with some parameters.
Here i’m taking root user details to explain with details.
root:x:0:0:root:/root:/bin/bash
root – User name
x – Password
0 – User ID
0 – Group ID
Root – Comments/ User description
/root – Home directory
/bin/bash – shell
Home directory and Group will be created with the same name of user, once we created a user.
Command to create user:
Useradd <username>
adduser <username>
Example:
[root@server ~]# useradd anand [root@server ~]# adduser anand
Command to delete user account:
deluser <username>
[root@server ~]# userdel anand
The above command will delete a user but will not delete home directory of the user.
Command to delete a user account with home directory:
Use option -r to delete user with home directory.
[root@server ~]# userdel –r deepak
Use id command to know whether a user available in system and if the user exist, it will display User ID, Group ID and Primary group and Secondary group details.
[root@server ~]# id abu uid=1000(abu) gid=1011(Technology) groups=1011(Technology),1012(DBcheck)
Command to add secondary group to a user:
[root@server ~]# usermod –G Technology abu
Command to change primary group:
[root@server ~]# usermod –g Technology abu
Customizing home directory:
by default home directory will be created under /home for all the users. We can set some other directory as home directory as well.
we have two options to change the home directory.
- we can edit /etc/passwd file using vi editor to change the home directory
- Using usermod command will change home directory
Changing home directory by editing /etc/passwd file
[root@server ~]# vi /etc/passwd testuser:x:1004:1004::/testuser:/bin/bash
Command to change home directory:
[root@server ~]# usermod -m -d /testuser testuser
-m: option to move the home directory
-d: option to mention the home directory
Assigning expiry date to user:
Command to check expiry details for a user
[root@server ~]# chage -l testuser Last password change : May 27, 2017 Password expires : never Password inactive : never Account expires : never Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
Command to set expiry date for a user:
[root@server ~]# usermod --expiredate=2017-07-20 testuser
Again will check whether the expire date has been set or not.
[root@server ~]# chage -l testuser Last password change : May 27, 2017 Password expires : never Password inactive : never Account expires : Jul 20, 2017 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
We successfully set expire date for testuser.
Group Management:
Group is used to manage more than one users without touching the each and every user alone.
Group is created based on the Team on an organization.
All the group details available in /etc/group file, once we created a group.
[root@server ~]# more /etc/group
Using Groupadd command will create group
[root@server ~]# groupadd Technology