Linux on call · Chapter 17 of 30

umask and special permissions

By the end of this chapter you can make a shared folder hand its group to every new file, stop people deleting each other's files, and choose the mode a file is born with. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Permissions and users
  • 5 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Anjali and Meera, the menu team, share menu-shared, and every file there ends up in its creator's own group:

ls -l menu-shared
total 8
-rw-r--r-- 1 student      student      33 Oct  7 01:09 diwali-menu.txt
-rw-r--r-- 1 tapri-anjali tapri-anjali 45 Oct  7 01:09 poster-notes.txt

(The times are when the chapter was built.) So the rest of the team cannot edit them. In uploads, open to everyone, Bunty deleted Meera's photo again. And Ravi wants this month's payouts in a file nobody else can ever read. Kabir's list is in note.txt.

The rwx bits from chapter 14 cannot do this. You could chgrp each new file by hand, forever. A folder that everyone may write to lets everyone delete in it too. And a file you chmod 600 after writing it was readable by others from the moment it was made.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Make new files in menu-shared take its group (setgid)

    Give the folder menu-shared the setgid bit, so every new file made in it gets the group tapri-menu.

    1. Run ls -ld menu-shared. Look at the group's three letters.

    2. Use chmod to add the setgid bit to menu-shared. You are not in tapri-menu, so use sudo.

    3. Run ls -ld menu-shared again. An s is now where the group's x was.

    To pass: menu-shared is still a folder in the group tapri-menu, it has the setgid bit, and the group still has read, write and enter (rwx) on it.

  2. Task 2

    Put the files already in menu-shared in tapri-menu, with group write

    setgid changes only new files. Fix the two files that are already in menu-shared.

    1. Run ls -l menu-shared. Look at the group of each file.

    2. Use sudo with chgrp to put every file in menu-shared in the group tapri-menu.

    3. Use sudo with chmod to give the group write on every file in menu-shared.

    To pass: every file in menu-shared (diwali-menu.txt, poster-notes.txt and any file you add) is in the group tapri-menu, and the group can write to it. Do not move or delete the two files.

  3. Task 3

    Let only a file's owner delete it in uploads (sticky bit)

    Give the folder uploads the sticky bit, so people cannot delete each other's files in it.

    1. Run ls -ld uploads. Everyone can write in it now.

    2. Use chmod to add the sticky bit to uploads. The folder is yours, so you do not need sudo.

    3. Run ls -ld uploads again. The mode now ends in a t.

    To pass: uploads has the sticky bit, others can still make files in it (write and enter), and kulhad-photo.txt is still in uploads.

  4. Task 4

    Write payouts.txt private from the start (umask 077)

    Make payouts.txt private from the moment it is made. The umask does this, not chmod.

    1. In the terminal, in the chapter folder, run umask 077.

    2. Then write this month's payouts into payouts.txt, for example with echo and >.

    3. Run ls -l payouts.txt. Its mode is 600. You may set the umask back to 022 after that.

    To pass: payouts.txt has mode 600, and you ran umask 077 in the chapter folder. A command with a typing mistake does not count, so type it again. Never run chmod on payouts.txt, not even once. If you did, run lab reset and do the chapter again.

  5. Task 5 · interview question

    Interview: why /tmp has the sticky bit

    Interview question: why does /tmp have the sticky bit?

    1. Run ls -ld /tmp and look at the last letter of its mode.

    2. Write in your own words what everyone may do in /tmp, and who may delete a file there.

    3. Save it in sticky.txt, in the chapter folder.

    To pass: sticky.txt talks about deleting (or removing, or renaming) a file, and about the file's owner.

What you will learn

  • setgid on a folder: new files inside take the folder's group.
  • The sticky bit: changes who may delete in a shared folder.
  • umask: takes bits away from every new file's mode, at birth.

More in Permissions and users

Build "umask and special permissions" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.