Set group password,its use and check which group after newgrp command in linux

Set group password,its use and check which group after newgrp command in linux

As per the title of my post it has 3 questions:

(1) How to set group password in Linux ?
(2) What is its use ?
(3) How to know you are using which group after using command newgrp ?

How to set group password in Linux

Follow the given below steps to set group password

Step 1: Either you can create a new group or use the existence group.
To create new group use below command.


groupadd group_name

for eg.

groupadd hr

Step 2: To set group password use below given command

gpasswd group_name

For eg.

gpasswd hr

How to use group passwd

To use the permission and privileges of group you can temporarily join the group by using its group password
User can temporarily join the group which has password already set by below given command

newgrp group_name

for eg.


newgrp hr

In below given example we will see, the user called john do not have read,write,execute permission in directory called /opt/testdir.
The ls -ld command shows it has ownership of joe and group is hr and permission is 770 means only owner joe and group hr have read write execute permission.
But user john can also “read write execute” only when it uses the newgrp command and give group password of hr group.
See the below given scenario, how the john will create a testfile after using newgrp command which he was earlier not able to do so

root@tuxworld:~# ls -ld /opt/testdir/
drwxrwx--- 2 joe hr 4096 Jul 31 01:12 /opt/testdir/
root@tuxworld:~# 
root@tuxworld:~# su -l john
$ whoami
john
$ 
$ touch /opt/testdir/testfile
touch: cannot touch `/opt/testdir/testfile': Permission denied
$ 
$ newgrp hr
Password: 
$ 
$ touch /opt/testdir/testfile
$ ls -l /opt/testdir/testfile
-rw-rw-r-- 1 john hr 0 Jul 31 02:00 /opt/testdir/testfile
$ 
$ exit
$ exit
root@tuxworld:~# 

Note: You can use this in other scenario above one was just an example

How to know you are using which group after using command newgrp

$ groups
john
$ newgrp hr
Password: 
$ groups
hr john
$ id
uid=1011(john) gid=1012(hr) groups=1011(john),1012(hr)
$ groups
hr john
$ whoami
john
$ 
$ id john
uid=1011(john) gid=1011(john) groups=1011(john)
$ 
$ groups
hr john
$ exit
$ exit

To describe about this query I will first start with example.

(A)In below command user name is john and I use the command whoami. By which I got to know the username which I login is john

(B)After this I use the command id john which shows the user john’s UID and GId by which I can also know which is its primary or supplementary GROUPS.(i.e john ,john [here group is also john])

(C)Then I use the command called groups now here you can see the output in which it shows hr group which was not earlier listed with id command.

(D) Hence to know which group you are using after newgrp command ,check with below given two commands
1: id username
2: groups

(E)Now in next step you can see in example, I typed two times exit command .
The first exit command is when it exit out from group called hr
And the Second exit command is when I am doing exit from shell of user john

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.