I have 2 users user1 and user2 in my Ubuntu PC. user1 was the one I created during installation. I created user2 to execute a python program. Now, this program is present in the path /home/user1/programs/. So, user2 is executing a program located in the home directory of user1. During the execution, I'm unable to create files inside the output directory /home/user1/programs/Output/. So, I went ahead to check the permissions and found the following:

$ ls -l /home/user1/programs/
drwxrwxr-x 10 user1 user1 4096 Sep  6 11:30 Output
drwxrwxr-x  2 user1 user1 4096 Sep  5 17:50 source

$ cd Output

$ mkdir dummy1

$ ls -l
drwxrwxr-x 2 user1  user1  4096 Sep  6 11:30 dummy1

$ cd dummy1

$ vi a.txt

$ ls -l
-rw-rw-r-- 1 user1 user1 7 Sep  6 11:31 a.txt

$ cd ..

$ sudo mkdir dummy2

$ ls -l
drwxr-xr-x 2 root root 4096 Sep  6 12:37 dummy2

It's clear that other users like user2 don't have write permissions within the home directory of user1.

Now, I want to add the users user1 and user2 into the same group with same permissions so that user2 can get a rwx (7)? How to create such a group?

sudo -i
addgroup mygroup
addgroup user1 mygroup
addgroup user2 mygroup
chgrp -R mygroup /home/user1/programs/Output/
chmod -R ug+rwX  /home/user1/programs/Output/
find /home/user1/programs/Output/ -type d -exec chmod g+s {} +

This adds a group called mygroup, adds both users to that group, and changes the group-ownership of /home/user1/programs/Output/ (and all files & sub-directories) to mygroup.

It and then sets the permissions of the directory to read, write, execute and all files in it to allow read and write. Again, this is applied recursively to all sub-directories of ../Output/

Finally it sets the setgid bit on the directories with chmod g+s so that all files and sub-directories created underneath /home/user1/programs/Output/ will be in mygroup.

All of this needs to be done as root, hence the sudo -i to start a root shell first and the exit at the end to quit the root shell.

  • Looks neat. Trying this out now. Any possible negative side-effects if I add the entire home directory of both users (by doing steps 5 and 6 for user1 and user2)? Just being curious. – skrowten_hermit Sep 9 at 7:36
  • 1
    no side-effect, but the effect will be that user1 and user2 will have full read and write access to each other's files and sub-directories (including ~/mail, ~/.gnupg, ~/.ssh/ and everything else. actually ssh and gnupg would probably complain about permissions, by default they're fussy about perms). I wouldn't recommend it. – cas Sep 9 at 7:42
  • If a file in /home/user1/programs/Output/ has a description of drwxrwxr-x, won't adding user1 and user2 to mygroup automatically give user2 write and execute access? – skrowten_hermit Sep 9 at 10:03
  • 1
    yes, if the file is is owned by group mygroup and both those users are in that group. All the chmod + chgrp stuff in my answer is to ensure that that is the case for that directory. – cas Sep 9 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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