Lesson 06 beginner

File Permissions and Ownership

How Linux decides who may read, write or run a file, how to set modes with chmod, owners with chown, defaults with umask, shared folders with setgid and exceptions with ACLs, and how to debug "Permission denied" fast.

Every file on a Linux server answers three questions: who owns it, which group it belongs to, and what everyone is allowed to do with it. Get those answers right and the Orbit API can read its secrets while nobody else can, developers can share a folder without stepping on each other, and a compromised process cannot rewrite your code. Get them wrong and you see either “Permission denied” at 2 a.m. or, worse, no error at all while an attacker edits your files.

In this lesson you learn to read permission strings, change them in both notations, set owners and defaults, use the special bits and ACLs for the cases plain modes cannot express, and lay out Orbit’s directories properly. Use the orbit-lab VM: you will create a few users and groups that later lessons reuse.

If you skipped ahead

This lesson needs the Orbit users and directories. Lesson 7 explains these commands; for now just run them:

sudo useradd --system --user-group --home-dir /srv/orbit --shell /usr/sbin/nologin orbit
sudo groupadd orbit-devs
sudo useradd -m -s /bin/bash -G orbit-devs maya
sudo useradd -m -s /bin/bash -G orbit-devs leo
sudo mkdir -p /srv/orbit/api /srv/orbit/public /srv/orbit/releases /srv/orbit/shared /etc/orbit /var/lib/orbit /var/log/orbit
echo 'DATABASE_URL=postgres://orbit:s3cret@orbit-db-01/orbit' | sudo tee /etc/orbit/orbit.env > /dev/null
echo '<h1>Orbit</h1>' | sudo tee /srv/orbit/public/index.html > /dev/null

Reading ls -l

ls -l shows permissions for files, and ls -ld for a directory itself rather than its contents:

$ ls -l /etc/orbit/orbit.env /usr/bin/passwd
-rw-r----- 1 root orbit    52 Nov  3 09:20 /etc/orbit/orbit.env
-rwsr-xr-x 1 root root  64152 Apr  8  2026 /usr/bin/passwd
$ ls -ld /tmp /srv/orbit/shared
drwxrwxrwt 12 root root       4096 Nov  3 09:30 /tmp
drwxrwsr-x  2 root orbit-devs 4096 Nov  3 09:25 /srv/orbit/shared

Take -rw-r----- 1 root orbit apart:

PartValueMeaning
Type-Regular file (d directory, l symlink, s socket, c/b devices)
Owner bitsrw-The owner, root, may read and write
Group bitsr--Members of group orbit may read
Other bits---Everyone else gets nothing
Owner, grouproot orbitWho “owner” and “group” refer to

A tenth character sometimes follows the bits: + means an ACL is set (covered below), and on Red Hat systems a . means an SELinux label.

How the check works

The kernel picks one class and stops: if you are the owner, owner bits apply; otherwise, if you are in the file’s group, group bits apply; otherwise, other bits apply. It does not add them up. A file with mode -r--rw---- denies writes to its owner even though the group may write. root skips the read and write checks entirely, which is why “it works with sudo” proves nothing about the real problem.

Read, write and execute: files versus directories

The same three letters mean different things on a directory, and most permission puzzles come from this table:

BitOn a fileOn a directory
rRead the contentsList the names inside (ls)
wChange the contentsCreate, delete and rename entries inside (needs x too)
xRun it as a programEnter it and reach anything inside (cd, open files by path)

Two consequences surprise everyone once:

  • Every directory on the path needs execute. To open /a/b/file you need x on /, /a and /a/b, plus r on the file. One directory without x on the path blocks everything below it.
  • Deleting a file is a directory operation. You need w and x on the directory, not any permission on the file. A read-only file inside a directory you can write is still yours to delete (unless the sticky bit says otherwise).

Changing modes with chmod

Symbolic mode

Symbolic mode says who (u user/owner, g group, o other, a all), how (+ add, - remove, = set exactly) and what (r, w, x):

$ chmod u+x deploy.sh
$ chmod g-w,o= notes.txt
$ chmod a+r README.md
$ sudo chmod -R u=rwX,g=rX,o= /var/lib/orbit

The capital X in the last line adds execute only to directories (and files that are already executable by someone). It is the right way to fix a whole tree: directories become enterable, plain files do not suddenly become programs.

Octal mode

Each class is a sum of read (4), write (2) and execute (1), so three digits describe owner, group and other:

ModeStringTypical use
600rw-------Private keys, personal secrets
640rw-r-----Config read by a service group (orbit.env)
644rw-r--r--Normal files, public keys
700rwx------Private directories such as ~/.ssh
750rwxr-x---Directories for a team or service only
755rwxr-xr-xPrograms and normal directories

Symbolic mode is best for small changes (“add execute”), octal for setting a complete, known state. stat shows both at once:

$ stat -c '%a %A %U:%G %n' /etc/orbit/orbit.env
640 -rw-r----- root:orbit /etc/orbit/orbit.env

Changing owners with chown and chgrp

chown user:group path sets both, chown user path only the owner and chown :group path (or chgrp group path) only the group. -R recurses:

$ sudo chown -R orbit:orbit /srv/orbit /var/lib/orbit /var/log/orbit
$ sudo chown root:orbit /etc/orbit /etc/orbit/orbit.env
$ sudo chgrp orbit-devs /srv/orbit/shared

Only root can give a file to another user; otherwise anyone could dodge disk quotas or plant files in your name. A normal owner may change the group, but only to a group they belong to.

Tip: On a server, ownership is usually the real fix. If the API cannot write /var/lib/orbit, the answer is chown orbit:orbit, not more permission bits for everybody.

Default permissions with umask

New files start from 666 and new directories from 777, and the umask removes bits from that. It is a mask, not a subtraction: a bit set in the umask is switched off.

umaskNew fileNew directoryTypical use
022644755Classic default for root and services
002664775Teams sharing files through groups
027640750Services whose files nobody else should read
077600700Paranoid personal default
$ umask
0002
$ umask -S
u=rwx,g=rwx,o=rx

On Ubuntu, login sessions of normal users usually report 0002, because each user has a private group of their own; root and system services usually get 0022. A umask set with the umask command lasts for the current shell. For a service, set it in its systemd unit with UMask=0027 (lesson 9) so files the Orbit API creates are never world-readable.

Special bits: setuid, setgid and sticky

A fourth octal digit, written in front, holds three special bits:

BitOctalShows asOn a fileOn a directory
setuid4000s in owner xRuns as the file’s ownerNo effect on Linux
setgid2000s in group xRuns with the file’s groupNew entries inherit the directory’s group
sticky1000t in other xNo effectOnly the owner of an entry may delete or rename it

setuid is how /usr/bin/passwd (mode 4755) lets you change your own password in /etc/shadow, a file only root can write. Every setuid-root program is a potential privilege escalation, so audit them with sudo find / -xdev -perm -4000 -type f. Linux ignores setuid on scripts, so chmod u+s deploy.sh does nothing useful.

The sticky bit is why /tmp is 1777: everyone may create files there, but nobody can delete someone else’s.

A shared folder for orbit-devs

Maya and Leo both drop files into /srv/orbit/shared. Without help, each new file belongs to its creator’s own group (maya or leo), and the other person cannot edit it. Setgid on the directory fixes the group:

$ sudo chown root:orbit-devs /srv/orbit/shared
$ sudo chmod 2775 /srv/orbit/shared

When Maya, logged in as herself, runs touch /srv/orbit/shared/runbook.md, ls -l shows:

$ ls -l /srv/orbit/shared
-rw-rw-r-- 1 maya orbit-devs 0 Nov  3 09:41 runbook.md

The file now belongs to group orbit-devs, and it is group-writable because Maya’s umask is 002. With a 022 umask it would be 644 and Leo still could not edit it. Setgid fixes the group, not the mode; a default ACL (next section) fixes the mode as well. Note that sudo -u maya touch would not show this: sudo applies its own 022 umask.

ACLs for one-off exceptions

Plain modes give you one owner and one group. When Leo, who is not in group orbit, needs to read the API logs, adding him to orbit would also give him the database password. An access control list grants exactly one extra permission instead:

$ sudo setfacl -m u:leo:rX /var/log/orbit
$ sudo setfacl -m u:leo:r /var/log/orbit/api.log
$ getfacl -c /var/log/orbit/api.log
user::rw-
user:leo:r--
group::r--
mask::r--
other::---
$ ls -l /var/log/orbit/api.log
-rw-r-----+ 1 orbit orbit 2210 Nov  3 09:44 /var/log/orbit/api.log
  • -m modifies the ACL, -x u:leo removes one entry and -b removes all of them.
  • The mask caps every named entry and the group; with an ACL present, the group bits in ls -l show the mask.
  • -d sets a default ACL on a directory, inherited by new files: sudo setfacl -d -m g:orbit-devs:rwX /srv/orbit/shared makes every new file in the shared folder group-writable, whatever the creator’s umask.

ACL tools come from the acl package (sudo apt install acl if getfacl is missing). Use them for exceptions; if you find yourself writing ACLs for half the team, create a group instead.

Orbit’s permission layout

