TT Lab
Blog Get started
Learn Learning paths Courses

Linux Fundamentals

Permissions Attach to Actions, Not Files

Continue in TT Lab

In one sentence

The three letters rwx mean different things on a file and on a directory. If you do not know this difference, you will keep running into incidents such as "why was a read-only file deleted?"

Why this matters

A team decided to create one shared directory and use it together. Everyone uploads their own files and promises not to touch anyone else's. Then one day someone's file disappeared. The file's permissions were 644, and the owner was unchanged.

If you ask here, "how was it deleted when the permissions are 644?", there is no answer. Deleting is not an action on the file; it is an action on the directory. Deleting a file means detaching one name from a directory, and for that you only need write permission on that directory. It does not matter whether the file itself is read-only or belongs to someone else.

Unix added one more bit for exactly this problem: the sticky bit.

How it works

First, here is how the same letter changes depending on what it is applied to.

Bit On a file On a directory
r You can read the content You can list the entries (ls)
w You can modify the content You can create and delete entries
x You can execute it You can enter it and pass through the path

If a directory has only r and no x, you can see the names but cannot read information about the files inside. Conversely, if it has only x and no r, you cannot list it but can access an entry if you know its exact name.

Three special bits are layered on top. They are the leading digit of the four octal digits.

So the standard form of a team shared directory is 2770 (group inheritance plus blocking anyone outside the group), and if you want anyone to upload but not delete other people's files, it is 1777.

What you see in the field

First, you added a group but it has no effect. Even if you run usermod -aG devs alice, it is not reflected in a shell that alice was already logged in to. The group list is fixed when a process is created. A new login session is needed. If you leave out -a (append) here, all existing supplementary groups are wiped out, which is an incident that often happens on production servers.

Second, octal and symbolic notation. There is the absolute style, such as chmod 750 run.sh, and the relative style, such as chmod g+x,o-rwx run.sh. In deployment scripts the result must be the same regardless of the current state, so absolute values are safer. Conversely, when you touch a large number of files, symbolic notation reduces mistakes.

Third, check permissions with a command, not with your eyes. In the output of ls -l, counting the letters of -rwxr-x--- by eye is less accurate than running stat -c %a. When you write grading scripts or check scripts, always use stat.

Who decides the permissions of a newly created file

Many people have typed chmod but do not know where the permissions of a file at the moment it is first created come from. What decides this is umask.

When a program creates a file, it tells the kernel the permissions it wants; regular files usually request 666 and directories 777. The kernel then removes the bits that are set in umask and creates the file. With a umask of 022, a file becomes 644 and a directory becomes 755. So umask is not an "allow" list but a "forbid" list, and if you get that direction wrong when reading the value, the calculation comes out backwards.

Two things often cause trouble here. First, the execute bit is never added by umask. The request is 666, so there is no execute bit to begin with, which is why a newly created script always needs chmod +x. Second, umask exists per process and is inherited by children, so a service's umask is decided by the environment that started it. The value differs between when a person starts it from a shell and when the system starts it at boot, so you can end up with only the files created by a manually restarted service having different permissions. For such services it is safer to state the umask explicitly in the configuration.

It also helps to fix an order of checks for when permissions look strange: who created it → what was the umask at that time → is setgid set on the directory → who ran chmod afterward. In particular, for the third one, as we saw earlier, the group changes, so if the file's owning group differs from the creator's primary group, look at that directory first.

Finally, a note on access control lists (ACLs). With only owner, group, and others, you cannot express "give read access to just this one person," so in that case you layer individual permissions on with setfacl. However, in ls -l only a single + is added at the end, which is easy to miss, so when the permissions cannot be explained, check with getfacl.

Permission denied is a verdict on the whole path

Reports like "there is disk space left and the file permissions are right, but the service cannot write" usually drag on because people judge by looking at a single file. When opening a file, the kernel checks one step at a time from the top of the path. Every directory you pass through needs x, and the last step needs the bit that matches what you want to do. To append to an existing file you need w on that file; to create a new file you need w on that directory. So the same "Permission denied" splits into three places where you can be blocked.

The owner of the result file changed. If someone runs the job once as root to check an outage, the result file created at that moment is owned by root. From then on, the service account cannot append to that file. The directory is fine, so one line about the file shows it right away, but people usually suspect the directory first.

x is missing on an intermediate directory. If you "tidy up log permissions" by putting the same number as a file (such as 660) on a directory, x disappears. No matter how healthy the files and subdirectories below it are, you cannot pass through, so you cannot open them. If you only look at file permissions, this cause stays invisible to the end.

The owner of the directory changed. A job that creates a new file every day needs w on the directory. If root deleted and recreated the directory, you can still write to files that existed until yesterday, but you cannot create today's file. The phrase "it worked until yesterday" is the clue to this cause.

The fastest way to tell these three apart is to become that account. Running it as root often works, but that proves nothing; root, especially with the capability to bypass permission checks (DAC_OVERRIDE), passes whatever the bits are. Scan the whole path at once with a tool such as namei -l, which lays out the permissions of each step of a path, then try it once directly as that account, and the blocked step reveals itself. When you fix it, open only as much as that account needs. Opening it with 777 makes the symptom disappear, but it also gives write access to other services and people, and it is the first thing an audit flags.

The same habit applies to shared directory incidents. If two people in the same group cannot open each other's new files, look at the group field of the file before group membership or umask. If setgid is missing on the directory, new files are created with the creator's personal group, and even with the group read bit set, the other person is not in that "group". If you only fix the group of the existing files and do not set setgid again, the same thing happens with tomorrow's new files.

What you will do in the next lab

You create users and groups yourself, set up a shared directory with setgid and sticky set, and write a check script that inspects whether "this file has the permissions we expect" and reports the result through its exit code. The last two steps raise incidents: following the path, you tell apart and fix why three service accounts each cannot write for a different reason, and you find why members of the same team cannot open each other's new files and fix it so it does not happen again.