5.3. Controlling File Access
Every file and directory on a Debian system carries an owning user, an owning group, and nine permission bits, plus a handful of special bits this section introduces along the way. On each access, the kernel compares the requesting process's UID and group list against that record and answers one question: does the class that matches this process contain the right being requested? Root alone is exempt. This section works through the check with real files, then extends it with the setgid and sticky bits, the umask, and POSIX ACLs.
The examples use the shared workspace from Section 5.2.1. When a block
below is labeled as sam, it runs in sam's login shell - reached from
john's session with su - sam, which asks for sam's password exactly as
the su - tempuser invocations above did; an SSH login works identically
and so does a console. John's blocks run in his own session.
5.3.1. Ownership and Read, Write, and Execute Permissions
Figure 5.2 shows the whole decision at once; the demonstrations then walk each branch of it.
Figure 5.2. Conceptual flow of a single permission check: root bypasses the bits; otherwise exactly one class - owner, group, or other - is selected, and the request must fit inside that class's bits.
As john, create the file the section will work with. The redirection writes the text to the file; the command itself prints nothing, and the terminal stays silent:
$ printf "Quarterly report for the project.\n" > /srv/handbook-project/report.txt
$ cat /srv/handbook-project/report.txt
Quarterly report for the project.
The file was created by john's shell with his identity and his current
umask (Section 5.3.3), so it belongs to him and, because the directory's
setgid bit put the group there, to project. Make the sharing explicit
with a mode that grants the group write access:
$ chmod 660 /srv/handbook-project/report.txt
$ stat -c "%A %a %U %G %n" /srv/handbook-project/report.txt
-rw-rw---- 660 john project /srv/handbook-project/report.txt
The nine bits are three classes of three, and each digit of the octal mode is one class:
| Bits | Right on a file | Digit |
|---|---|---|
r-- |
Read the contents. | 4 |
-w- |
Write (modify or truncate) the contents. | 2 |
--x |
Execute: run the file as a program. | 1 |
rw- |
Read and write. | 6 |
rwx |
All three. | 7 |
660 is therefore rw- for the owner (6), rw- for the group (6), and
--- for other (0). The classes are tried in order and exactly one wins:
john's processes match the owner class and never consult the group bits;
sam's match the group class and never consult other. Sam, as a member of
project, can now read and append:
As sam:
$ cat /srv/handbook-project/report.txt
Quarterly report for the project.
$ printf "Reviewed by sam.\n" >> /srv/handbook-project/report.txt
The append writes nothing to the terminal; read the file back in john's session to see both lines:
$ cat /srv/handbook-project/report.txt
Quarterly report for the project.
Reviewed by sam.
Take the group rights away and the same operations fail:
$ chmod 600 /srv/handbook-project/report.txt
As sam:
$ cat /srv/handbook-project/report.txt
cat: /srv/handbook-project/report.txt: Permission denied
The read failed too - 600 removed all group rights, and sam's access
fell from the group class to other, which has nothing. Back in john's
session, restore the shared mode before continuing:
$ chmod 660 /srv/handbook-project/report.txt
The execute bit behaves differently from the other two, and the difference matters enough to demonstrate. Create a small script and make it executable and group-runnable:
$ printf "#!/bin/sh\ncat /srv/handbook-project/report.txt\n" > /srv/handbook-project/run-report.sh
$ chmod 750 /srv/handbook-project/run-report.sh
$ stat -c "%A %a %U %G %n" /srv/handbook-project/run-report.sh
-rwxr-x--- 750 john project /srv/handbook-project/run-report.sh
The printf wrote two lines - a shebang line naming an interpreter, and the
cat command. There are now two ways to run it, and they check different
permissions. Naming an interpreter explicitly never requires the execute
bit, because the interpreter is the program being executed; the script is
only text it reads:
$ sh /srv/handbook-project/run-report.sh
Quarterly report for the project.
Reviewed by sam.
Invoking the script directly makes the kernel execute the file itself, which requires the execute bit - present here for the owner and the group:
As sam:
$ /srv/handbook-project/run-report.sh
Quarterly report for the project.
Reviewed by sam.
Back in john's session, remove the group's execute bit and the direct invocation breaks, while the interpreter route keeps working:
$ chmod 740 /srv/handbook-project/run-report.sh
As sam:
$ /srv/handbook-project/run-report.sh
-bash: line 1: /srv/handbook-project/run-report.sh: Permission denied
The kernel found the file but refused to execute it; the shell reports the refusal with exit status 126, the "found but not executable" status Chapter 3 tabulated. Back in john's session, restore the mode:
$ chmod 750 /srv/handbook-project/run-report.sh
chmod option |
Description |
|---|---|
-R |
Apply recursively to directories and their contents. |
-v |
Print a line for every file changed. |
--reference file |
Copy the mode from file instead of naming one. |
+x, -w, u=rw |
Symbolic forms; operate on named classes instead of setting all nine digits. |
The symbolic forms are the natural companion to the octal ones: chmod g+w
file adds exactly the group write bit without touching the rest, where a
numeric mode always sets all three classes at once. One directory-specific
surprise of the numeric form is important enough to get its own explanation
in the next section.
5.3.2. Directory Permissions and the Sticky Bit
On a directory the same nine bits mean different things, because a directory's contents are its names, not the files behind them:
| Bit | Meaning on a directory |
|---|---|
r |
List the names in the directory. |
w |
Add, remove, or rename entries. |
x |
Traverse: reach entries inside, and stat them by name. |
r without x |
See that names exist, but not open or stat them. |
x without r |
Use a known name, but not list the directory. |
Write access to a directory is thus much stronger than write access to a file: it is the right to unlink other users' files, because removing a name only touches the directory, regardless of who owns the file. The shared workspace grants it deliberately, but a subdirectory can revoke it for itself:
$ mkdir /srv/handbook-project/private
$ chmod 00700 /srv/handbook-project/private
$ stat -c "%A %a %U %G %n" /srv/handbook-project/private
drwx------ 700 john project /srv/handbook-project/private
The five-digit form 00700 is deliberate, and the reason is a GNU chmod
behavior that will otherwise surprise you: on a directory, a three-digit
numeric mode leaves the setgid bit untouched - it neither sets nor clears
it, even though the three digits say nothing about it. This directory was
created inside the setgid /srv/handbook-project, so it inherited both the
group and the setgid bit; a plain chmod 700 would have left the mode at
2700, whose symbolic form differs from drwx------ only by an
easy-to-miss capital S in the group-execute position - and new
subdirectories would still inherit the group. Naming the special bits with
five digits (or symbolically with g-s) clears them explicitly. The group
is still project, but its class has no bits, so:
As sam:
$ ls /srv/handbook-project/private
ls: cannot open directory '/srv/handbook-project/private': Permission denied
Sam cannot even list it: with no x for the group, traversal is barred -
namei -l in Section 5.1.3 showed the same mechanism from the outside of
john's home.
The sticky bit answers the opposite problem: a directory everyone may write to, in which no one may delete anyone else's work. Back in john's session, create one inside the shared area:
$ mkdir /srv/handbook-project/dropbox
$ chmod 01777 /srv/handbook-project/dropbox
$ stat -c "%A %a %U %G %n" /srv/handbook-project/dropbox
drwxrwxrwt 1777 john project /srv/handbook-project/dropbox
Mode 1777 is rwxrwxrwx plus the sticky bit, shown as t in the
last position. Its effect on the kernel check is one added rule: inside a
sticky directory, a process may remove or rename an entry only if it owns
the entry or owns the directory - the ordinary group-write right to
delete other people's names is withdrawn. Put it to the test; john leaves a
note:
$ printf "john owns this note.\n" > /srv/handbook-project/dropbox/john-note.txt
As sam:
$ rm /srv/handbook-project/dropbox/john-note.txt
rm: cannot remove '/srv/handbook-project/dropbox/john-note.txt': Operation not permitted
The mode says sam may write to the directory, but the sticky rule overrides that for entries she does not own. Sam's own files are unaffected:
$ printf "sam owns this note.\n" > /srv/handbook-project/dropbox/sam-note.txt
$ rm /srv/handbook-project/dropbox/sam-note.txt
The removal of her own note produced no output: it succeeded. Back in john's session, his note survived the whole exchange:
$ ls /srv/handbook-project/dropbox
john-note.txt
The canonical sticky directory already exists on every Debian system, and
the same mode explains why /tmp works at all:
# ls -ld /tmp
drwxrwxrwt 15 root root 960 Nov 18 13:57 /tmp
World-writable, sticky, and owned by root: everyone may create files there, and the sticky bit is what keeps one user from erasing another's temporary work.
5.3.3. umask, Setgid Directories, and Collaborative Workspaces
New files do not get to choose their own permissions; the creating process proposes a mode and the kernel subtracts the process's umask - a mask of bits to remove - before creating the file. Check john's session mask:
$ umask
0002
Many references describe a default of 0022; this session reports 0002,
and the difference is a deliberate Debian arrangement rather than an
accident of the shell. The session mask is set by pam_umask, which the
installed PAM session stack includes; with USERGROUPS_ENAB yes in
/etc/login.defs - and no UMASK entry in that file on Debian 13 - it
clears the mask's group-write bit for accounts whose primary group is their
user-private group. John qualifies, so his sessions start at 0002: files
he creates are group-writable from birth, and the group is john's alone -
private by ownership, collaborative by convention the moment a shared group
owns the file instead. A session may override the mask at any time; the
change lasts until that shell exits:
$ umask 027
$ touch /home/john/debian-handbook/accounts-permissions/umask-file.txt
$ stat -c "%a %n" /home/john/debian-handbook/accounts-permissions/umask-file.txt
640 /home/john/debian-handbook/accounts-permissions/umask-file.txt
Creation modes start from 666 for files and 777 for directories, and
the umask bits are removed from them: 666 minus 027 leaves 640,
exactly what stat shows. A directory in the same session:
$ mkdir /home/john/debian-handbook/accounts-permissions/umask-dir
$ stat -c "%a %n" /home/john/debian-handbook/accounts-permissions/umask-dir
750 /home/john/debian-handbook/accounts-permissions/umask-dir
777 minus 027 leaves 750. The common masks differ in exactly what
they withhold from the group and other classes:
| umask | New file | New directory | Typical intent |
|---|---|---|---|
0002 |
664 |
775 |
User-private groups; group-writable by default. |
0022 |
644 |
755 |
Traditional default: group and other read only. |
0027 |
640 |
750 |
Group may read; other gets nothing. |
0077 |
600 |
700 |
Fully private; used for keys and secrets. |
A umask set this way is per-session. A persistent personal default belongs
in ~/.profile, which the login shell runs before the first prompt
(Chapter 3 covers shell startup); a system-wide
change belongs in the PAM session configuration, not in scattered rc files.
The second half of collaboration is making sure files created by different people end up in the shared group. The setgid bit on a directory does exactly that. The workspace root already carries it; build a second level to watch it work:
$ mkdir /srv/handbook-project/shared
# chgrp project /srv/handbook-project/shared
$ chmod 2775 /srv/handbook-project/shared
$ stat -c "%A %a %U %G %n" /srv/handbook-project/shared
drwxrwsr-x 2775 john project /srv/handbook-project/shared
The s in the group-execute position is the setgid bit. Its rule: a file
created inside the directory takes the directory's group, not the
creator's primary group, and a subdirectory additionally inherits the
setgid bit itself. Create from both identities:
$ touch /srv/handbook-project/shared/john-shared.txt
$ stat -c "%G %n" /srv/handbook-project/shared/john-shared.txt
project /srv/handbook-project/shared/john-shared.txt
As sam:
$ touch /srv/handbook-project/shared/sam-shared.txt
$ stat -c "%G %n" /srv/handbook-project/shared/sam-shared.txt
project /srv/handbook-project/shared/sam-shared.txt
Both files landed in project - sam's primary group is sam, but the
directory overrode it. Figure 5.3 shows the arrangement the team now has:
one group, two creators, and no per-file chgrp anywhere.
Figure 5.3. Files created in the setgid directory inherit its group; new subdirectories inherit the bit and the group; the session umask leaves group write in place.
To audit where the arrangement is in force, search for the bit itself:
$ find /srv/handbook-project -maxdepth 2 -type d -perm -2000
/srv/handbook-project
/srv/handbook-project/shared
-perm -2000 matches directories with the setgid bit set, whatever their
other bits (find syntax from Chapter 3). The
workspace root and its subdirectory both qualify.
The setgid bit has a sibling on files: setuid, shown as s in the
owner-execute position, which runs the program with the file's owner's
identity rather than the caller's. Debian uses it sparingly, for programs
that must act as root on behalf of ordinary users:
# ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 118168 Apr 19 2025 /usr/bin/passwd
/usr/bin/passwd is setuid root - that is how an ordinary user can change
their own password (Section 5.5.1) without being able to read the shadow
file the new hash lands in. Setuid is powerful and therefore a standing
concern: every setuid root program is a privilege boundary, so inventories
of such binaries belong to security auditing, covered in
Chapter 26.
Three mechanisms now cover collaboration, and they compose:
| Mechanism | What it provides | New files receive | Caveats |
|---|---|---|---|
User-private groups + umask 0002 |
Group-writable defaults with a private group | The creator's private group | Shared only inside setgid directories |
| Setgid directory | One group for a whole tree | The directory's group; subdirectories inherit the bit | Membership governs access; others fall to the other bits |
| Default ACL (next section) | A fixed permission template per directory | The directory's ACL entries | Mask interacts with the creation mode; less visible in ls |
5.3.4. POSIX ACLs and Effective Permissions
The nine bits have one structural limit: a file has exactly one owning
group. POSIX access control lists extend the model with named entries -
additional users and groups on a single file - plus a mask that governs
how much of the extended rights actually apply. The tools come from the
acl package; if it is not installed, an administrator can add it with
apt install acl (Chapter 6 covers package
installation in depth). On ext4 - and on the other local filesystems covered
in Chapter 12 - the entries are stored as file metadata;
network filesystems have their own ACL models, noted at the end of the
section.
Start from a deliberately restrictive base: the report is readable by
project and writable by no group member:
$ chmod 640 /srv/handbook-project/report.txt
Grant sam write access by name, without touching the group:
# setfacl -m u:sam:rw /srv/handbook-project/report.txt
$ getfacl -c /srv/handbook-project/report.txt
user::rw-
user:sam:rw-
group::r--
mask::rw-
other::---
setfacl -m modifies entries; u:sam:rw names a user and the rights to
grant. getfacl -c prints the entries without the file header (it also
prints Removing leading '/' from absolute path names on standard error
when given an absolute path - a notice, not an error; the listing is
correct). The entries read: the owner keeps rw-; sam has a named entry
with rw-; the owning group still has only r--; the mask (explained
below) is rw-; and other has nothing. ls -l marks such files with a +
after the mode (-rw-rw----+), the visible hint that ACL entries exist.
The named entry is exactly what sam needs to append:
As sam:
$ printf "Named ACL write.\n" >> /srv/handbook-project/report.txt
The append produced no output, meaning it succeeded; confirm from john's session:
$ tail -n 1 /srv/handbook-project/report.txt
Named ACL write.
Remove the named entry and the access reverts:
# setfacl -x u:sam /srv/handbook-project/report.txt
$ getfacl -c /srv/handbook-project/report.txt
user::rw-
group::r--
mask::r--
other::---
Sam's entry is gone, and the mask dropped back to r-- - setfacl had
raised it to cover the rights of the entries it created. That behavior is
the door to the model's most misunderstood rule.
The mask caps the rights of the group class: the owning group and
every named group entry. A named user entry is not capped by the mask -
sam's write above worked even while the group had r-- - but group entries
are. Demonstrate by granting project write access and then pinning the
mask below it:
# setfacl -m g:project:rw /srv/handbook-project/report.txt
# setfacl -m m::r /srv/handbook-project/report.txt
$ getfacl -c /srv/handbook-project/report.txt
user::rw-
group::r--
group:project:rw- #effective:r--
mask::r--
other::---
The listing says it outright: the entry grants rw-, but its effective
rights are r--, because the mask r-- filters the group class. The
append now fails:
As sam:
$ printf "Masked write.\n" >> /srv/handbook-project/report.txt
-bash: line 1: /srv/handbook-project/report.txt: Permission denied
Raise the mask and the same entry works again:
# setfacl -m m::rw /srv/handbook-project/report.txt
As sam:
$ printf "Masked write restored.\n" >> /srv/handbook-project/report.txt
The command was silent - the append succeeded this time. Two practical
consequences follow. First, getfacl's #effective: comments are the truth
about access; an entry without them applies as written. Second, on a file
with extended entries, chmod's group digits edit the mask, not the
owning-group entry - which is why the group rights can seem to "stick" at
odd values after a chmod. Remove all extended entries to return to the
plain model:
# setfacl -b /srv/handbook-project/report.txt
$ getfacl -c /srv/handbook-project/report.txt
user::rw-
group::r--
other::---
-b strips every named entry and the mask; the base entries are the
original mode bits. Figure 5.4 collects the classes and the mask's
position.
Figure 5.4. Conceptual ACL access classes: the owner and named-user entries apply directly; the group class is the union of its entries, filtered by the mask; other applies when nothing matched.
setfacl option |
Description |
|---|---|
-m entries |
Modify or add entries (u:name:perms, g:name:perms, m::perms). |
-x entries |
Remove named entries. |
-b |
Remove all extended entries (keep the base mode). |
-d -m entries |
Modify the default ACL of a directory. |
-k |
Remove the default ACL. |
--set-file=file |
Replace the whole ACL from a listing, - for standard input. |
getfacl option |
Description |
|---|---|
-c |
Omit the comment header; entries only. |
-p |
Keep the leading slash in printed paths. |
--skip-base |
Show only extended entries. |
The one piece still missing is inheritance: a default ACL on a directory is a template that new entries copy. Apply one to the setgid subdirectory:
# setfacl -d -m g:project:rwx /srv/handbook-project/shared
$ getfacl -c /srv/handbook-project/shared
user::rwx
group::rwx
other::r-x
default:user::rwx
default:group::rwx
default:group:project:rwx
default:mask::rwx
default:other::r-x
The default: entries are not access rights for the directory; they are
what new files and subdirectories inside will receive. Create one:
$ touch /srv/handbook-project/shared/default-child.txt
$ getfacl -c /srv/handbook-project/shared/default-child.txt
user::rw-
group::rwx #effective:rw-
group:project:rwx #effective:rw-
mask::rw-
other::r--
The child inherited the group entries - and the template was intersected
with the file's creation mode (touch asks for rw- on a file), so the
effective rights are rw- rather than rwx. When a default ACL exists, it
replaces the umask as the shaping rule: the entries are capped by the
creation mode rather than carved down by the mask. Sam can write:
As sam:
$ printf "Inherited ACL write.\n" >> /srv/handbook-project/shared/default-child.txt
Again silent, again successful. Default ACLs and setgid directories solve overlapping problems - the setgid bit fixes the group, the default ACL fixes the rights - and using both is common. Remove the template when the demonstration is over:
# setfacl -k /srv/handbook-project/shared
$ getfacl -c /srv/handbook-project/shared
user::rwx
group::rwx
other::r-x
The entries that already exist on files are untouched - -k removes the
template, not the copies. To archive and restore a whole tree's ACLs,
getfacl -R piped to setfacl --set-file=- reproduces them; the same
round-trip pattern applies to backups of the workspace.
ACL support is a filesystem property. Ext4 and the other filesystems in Chapter 12 store POSIX ACLs natively; NFS and SMB exports translate or replace them with their own models, and Chapter 21 covers what actually crosses the wire. On a local ext4 tree, everything in this section works as shown.