11

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.

3 Answers 3

11

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.

2
  • you r changing the primary group of the user. i want to do it with out changing the primary group. Thank you, i found the solution. i posted the answer.
    – Necktwi
    Nov 24, 2012 at 4:44
  • As @gertvdijk mentions in his comment below, this does not change the primary group permanently; newgrp GROUP will create a new shell and login as the current user, setting the user's primary group to GROUP.
    – jrw32982
    Nov 14, 2019 at 21:29
8

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 .
0
0

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

login <username>:<group>
2
  • its working to login in shell. But in my situation i am using ftp client to read or write files. i use sftp://user@host to login. sftp://user:group@host don't work. how to do it in case of ftp login??
    – Necktwi
    Nov 24, 2012 at 4:56
  • 1
    that's changing the primary group the same way as in my answer; temporarily for that session.
    – gertvdijk
    Nov 24, 2012 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .