Who may change a mode
Only the owner of a file, or root, may change its permission bits. The kernel checks the UID of the process against the owner of the file before it allows a change. This rule is separate from the read, write, and execute bits themselves. A file can be read-only for you and still be yours, and you can change its mode because you own it.
That is why the labs in this module work on files that you own. You can change the mode of a file that you own, but you cannot change a file that belongs to another account. The same restriction applies to the owner and the group, which the next unit covers.
Symbolic mode
A symbolic mode has three parts: who, the operation, and the permission. The who part is a combination of u for the owner, g for the group, o for other, and a for all three. If you leave it out, chmod acts on all three classes, but only the bits that are allowed by the umask, which the umask unit explains. The operation is + to add bits, - to remove them, or = to set them exactly. The permission part is any of r, w, x, and in later units s and t.
touch tool.sh
chmod u+x tool.sh
stat -c "%A %a %n" tool.shThe first command creates the file, which starts with a default mode. The second adds execute for the owner only. The third prints the result, which shows the owner triplet with an x and leaves the group and other triplets alone. This is the safest kind of change, because it names exactly the class you mean.
The equals operator is different. The command chmod u=rw tool.sh sets the owner triplet to exactly read and write, and it clears execute if it was present. Use equals when you want to know the final state of a class, and use plus or minus when you want to change one bit and leave the rest alone.
Octal mode
An octal mode sets all nine bits at once. The three digits are the owner, the group, and other, in that order, and each digit is the sum of read as 4, write as 2, and execute as 1. The command chmod 640 notes.txt sets the owner to read and write, the group to read, and other to nothing. The command chmod 755 run.sh gives the owner full access and everyone else read and execute.
| Digit | Owner | Group | Other |
|---|---|---|---|
| 7 | rwx | rwx | rwx |
| 6 | rw- | rw- | rw- |
| 5 | r-x | r-x | r-x |
| 4 | r-- | r-- | r-- |
| 0 | --- | --- | --- |
The table shows the digits that appear most often. An octal mode is a complete statement, so it is the right choice when a script must produce a known result, and it is the wrong choice when you only want to add one bit to a file whose other bits you have not checked.
Recursive changes are dangerous
The -R option applies the change to a directory and everything inside it. It is convenient, but it is easy to set execute bits on ordinary data files, or to remove the owner write bit from a directory that you need to change later. Before you use -R, list the files with find and check which ones the change would touch. A capital X in a symbolic mode is useful here, because it adds execute only to directories and to files that already have some execute bit.
Never set mode 777 on a file to make a permission error go away. That gives every account on the system write access, which is usually the real problem. Find the class that is missing the bit, and add only that bit.
Choosing a notation
Pick the notation that matches the question you are answering. If you are asking what a file should look like after the change, octal is clearer, because the whole mode is visible in three digits. If you are asking how to add one capability to a file you already have, symbolic is clearer, because the command names only the bit you want. Scripts usually prefer octal, because a script that says 644 produces the same result on every machine, while a script that says g-w depends on the current mode.
A common compromise is to use symbolic mode in an interactive shell and octal mode in a script that is meant to run again. An interactive change is quick to check by eye. A repeated script must set a known state, and an octal value is that state written down.
Symbolic modes can combine several operations in one command. The expression u+x,go-w adds execute for the owner and removes write for the group and other classes. Commas separate the clauses, and each clause is applied to the result of the one before it. Read the expression from left to right, the same way the kernel and the chmod program apply it.
The chmod command also accepts a reference file. The option --reference=FILE copies the mode of another file, which is useful when two files should match. That option is less common, but it removes the need to retype octal digits by hand, and it avoids the arithmetic mistakes that appear when a mode is written from memory.
Hands-on: change modes on your own files
Create a file, then change it with a symbolic mode, and check the result with stat -c %a. Repeat the work with an octal mode and compare the two. The labs for this unit ask you to add execute for the owner, to set a file to 640 with octal, and to remove write for the group and other classes with a symbolic command. Each check confirms the final mode, so you can see whether the notation did what you intended.