Skip to main content

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.

  1. Most of the commands in this part need root permissions, so let's switch first to the superuser:

    sudo su
    whoami
  2. Let's create the users "bill" and "karen":

    useradd -m -s /bin/bash bill
    ls /home/
    useradd -m -s /bin/bash karen
    ls /home/
    tail /etc/passwd
  3. We also need to create a group for these two users:

    groupadd music
    tail /etc/group
    adduser bill music
    adduser karen music
    tail /etc/group
  4. Now let's create a directory:

    mkdir -p /usr/local/share/Music
    ls -ld /usr/local/share/Music

    To make this directory shareable we need to change the group ownership and the group permissions to allow writing:

    chown :music /usr/local/share/Music
    chmod 775 /usr/local/share/Music
    ls -ld /usr/local/share/Music

    Now we have a directory that is owned by root and allows read and write access to the group music. Users bill and karen are members of the group music, so they can create files in this directory. Other users can only list the contents of the directory but cannot create files there.

  5. But we still have a problem. Let's try to create a file as user bill:

    su -l bill

    Now let's create an empty file, just for testing:

    > /usr/local/share/Music/test
    ls -l /usr/local/share/Music

    The group of the created file is bill (which is the primary group of the user bill). Actually we want the group of the created file to be music, otherwise karen won't be able to access it properly.

    We can fix it by giving this command (as root):

    exit
    chmod g+s /usr/local/share/Music
    ls -ld /usr/local/share/Music

    When 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_1
    ls -al /usr/local/share/Music

    Notice that the second file belongs to the group music.

    exit
    whoami

    exit
    whoami
Loading asciinema cast...