I couldn't exactly understand function of newgrp. Does it helps to move from one group to another group for which particular user is member. Or it allow to become member to group for which the user is not member ,but has group password. I am not clear about it . Can any of you could give me a clear view about it .
2 Answers
To understand what the newgrp
command does, you need to understand that when you add a user to a group, the new user won't actually be added to the group, until you log out and log back in again. This is often a hassle because it kills all your programs. newgrp
allows you to add a user to a group and make it take effect in the current login session.
usage
# show groups user "ubuntu" belongs to
ubuntu@ubuntu:/usr/share/man$ groups ubuntu
ubuntu : ubuntu adm cdrom sudo dip plugdev lpadmin sambashare
# add user "ubuntu" to group: src
ubuntu@ubuntu:/usr/share/man$ sudo gpasswd -a ubuntu src
Adding user ubuntu to group src
ubuntu@ubuntu:/usr/share/man$ groups ubuntu
ubuntu : ubuntu adm cdrom sudo dip src plugdev lpadmin sambashare
# ^^^
# user ubuntu is "technically" now added to group `src`
# now try do something that members of `src` can do
# i.e. create a file in /usr/local/src/
ubuntu@ubuntu:/usr/share/man$ ls -alh /usr/local/
total 0
drwxr-xr-x 1 root root 100 Jul 20 08:42 .
drwxr-xr-x 1 root root 180 Dec 2 20:19 ..
drwxr-xr-x 1 root root 80 Dec 2 20:15 bin
drwxr-xr-x 2 root root 3 Jul 20 08:42 etc
drwxr-xr-x 2 root root 3 Jul 20 08:42 games
drwxr-xr-x 2 root root 3 Jul 20 08:42 include
drwxr-xr-x 4 root root 49 Jul 20 08:45 lib
lrwxrwxrwx 1 root root 9 Jul 20 08:42 man -> share/man
drwxr-xr-x 2 root root 3 Jul 20 08:42 sbin
drwxr-xr-x 1 root root 60 Dec 2 19:53 share
drwxrwxr-x 1 root src 60 Dec 2 19:55 src
ubuntu@ubuntu:/usr/share/man$ touch /usr/local/src/a_file
touch: cannot touch '/usr/local/src/a_file': Permission denied
# it doesn't work
# now run "newgrp" on src group
ubuntu@ubuntu:/usr/share/man$ newgrp src
ubuntu@ubuntu:/usr/share/man$ touch /usr/local/src/a_file
# now you are truly part of src group
ubuntu@ubuntu:/usr/share/man$ ls -alh /usr/local/src
total 0
drwxrwxr-x 1 root src 80 Dec 2 21:26 .
drwxr-xr-x 1 root root 100 Jul 20 08:42 ..
-rw-rw-r-- 1 ubuntu src 0 Dec 2 21:26 a_file
newgrp
runs a shell setting its real group ID (see man 7 credentials
) to the requested group, which can be either to one of the groups to which the user belongs, or a group of which the user knows the password. (Groups can have passwords just like user accounts.)
To return to the situation before newgrp
use exit
to terminate the shell executed by newgrp
. Because newgrp
runs a new shell you need to be careful when using it in scripts (hint: pipe a command into it, or use a here-document).
-
after reading
man credentials
Im wondering doesnewgrp
make a system call to change the group id of the running process ? Dec 2, 2016 at 9:33 -
@the_velour_fog: The running process is
newgrp
... . What you mean is why isn'tnewgrp
a builtin. It's not a builtin because it must be able to change the group to a group of which the user is not a member; that's why it must be setuid to root.– AlexPDec 2, 2016 at 9:47 -
ah ok, after running
htop
andpstree
I understand your point aboutnewgrp
actually forking an entire new shell! that explains why I had to exit twice when usingnewgrp
. Im just testing it now, but Im assumingnewgrp
allows you to temporarily join a group you arent a part of at all. as you say you only need to know the password to the group ( or a user who belongs to the group) Dec 2, 2016 at 9:51 -
1my testing is showing that, you need to already belong to the group, before you can switch to it ? not sure if its relevant but I keep getting an error
newgrp: failed to crypt password with previous salt: Invalid argument
if the user doesn't already belong to the group Dec 2, 2016 at 9:56 -
You must first assign a password to the group.
The user will be denied access if the group password is empty and the user is not listed as a member
.– AlexPDec 2, 2016 at 9:57