Back to Explore
GuideVibeFix editorialUpdated Nov 26, 2026

Linux File Permissions from Zero: umask, chmod, and the nobody User, Measured Live

Root, umask 0007, zero packages: new files 660, new directories 770; under umask 0022, 644 and 755; run.sh exits 126 at 644 and 0 at 755; nobody is denied at 000 and reads SECRET-255 at 600; setgid directory 2775 passes group nogroup to new files.

Real photograph of an engineer holding a laptop in a server room aisle (illustrative photo)

Almost everyone learning Linux trips over file permissions sooner or later: a script uploaded to a server answers with Permission denied; a service that runs fine suddenly cannot read its own config file under a different user; worse, someone runs chmod 777 to make the problem go away, and now the private key file is readable by everyone — the equivalent of hanging the house key on the front door. Permissions look like a few letters and digits, but they decide who can do what to a file: read, write, execute, handed out to three groups of people. This guide explains the whole system once, and every number in it was actually run on this machine.

First, the demo environment, so you can reproduce the numbers. This machine runs as root: id prints uid=0(root) gid=0(root) groups=0(root), and the umask is 0007. The demo script is demo255_g1.sh, working in /tmp/perm_demo255, wiped and rebuilt on every run, with zero third-party packages. Why /tmp and not the workspace? Measured on this machine, the workspace lives on an overlay filesystem that refuses chown with Operation not permitted, and root there cannot even read a mode-000 file; /tmp is a tmpfs where both behave like the textbooks say, so the demo lives in /tmp. Root bypasses read-permission checks, so the denied half of the demo needs an ordinary user: nobody, uid 65534, group nogroup. The Chinese and English transcripts are both in the draft directory; reproducing takes two commands, bash demo255_g1.sh --lang zh and bash demo255_g1.sh --lang en, both exiting 0 with identical numbers.

The first lesson is reading the permission bits. Take the first column of ls -l, for example -rw-rw----, and split it into four parts: the first character is the file type, - for a regular file, d for a directory, l for a symbolic link; the next nine characters come in three groups of three, for owner, group, and others, each group in rwx order, r for read, w for write, x for execute, and - where a bit is missing. So -rw-rw---- means a regular file, owner can read and write, group can read and write, others get nothing. Section 2 of the demo prints the same facts more cleanly with stat: the file notes.txt has mode 660, symbolic form -rw-rw----, owner root, group root. With stat, -c %a prints the numeric mode, -c %A the symbolic mode, -c %U the owner, -c %G the group — all four fields at once, with far less noise than ls -l.

Default permissions for new files and directories are not fixed values; they are computed: the system starts from 666 for files and 777 for directories, then subtracts the bits the umask masks off. Section 1 of the demo creates files under this machine's umask 0007 and gets 660 for the file and 770 for the directory; in a subshell with umask 0022 the same steps give 644 and 755. The arithmetic is 666 minus 007 is 660, 777 minus 007 is 770, 666 minus 022 is 644, 777 minus 022 is 755. The umask works as a bitwise mask, not decimal subtraction, and it only ever removes permissions, never adds them — so a new file never comes with the execute bit, which is exactly why a script you upload does not run until you chmod it. Changing the umask affects only the current shell; the demo uses a subshell, (umask 0022; touch notes22.txt; mkdir work22), so the change is gone when the subshell exits. Many distributions default to umask 0022; this machine uses 0007, and that difference shows up directly in the permissions of new files, so check the umask first when troubleshooting.

The chmod command has two spellings, numeric and symbolic, and the numeric one is used most: r is 4, w is 2, x is 1, added up per group. 644 means owner read and write, group read-only, others read-only; 755 means owner everything, group and others read plus execute; 600 means only the owner can read and write; 640 means owner read and write, group read-only, others nothing. Symbolic spelling is handy when you do not want to recompute the whole group: u+x adds execute for the owner, g-w removes write for the group, o-r removes read for others. Section 3 of the demo runs chmod g-w on notes.txt and takes it from 660 to 640, exactly one bit, no more and no less. Four combinations cover daily use: 644 for ordinary files, 600 for private files, 755 for executable scripts, 755 for directories. Memorize those four and you will rarely need the manual.

Symbolic spelling has a few more forms worth memorizing in one pass. u, g, and o are owner, group, and others, a means everyone; plus adds a permission, minus removes one, equals sets exactly the listed permissions. chmod a+r adds read for everyone, chmod go-rwx strips read, write, and execute from group and others, chmod u=rw,go=r sets the owner to read and write and group and others to read-only, all in one command with no ambiguity. Symbolic form fits jobs where you change one bit: a script is written and needs its execute bit, chmod u+x is enough, no need to recompute the whole group. Numeric form fits jobs where you set the final state in one go: a deploy script that says chmod 755 install.sh tells the reader the exact end state. Neither is better; mix them freely, and always verify with stat afterwards that the number matches what you intended.