PathOwner:groupModeWhy
/srv/orbit and its codeorbit:orbit755 dirs, 644 filesCode is not secret; the service reads it
/srv/orbit/sharedroot:orbit-devs2775 + default ACLTeam folder, group inherited
/etc/orbitroot:orbit750Only root and the service can enter
/etc/orbit/orbit.envroot:orbit640Service reads secrets, cannot change them
/var/lib/orbit, /var/log/orbitorbit:orbit750Data and logs, private to the service
/usr/local/bin/orbit-backup.shroot:root755Only root may change what the backup runs

Notice that orbit.env belongs to root: the API can read its secrets but a bug in the API cannot rewrite them. Stricter teams make the code root-owned for the same reason.

Troubleshooting “Permission denied”

Maya runs tail /var/log/orbit/api.log and gets “Permission denied”. Do not reach for chmod; ask the system which step fails. namei -l prints every component of the path with its mode and owner:

$ namei -l /var/log/orbit/api.log
f: /var/log/orbit/api.log
drwxr-xr-x root  root   /
drwxr-xr-x root  root   var
drwxrwxr-x root  syslog log
drwxr-x--- orbit orbit  orbit
-rw-r----- orbit orbit  api.log

/var/log/orbit has no permissions for other, and Maya is neither orbit nor in its group, so she cannot even enter it. Work through this checklist:

  1. Run id as the user: which groups are they really in? New group memberships need a new login.
  2. Run namei -l on the full path: every directory needs x, the file needs r (or w).
  3. Reproduce as the real user, not as yourself: sudo -u orbit cat /etc/orbit/orbit.env.
  4. Look for a + in ls -l and run getfacl.
  5. Still stuck? Check lsattr (the immutable i flag blocks even root), a noexec or ro mount in findmnt, and SELinux or AppArmor.

SSH keys care about permissions

OpenSSH refuses to work with keys that others could read or replace. On your laptop, ssh stops with “WARNING: UNPROTECTED PRIVATE KEY FILE!” if your private key is readable by others. On the server, sshd silently ignores authorized_keys when the home directory, ~/.ssh or the file is writable by group or others, and logs “bad ownership or modes”. The safe set (lesson 16 covers keys in depth):

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/id_ed25519.pub

SELinux and AppArmor: the second lock

Everything so far is discretionary access control: owners decide. Mandatory access control adds a second check that the owner cannot override. Ubuntu uses AppArmor (profiles per program; sudo aa-status lists them), and the Red Hat family uses SELinux, which labels every file and process. Both must say yes.

On orbit-reports (Rocky Linux 10), a file with perfect modes can still be denied because it carries the wrong label, often after mv from a home directory. getenforce shows the mode, ls -Z the labels, sudo restorecon -Rv /srv/orbit restores the default labels and sudo ausearch -m avc -ts recent shows recent denials. Fix the label; do not disable SELinux.

Red Hat and Alpine: chmod, chown, umask, setfacl and namei work the same on Rocky and Alma; expect the . after the mode string and SELinux checks. Alpine’s BusyBox versions support the same modes; install acl with apk add acl for ACL tools.

Practice lab

Orbit is moving to a new server and your job is the file layout. On orbit-lab:

  1. Give /srv/orbit, /var/lib/orbit and /var/log/orbit the owners and modes from the layout table.
  2. Lock down /etc/orbit/orbit.env and prove that orbit can read it while maya cannot.
  3. Make /srv/orbit/shared a team folder where files Maya creates are editable by Leo.
  4. Let Leo read the API logs without joining group orbit.
Show a solution

sudo chown -R orbit:orbit /srv/orbit /var/lib/orbit /var/log/orbit sets the owners, then sudo chmod 750 /var/lib/orbit /var/log/orbit closes the private directories. For the secrets, sudo chown root:orbit /etc/orbit /etc/orbit/orbit.env, sudo chmod 750 /etc/orbit and sudo chmod 640 /etc/orbit/orbit.env; sudo -u orbit cat /etc/orbit/orbit.env prints the file and sudo -u maya cat /etc/orbit/orbit.env fails. For the team folder, sudo chown root:orbit-devs /srv/orbit/shared, sudo chmod 2775 /srv/orbit/shared and sudo setfacl -d -m g:orbit-devs:rwX /srv/orbit/shared; test with sudo -u maya touch and sudo -u leo sh -c 'echo ok >> /srv/orbit/shared/runbook.md' (the default ACL makes it work despite sudo’s umask). For the logs, sudo setfacl -R -m u:leo:rX /var/log/orbit (capital X keeps the directory enterable) plus sudo setfacl -d -m u:leo:rX /var/log/orbit so new log files are covered too, then verify with sudo -u leo tail /var/log/orbit/api.log.

