user relation with group in linux
user relation with group in linux
The tutorial is about the user relation with group in linux. Many users can be listed in single group in linux.
Groups allow to set permissions on the group level instead
of having to grant permissions for every individual user
It specifies database structure with lots of ease
There are many useful commands for user and group management
in linux which helps to maintain groups very easily
groups
This command lists all the group names from which the user belongsFor example- root@kali:~# groups
root
groupadd
This command is used to create groups,
so that user can be listed in that groupFor example- root@kali:~# groupadd sandy
- It will create group name sandy in /etc/group file
groupmod
It is used to change the particular group definition in the system database
For example,to change the name of any group- root@kali:~# groupmod -n linux sandy
- -n is to change name
Hence group sandy is changed to linux
groupdel
This command is used to permanently delete the particular groupFor example- root@kali:~# groupdel sandy
- It will delete the group named sandy from the system database
useradd
This command is used to add user to any particular group.For example- root@kali:/etc# useradd -g linux user1
- It will add user named user1 to the group linux
usermod
It is used to modify the user account within the system database.
It modifies the system account files to show the
changes that are specified on the command line. For example- usermod -a -G sandy,linux user1
- user1 will be member of groups sandy and linux>
- -a is used to add the user to the supplementary group(s).
- -G represents list of supplementary groups which the user is also a member of.
If the user is currently a member of a group which is not listed in the command,
then the user will be removed from that group.
gpasswd
It is used to give control of group membership to another user with the gpasswd command.
It is used to set administrators to group,
who can control the membership of different usersFor example- root@kali:~# gpasswd -A sandeep sandy
- It will make sandeep the administrator of group sandy
- root@kali:~# su - sandeep
- It will make sandeep the super user
- $ id user1
- uid=1005(user1) gid=1004(sandy) groups=1004(sandy)
- $ gpasswd -a google sandy
- Adding user google to group sandy
- $ id google
- uid=1007(google) gid=1004(sandy) groups=1004(sandy)
- $ tail -1 /etc/group
- sandy:x:1004:google,sandeep
- Information about group administrators is kept in the /etc/gshadow file.
newgrp
It helps to start a child shell with new primary group.
Thus it helps to change the current group with another named groupFor example- root@kali:~# mkdir sandy
- root@kali:~# cd sandy/
- root@kali:~/sandy# touch operators.txt
- root@kali:~/sandy# ls -lh
total 0
-rw-r--r-- 1 root root 0 Jun 12 16:50 operators.txt - echo $SHLVL
1
"It shows that we are in parent shell" - root@kali:/etc# newgrp sandeep
- root@kali:/etc# echo $SHLVL
2
"It shows that we are in child shell" - root@kali:~/sandy# ls -lh
total 0
-rw-r--r-- 1 root sandeep 0 Jun 12 17:08 operators2.txt
-rw-r--r-- 1 root root 0 Jun 12 16:50 operators.txt
- root@kali:~/sandy# exit
exit
"exiting from child shell"
groups
This command lists all the group names from which the user belongs- For example
- root@kali:~# groups
root
groupadd
This command is used to create groups,so that user can be listed in that group
- For example
- root@kali:~# groupadd sandy
- It will create group name sandy in /etc/group file
groupmod
It is used to change the particular group definition in the system database- For example,to change the name of any group
- root@kali:~# groupmod -n linux sandy
- -n is to change name
Hence group sandy is changed to linux
groupdel
This command is used to permanently delete the particular group- For example
- root@kali:~# groupdel sandy
- It will delete the group named sandy from the system database
useradd
This command is used to add user to any particular group.- For example
- root@kali:/etc# useradd -g linux user1
- It will add user named user1 to the group linux
usermod
It is used to modify the user account within the system database.It modifies the system account files to show the
changes that are specified on the command line.
- For example
- usermod -a -G sandy,linux user1
- user1 will be member of groups sandy and linux>
- -a is used to add the user to the supplementary group(s).
- -G represents list of supplementary groups which the user is also a member of.
If the user is currently a member of a group which is not listed in the command,
then the user will be removed from that group.
gpasswd
It is used to give control of group membership to another user with the gpasswd command.It is used to set administrators to group,
who can control the membership of different users
- For example
- root@kali:~# gpasswd -A sandeep sandy
- It will make sandeep the administrator of group sandy
- root@kali:~# su - sandeep
- It will make sandeep the super user
- $ id user1
- uid=1005(user1) gid=1004(sandy) groups=1004(sandy)
- $ gpasswd -a google sandy
- Adding user google to group sandy
- $ id google
- uid=1007(google) gid=1004(sandy) groups=1004(sandy)
- $ tail -1 /etc/group
- sandy:x:1004:google,sandeep
- Information about group administrators is kept in the /etc/gshadow file.
newgrp
It helps to start a child shell with new primary group.Thus it helps to change the current group with another named group
- For example
- root@kali:~# mkdir sandy
- root@kali:~# cd sandy/
- root@kali:~/sandy# touch operators.txt
- root@kali:~/sandy# ls -lh
total 0
-rw-r--r-- 1 root root 0 Jun 12 16:50 operators.txt - echo $SHLVL
1
"It shows that we are in parent shell" - root@kali:/etc# newgrp sandeep
- root@kali:/etc# echo $SHLVL
2
"It shows that we are in child shell" - root@kali:~/sandy# ls -lh
total 0
-rw-r--r-- 1 root sandeep 0 Jun 12 17:08 operators2.txt
-rw-r--r-- 1 root root 0 Jun 12 16:50 operators.txt - root@kali:~/sandy# exit
exit
"exiting from child shell"
download file now