Numeric permissions come apart like a times table once you practice the split. 754 is owner 7, read plus write plus execute, group 5, read plus execute, others 4, read-only, together rwxr-xr--; 700 is owner only, read plus write plus execute, nobody else gets anything, the usual choice for a private directory; 640 is owner read and write, group read-only, others nothing, the usual choice for handing a read-only file to a team. One more digit in front is the special bits: 4 is setuid, 2 is setgid, 1 is the sticky bit, so 4755 is an executable with setuid, 2755 a directory with setgid, 1755 a directory with the sticky bit. The demo's shared directory is 2775 and /tmp is 1777, both read this way. When working out a mode, separate the special digit from the ordinary three: the special digit decides running with a different identity and new files inheriting a group, the ordinary three decide who can read, write, and execute — two different ideas, do not mix them.

The execute bit is the most misunderstood bit, because it means something different for files and for directories. For a file, no x means it cannot be run directly. Section 3 writes a two-line script, run.sh, that prints HELLO-RUN: at mode 644, running it exits 126 with Permission denied; after chmod 755, running it exits 0 and prints HELLO-RUN. Exit code 126 is worth remembering: the command was found and the file exists, but the system will not let you execute it, so the shell returns 126. For a directory, the x bit means you can enter it and reach the files inside; without it, the loosest file permissions in the world do not help. Section 4 hit this for real on the first run: the demo directory was mode 770, secret.txt was already owned by nobody at mode 600, and nobody reading it still got exit code 1; after the demo directory was changed to 755, the same command exited 0 and read SECRET-255. The lesson is practical: never check the file alone — check the x bit on every directory from the root down to the file, because one missing level blocks everything.

A symbolic link's permissions are a common optical illusion, verified in a follow-up check outside the demo: create a target file real.txt at mode 600, then a link link.txt pointing at it, and stat shows the link itself is always 777, symbolic form lrwxrwxrwx. The 777 is decoration; what decides access is the target file's permission: with the target at 600, nobody reading the link got exit code 1; after the target was changed to 644, nobody reading the link got the content DATA with exit code 0. The same check had a second twist: the first attempt after changing the target to 644 still failed, because the demo directory was mode 770 and nobody could not even enter it; changing the directory to 755 fixed that too. The rule for links is one sentence: always judge by the target file, ignore the link's own 777 — and remember the target file's directory needs its x bit as well.

There is an order to permission checks that is easy to miss: the system first asks whether you are the owner, and if so it applies only the owner bits; if not, it checks the group; only if neither matches does it look at the others. Only the first matching group applies; the three groups never add up. A follow-up check outside the demo proved it: a file owned by nobody, group nogroup, mode 070 — owner bits 0, group bits 7 — and nobody, who is both the owner and a member of nogroup, still got exit code 1 reading it, Permission denied, because the system refused at the owner step and never looked at the group bits. It works the other way too: if you are only in the group, the owner bits, however generous, do not apply to you. This order explains many mysteries where permission was plainly granted yet access was refused: first work out which group the person matches, then read that group's three letters, and never add the three groups together.

Owner and group are the other half of permissions, changed with chown and chgrp. Section 4 changes secret.txt to owner nobody and group nogroup; stat reports owner nobody, group nogroup, mode 000. The key point is the second half: changing the owner alone does not change the permissions, and nobody reading the file at 000 still got exit code 1, still Permission denied; only after the mode was changed to 600 did the read exit 0 and return SECRET-255 unchanged. chown changes whose file it is, chmod changes who can do what with it — change only one half and nothing is fixed. chown needs root; an ordinary user cannot change the owner of someone else's file. With chgrp, an ordinary user can only change to a group they already belong to: in a follow-up check, nobody trying to change a file's group to root was refused with exit code 1, Operation not permitted. And chown can change owner and group in one go — chown nobody:nogroup is the owner:group spelling for exactly that.

The umask change lasts only for the current shell; to make it stick, write it into the shell's startup file. A follow-up check verified this: a temporary startup file containing a single line, umask 0027, started an interactive shell with bash --rcfile, and the umask it reported was 0027. 0027 masks off group write and everything for others, so by the arithmetic the new defaults become 640 for files and 750 for directories. Many servers set umask 0027 precisely for that default-closed behavior: files the group can read, outsiders cannot read at all. Before editing a startup file, confirm which file you are editing: an interactive login shell reads .bash_profile or .profile, an ordinary interactive shell reads .bashrc, and editing the wrong one changes nothing. After the edit, open a new shell to verify; the umask in the old shell will not follow along.