Common mistakes

  • Using chmod -R 777 to make an error go away. Every user and every compromised process can now replace your code; SSH will even refuse keys under it. Find the failing step with namei -l and fix the owner or group instead.
  • Running chmod -R 755 on a tree of files. Every config and log becomes executable. Use u=rwX,g=rX,o= or find -type d and find -type f separately.
  • Testing as root. root bypasses the checks; test with sudo -u orbit.
  • Forgetting execute on a parent directory. The file mode is fine; the path is not.

Exercise

Maya can run ls -l /var/log/orbit/api.log as root and sees mode -rw-r–r–, yet as herself she gets “Permission denied”. /var/log/orbit is drwxr-x— orbit:orbit and she is not in group orbit. What blocks her?

  1. The file needs mode 666 so other users can read it
  2. She has no x permission on /var/log/orbit, so she cannot reach anything inside it, whatever the file’s mode
  3. Log files can only be read with sudo
  4. The file needs the sticky bit
Show answer

Answer: B. Opening a path needs x on every directory along it. Other has — on /var/log/orbit, so the file’s own r bit is never consulted. Fix it with group membership or an ACL, not by loosening the file.

Exercise

Files Maya and Leo create in /srv/orbit/shared keep ending up in their personal groups (maya, leo), so they cannot edit each other’s files. What is the right fix?

  1. Run chmod -R 777 /srv/orbit/shared every night
  2. Make the directory owned by maya so she can fix things
  3. chgrp orbit-devs on the directory, chmod 2775 to set setgid, plus a default ACL (or umask 002) so new files are group-writable
  4. Set the sticky bit so files are shared
Show answer

Answer: C. Setgid on a directory makes new entries inherit its group. Group-writable modes then come from the creators’ umask or, more reliably, a default ACL. The sticky bit does the opposite of sharing: it stops people deleting each other’s files.

Exercise

/srv/orbit/shared is drwxrwsr-x root:orbit-devs. A file report.csv inside it is -r–r–r– owned by root. Leo, a member of orbit-devs, runs rm -f report.csv. What happens?

  1. It fails, because the file is read-only for everyone
  2. It fails, because only root can delete root’s files
  3. The file is deleted, because removing an entry needs write and execute on the directory, not permissions on the file
  4. It fails unless Leo uses sudo
Show answer

Answer: C. Deleting changes the directory, not the file. Leo has w and x on the directory through group orbit-devs. Only the sticky bit (mode 3775 here) would restrict deletion to the file’s owner.

Key takeaways

  • Read ls -l as type, owner bits, group bits, other bits; exactly one class applies to each user.
  • On directories, r lists, w creates and deletes, x enters; every directory on a path needs x.
  • Use octal (640, 750) for known states, symbolic (u+x, g-w) for tweaks, and fix ownership with chown before adding bits.
  • umask sets defaults; setgid directories share a group; the sticky bit protects shared folders like /tmp; ACLs handle one-off exceptions.
  • Debug “Permission denied” with id, namei -l and sudo -u <user>, never with chmod 777.

FAQ

What does chmod 755 mean?

The owner may read, write and execute (7 = 4+2+1), while the group and everyone else may read and execute (5 = 4+1). It is the standard mode for programs and for directories that anyone may enter and list. For a regular data file you usually want 644 instead, which drops the execute bits.

What is the difference between chmod and chown?

chmod changes what each class may do (the mode bits), while chown changes who the owner and group are. Most real fixes on a server are ownership fixes: give the file to the user the service runs as, then keep the mode tight.

Why is chmod 777 a bad idea?

It lets every account and every process on the machine modify or replace the file, including a compromised web app or a curious contractor. Scripts become a place to plant code, and tools like sshd refuse to trust files others can write. It also hides the real cause, which is nearly always the wrong owner, a missing group membership or a directory without x.

How do I see permissions in numeric form?

Run stat -c '%a %n' file for the octal mode, or stat -c '%a %A %U:%G %n' file for octal, symbolic, owner and group together. BusyBox stat on Alpine supports the same format. ls -l only shows the symbolic form.

How do I give one user access to a file without changing its group?

Use an ACL: sudo setfacl -m u:leo:r file grants read access to Leo alone, and getfacl file shows the result. Remember that he also needs x on every directory above it, which you can grant with setfacl -m u:leo:x on those directories.

What does the s in rwsr-xr-x mean?

It is the setuid bit shown in the owner’s execute position. The program runs with the privileges of the file’s owner rather than the user who starts it; /usr/bin/passwd and sudo work this way. A capital S means setuid is set without execute, which is usually an error.

↑ ↓ navigate Enter open Esc close