3. Example with permissions
In this example we will set up a shared directory between the users "bill" and "karen", where they can store their music files.
-
Most of the commands in this part need root permissions, so let's switch first to the superuser:
sudo su
whoami -
Let's create the users "bill" and "karen":
useradd -m -s /bin/bash billls /home/useradd -m -s /bin/bash karenls /home/tail /etc/passwd -
We also need to create a group for these two users:
groupadd musictail /etc/groupadduser bill musicadduser karen musictail /etc/group -
Now let's create a directory:
mkdir -p /usr/local/share/Musicls -ld /usr/local/share/MusicTo make this directory shareable we need to change the group ownership and the group permissions to allow writing:
chown :music /usr/local/share/Musicchmod 775 /usr/local/share/Musicls -ld /usr/local/share/MusicNow we have a directory that is owned by
rootand allows read and write access to the groupmusic. Usersbillandkarenare members of the groupmusic, so they can create files in this directory. Other users can only list the contents of the directory but cannot create files there. -
But we still have a problem. Let's try to create a file as user
bill:su -l billNow let's create an empty file, just for testing:
> /usr/local/share/Music/testls -l /usr/local/share/MusicThe group of the created file is
bill(which is the primary group of the userbill). Actually we want the group of the created file to bemusic, otherwisekarenwon't be able to access it properly.We can fix it by giving this command (as root):
exitchmod g+s /usr/local/share/Musicls -ld /usr/local/share/MusicWhen we talked about permissions we did not mention the special permission s. When we give this permission to the group of a directory, the files that are created on this directory will belong to the same group as the directory.
Let's try this by creating another test file as user
bill:su -l bill> /usr/local/share/Music/test_1ls -al /usr/local/share/MusicNotice that the second file belongs to the group
music.exit
whoami
exit
whoami