How to share files and folders between Linux user accounts
January 12, 2021Since I'm working from home full time, I use my Ubuntu desktop PC for both work and personal stuff. To keep things neatly separated however, I have two different user accounts for that:
z1c0: The user account for my personal stuff.work: Well, my work account.
The Permission denied Problem
From time to time, however, I need to copy files from one account to the other, which usually goes like this:
Step 0: Permission denied
$ cp foo.png /home/z1c0/Pictures
cp: cannot create regular file '/home/z1c0/Pictures/foo.png': Permission denied

Of course, you can sudo and chown your way around this problem, but I was looking
for a permanent solution.
A Linux group to the rescue!
Whenever this problem came up, it involved sharing a file or folder somewhere in
the user's home (the ~) folder. So this was where the solution needed to be applied.
To fix this once and for all, I decided to make one account's home folder
and its contents recursively available to the other account and vice versa.
This is easily implemented using groups.
Step 1: Create a new group
I created a new group named wolfgang.
sudo groupadd wolfgang
Step 2: Add the user accounts to the new group
The accounts work and z1co are added to the wolfgang group.
sudo usermod -a -G wolfgang work
sudo usermod -a -G wolfgang z1c0
Step 3: Log out and back in
Changes in group assignments only take effect when users log out and back in. Of course, you can also do this after the last step, but now is the best time.
Note: For some reason the last time I tried this, I even had to reboot my system until the group changes came into effect. Usually, logging out should be fine AFAIK though.
You can verify the changes by running the groups command or listing the group members
for the new group.
$ getent group wolfgang
wolfgang:x:1003:work,z1c0
Step 4: Assign files to the group
With the chgrp command we make all files and folders in the home folder of
the z1c0 account available to the wolfgang group. Then we use the chmod
command to give this group full rwx (read, write, execute) rights.
sudo chgrp -R wolfgang /home/z1c0
sudo chmod -R 770 /home/z1c0
The we do the same thing for the work user.
sudo chgrp -R wolfgang /home/work
sudo chmod -R 770 /home/work
Step 5: Repeat Step 0
Now you should be able to mv and cp files and folders between these two user
accounts without any problem.
cp foo.png /home/z1c0/Pictures