how to login as a member of specific group

i want to login as a member of non primary group to create files whose owner as a member of non primary group of current user.

-rwxr-xr-x 2 gowtham  gowtham      4096 Sep  5 14:48 defaultNewFile
drwxr-xr-x 2 gowtham  specificgrp  4096 Sep  5 14:50 requiredNewFile

i don’t want to change group ownership after creation of file with chown.

I am more interested in login as a member of a non primary group.

Asked By: Necktwi

||

newgrp will help you out. See below for a walkthrough. Should speak for itself.

$ id
uid=1000(gert) gid=1000(gert) groups=1000(gert),4(adm),[...]
$ touch file1
$ newgrp adm
$ id
uid=1000(gert) gid=4(adm) groups=1000(gert),4(adm),[...]
$ touch file2
$ ls -al
[...]
-rw-rw-r--  1 gert gert     0 Nov 23 11:58 file1
-rw-rw-r--  1 gert adm      0 Nov 23 11:59 file2

Yes, this will change the primary group, but not permanently. Just for that session/shell, so in scripting this should not be an issue.

Answered By: gertvdijk

Depending on why you want to do this, there may be another way.

If the setgid bit of a directory’s permissions is set, then all files created in that directory will be owned by the same group that owns the directory.

$ chgrp specificgrp .
$ chmod g+s .
$ touch newfile
$ ls -l newfile
-rw-r--r-- 2 gowtham  specificgrp  4096 Sep  5 14:48 newfile
$ ls -ld .
drwxr-sr-x 2 gowtham  specificgrp  4096 Sep  5 14:48 .

one can login in a shell as a member of specific group using

login <username>:<group>
Answered By: Necktwi
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.