Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In Linux, I have the following id:

uid=1005(username) gid=1005(username) groups=1005(username),33(www-data),1002(git)

I'd like to change my effective gid to git so that everything I create will belong to gid group. E.g. touch testfile gives owner=username group=git

How?

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

You can change your current group using the newgrp commmand:

~$ id
uid=1000(bas) gid=1000(bas) groups=1000(bas),24(cdrom)
~$ newgrp - cdrom
~$ id
uid=1000(bas) gid=24(cdrom) groups=1000(bas),24(cdrom)
~$ touch foo && ls -l foo
-rw-r--r-- 1 bas cdrom 0 Jul 24 13:58 foo

Groups can be locked or have a password. See the manpage for more details.

man newgrp
share|improve this answer
add comment

If you have suitable sudo permissions you can run a shell as the new group

username    ALL=(:git) /bin/bash

this allows username to run /bin/bash as username:git

id
uid=1005(test) gid=1005(test) groups=1005(test)

touch testfile
ls -l testfile
-rw-rw-r-- 1 test test 0 Jul 24 11:19 testfile

sudo -g git /bin/bash
id
uid=1005(test) gid=1003(git) groups=1005(test)
rm testfile
touch testfile
-rw-r--r-- 1 test git 0 Jul 24 11:21 testfile

You could even use exec to replace your current shell with the one running as the git group

exec sudo -g git /bin/bash
share|improve this answer
add comment

Globally, you cannot - not without changing the user's primary group.

But on a per-directory basis, this is trivially achieved by setting the setgid bit on the directory, and chowning said directory to the correct group.

Anything created below said directory will inherit the gid, regardless of the user who created it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.