Ubuntu – Change user’s primary group – have new files made in that group

permissionsusers

I am setting up an environment where I am giving several users SSH access on my server. They are all trusted, but I want to corral them into a segment of the filesystem. I created the users like so:

adduser username -ingroup groupname

which works well enough. When I log in as one of them I can do this and get all the right answers:

$id -r -u -n
username
$id -r -g -n
groupname

I switched my own user account's primary group to groupname by doing this:

$usermod myuser groupname

I then logged out and back in. Now the files I would like the group to be able to access are under here:

$ls -l / | grep groupname
drwxr-xr-x  3 root     groupname    4.0K 2012-03-26 20:20 groupfiles
$cd /groupfiles
$ls -l 
drwxrwxr-x  2 root groupname    4.0K 2012-03-26 20:32 project

The permissions are by design, members of the group cannot alter files/folders under /groupfiles but they can add, edit, and delete under /groupfiles/project

The problem I'm having is that when I do this, I get the wrong group:

$touch test
$ls -l test
-rw-rw-r-- 1 myuser myuser 0 2012-03-26 20:58 test
$id -r -g -n
groupname

I need to make it so that the files created by vim, touch, etc have the correct group. I'm aware of newgrp() and the setgid bits. These are not what I am looking for. This works fine for the new users's I created, but doesn't work for my user. I'm not sure what happened, but it is working fine now. I've just leave this question here for future tinkerers.

Best Answer

I am guessing the behavior you want is sudo usermod -g groupname myuser .

Note: that command changes the group ownership for all files in $HOME, but not outside home. You may wish to change them back chgrp -R myuser $HOME

You then need to log off and back on for the changes you want to take effect.

If you do not want to change your 'myuser' primary group, newgrp is the way to go.

sudo usermod -g admin bodhi

ssh localhost

touch file

newgrp bodhi
touch file2

ls -l file file2
-rw-r--r-- 1 bodhi admin 0 2012-03-26 19:28 file
-rw-r--r-- 1 bodhi bodhi 0 2012-03-26 19:29 file2