There are several options.
Yes, you can add both users to the same group and set write access to this group:
sudo groupadd somegroup
sudo adduser user1 somegroup
sudo adduser user2 somegroup
sudo chgrp somegroup /var/lib/somedir
sudo chmod g+rwx /var/lib/somedir
You can also set the directory to be owned by user1
and belonging to group to which user2
belongs, and then set write access for both owner and group:
# assume group2 is a group to which user2 belongs
sudo chown user1:group2 /var/lib/somedir
sudo chmod ug+rwx /var/lib/somedir
Finally, you can use ACLs. First you have to install the commands to manipulate ACLs:
sudo apt install acl
Then you can eg. make the directory owned by user1
and add write access for user2
:
sudo chown user1 /var/lib/somedir
sudo chmod u+rwx /var/lib/somedir
sudo setfacl -m u:user2:rwx /var/lib/somedir
Or you can explicitly add access for user1
and user2
regardless of who is the owner of the directory:
sudo setfacl -m u:user1:rwx /var/lib/somedir
sudo setfacl -m u:user2:rwx /var/lib/somedir
BTW. You don't need write access on the parent directories (0777), what you need is the x
access to each parent directory only.