chmod 444 (r--r--r--)
Everyone can read the file and nobody can write to it. It marks a file as finished: published, generated, or not meant to be edited by hand.
This tool runs in your browser. We never send what you type or paste to a server.
What chmod 444 sets
| Who | Digit | Read | Write | Execute | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 4 | yes | no | no | r-- |
| Group (g) | 4 | yes | no | no | r-- |
| Others (o) | 4 | yes | no | no | r-- |
chmod 444 file # numeric
chmod a=r file # the same, in symbolic form
ls -l shows this mode as -r--r--r-- on a file and dr--r--r-- on a directory. On a file, execute means it can be run as a program. On a directory, read lists the names inside, write creates, renames and deletes entries, and execute (search) is what lets you enter it and reach the files inside.
When 444 is worth using
444 is 644 with the owner's write bit removed. That one bit is a seat belt for yourself:
- Generated files. Build output, a rendered configuration file or anything else that is overwritten by a tool. Marking it read-only makes your editor warn you before you change the copy instead of the source.
- Reference material. Released documents, data sets and archives that many people read and nobody should alter.
- Protection from a slip.
echo x > fileand similar accidents fail with "Permission denied" instead of emptying the file.
It is not for secrets. Everybody can read a 444 file; a read-only secret is 400.
Why a 444 file can still be changed or deleted
The mode stops writing into the file. It does not stop the file being removed or swapped for another one, because those operations change the directory, and it is the directory's write permission that counts. Anyone who can write to the directory can delete a 444 file or rename a different file over it. Programs that save by writing a new file and renaming it into place get round the mode the same way.
The owner can also simply run chmod u+w, and root can write to the file whatever its mode.
If a file really must not change:
- Put it in a directory the other users cannot write to, owned by a different account from the one that might modify it.
- On Linux, root can set the immutable attribute with
chattr +i file. An immutable file cannot be modified, deleted or renamed, even by root, until the attribute is cleared withchattr -i. - Or keep it on a file system mounted read-only.
444 on a directory
A mistake nearly every time. Without the execute bit nobody can enter the directory or reach the files in it; the read bit only lets people list the names. A read-only directory is 555 (r-xr-xr-x).
This is why chmod -R 444 on a tree locks you out of it. To make a whole tree read-only, remove the write bits and leave everything else alone:
chmod -R a-w release # files and directories lose write only
find release -type f -exec chmod 444 {} + # or set exact modes: files
find release -type d -exec chmod 555 {} + # and directories
Deleting such a tree later needs the write bit back on its directories first: chmod -R u+w release, then rm -r release.
Setting and checking it
chmod 444 report.pdf
chmod a-w report.pdf # same intent; keeps execute bits if there are any
stat -c '%a %n' report.pdf # GNU/Linux
stat -f '%Lp %N' report.pdf # macOS and BSD
chmod u+w report.pdf # editable by the owner again (644)
444 compared with its neighbours
Questions
Why can I still delete a file that is chmod 444?
Deleting a file changes the directory that lists it, not the file, so it is the directory's write permission that decides. The file's own mode only makes rm ask for confirmation first. To prevent deletion, remove write permission from the directory or, on Linux, set the immutable attribute as root.
What is the difference between chmod 444 and 644?
Only the owner's write bit. With 644 the owner can edit the file directly; with 444 the owner has to run chmod u+w first. Other users are treated the same by both: they can read and cannot write.
Does chmod 444 stop root from changing the file?
No. The superuser is not subject to read and write permission checks, so root can modify a 444 file directly. On Linux the immutable attribute, set with chattr +i, is what stops root too until it is removed.