Directories have two special bits that group collaboration and public directories both depend on. Section 5 creates a shared directory, changes its group to nogroup, and sets mode 2775, where the leading 2 is the setgid bit: in a setgid directory, new files automatically inherit the directory's group instead of their creator's group. The measured result is exactly that: inner.txt, created inside shared, has group nogroup and mode 660. Without setgid, a file created by root would belong to group root, and colleagues in the group could not write to it. In 2775, the leading 2 is setgid, and 775 is owner everything, group read plus write plus execute, others read plus execute. The other special bit is the sticky bit, and /tmp is the example: stat reports mode 1777, with the leading 1 as the sticky bit. 1777 means everyone can create files in /tmp, but each person can only delete their own files, never someone else's. Without the sticky bit, anyone in a public directory could delete anyone else's files, which is why /tmp and /var/tmp are always 1777. There is also a setuid bit; the classic example is the passwd program, which lets an ordinary user change their own password by writing a system file with temporarily raised privilege. That bit was not verified with a self-built program on this machine, so it is presented from the manual only: the mode starts with 4, for example 4755, and should be checked against your own system before use.

The chart shows the two default-permission sets measured in the demo: under umask 0007 and under umask 0022, for new files and new directories, with the numbers identical to the transcript.

Bar chart: under umask 0007 a new file is 660 and a new directory is 770; under umask 0022 a new file is 644 and a new directory is 755; plus the demo's key numbers: run.sh exits 126 at 644 and 0 at 755

Real deployments show permission problems most often as a web page returning 403: the site root is 755, index.html inside is 644, everything looks right, yet the page will not load — usually because some directory in the middle is missing its x bit, or the file's owner is wrong, or the group does not contain the user the web server runs as. Troubleshooting still follows the same five steps: stat the file for its three facts, walk the directories from the root checking each x bit, run id to confirm the web user is in the file's group, and check the umask to see whether freshly uploaded files were masked to 600. One point ties directly to the SSH keys guide: the ~/.ssh directory must be 700, authorized_keys inside it must be 600, and the home directory itself must not be writable by others — with the wrong permissions, the server refuses key login outright without an extra word of explanation. Those three are from the manual; no SSH server was set up to verify them on this machine, so run them through on a test server first. Treat permission, owner, group, and parent directories as a four-item checklist, run it every time you upload a file or switch users, and most 403s and Permission denied errors never happen.

Troubleshooting has a fixed order that saves a lot of time. First, stat the file for three facts: the numeric mode, the owner, and the group, confirming exactly who the file itself lets in. Second, check the parent directories: from the root down, every level needs its x bit for the person in question, and one missing level blocks everything — section 4's trap came from exactly this. Third, check the umask: when new files come out with the wrong permissions, look at whether the current shell uses 0007 or 0022 before deciding whether to change the umask or chmod the file afterwards. Fourth, check group membership: run id and groups and confirm the person is actually in the file's group, because group bits, however generous, mean nothing to a non-member. Fifth, check identity: which user you are right now, because root and ordinary users play by different rules — in the demo, root reading a 000 file exited 0 while nobody reading the same file exited 1, same permissions, different people, different results.

Finally, seven rules you can copy as-is. First, least privilege: files 644, directories 755 by default, enough is enough, never chmod 777 to save trouble. Second, private files at 600: keys, tokens, and password files are always 600, owned only by you, nothing for group or others. Third, share with a group plus setgid, not 777: put collaborators in one group, set the directory to 2775, and new files land in the group automatically — collaborative and still closed to outsiders. Fourth, stat before you change anything: record the original mode so a bad change can be undone exactly, and so troubleshooting has something to compare against. Fifth, chmod +x after uploading a script: new files never carry the execute bit, so when you see 126, check the permission bits before suspecting anything else. Sixth, be extra careful with recursive changes: chmod -R changes every file and subdirectory underneath, one wrong command rebuilds the whole tree, and the manual is explicit about the -R risk — test on a single file without -R first, then decide whether to recurse. Seventh, a group change only takes effect after logging in again: a user just added to a group still has the old membership in an old shell, id will not show the new group, so open a fresh shell and verify. Follow these seven and permission problems will mostly stop finding you.

Walk through this checklist once and you can reproduce everything in this guide on your own machine. First, run id and umask to confirm who you are and what the umask is — on this machine, root and 0007. Second, create one file and one directory, stat them for 660 and 770, then enter a subshell with umask 0022 and compare with 644 and 755. Third, write a two-line script, run it once at 644 and remember 126, then at 755 and remember 0 and HELLO-RUN. Fourth, try reading as nobody: at 000 the exit code is 1, at 600 it is 0, and the content is SECRET-255. Fifth, build a setgid directory at 2775, create a file inside it, and stat it to confirm the group was inherited from the directory. The demo script demo255_g1.sh and both transcripts are in the draft directory; two commands reproduce it all, bash demo255_g1.sh --lang zh and bash demo255_g1.sh --lang en. When reproducing, note that /tmp/perm_demo255 is wiped and rebuilt on every run, so old files never linger, and you can verify every number against the transcript with stat afterwards. What was not measured on this machine is also stated plainly: the setuid program is described from the manual with no self-built program to verify it; the ACL tools setfacl and getfacl are not installed on this machine, so install them and follow the manual; there are no usable SELinux or AppArmor management commands on this machine, so they are out of scope here; and chown is refused on the workspace's overlay filesystem, which is why the demo runs in /tmp — on another machine, verify that first.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles