Overview and What You Will Learn
Every Linux file has an owner, a group and a set of permission bits, and together they decide who can read, write or execute it. In this lab you will read permissions, change them with chmod and chown, control defaults with umask, grant extra access with ACLs, use the special bits, and apply safe permission patterns to a web root, an application directory, SSH keys and logs. It assumes a Debian or Ubuntu test server and a sudo-capable user.
- Reading the permission string and converting it to octal
- Changing modes with symbolic and octal
chmod - Changing owners and groups with
chown - Controlling default permissions with
umask - Granting per-user access with ACLs
- Using setuid, setgid and the sticky bit
- Diagnosing "Permission denied" along a whole path
Why This Matters in Production
Wrong permissions break deployments and weaken security at the same time. A script fails because its process user cannot write to a log directory, a web server returns 403 because the document root is unreadable, and SSH refuses a key file that is too open. Imagine a payments team at a company like Razorpay that makes every file in the payment service directory owned by one service user, writable by nobody else. If the web server or a CI runner is compromised, it still cannot change the payment code.
Core Principles
Linux checks permissions in a fixed order and stops at the first match. If you are the file's owner, only the owner bits apply. If not, and you are in the file's group, only the group bits apply. Otherwise the other bits apply, so an owner with fewer rights than "other" can be denied access that everyone else has.
the owner?}:::q B -->|yes| O[Apply owner bits]:::ok B -->|no| C{In the file's
group?}:::q C -->|yes| G[Apply group bits]:::ok C -->|no| X[Apply other bits]:::warn classDef src fill:#DBEAFE,color:#1E293B classDef q fill:#FFEDD5,color:#1E293B classDef ok fill:#10B981,color:#FFFFFF classDef warn fill:#F97316,color:#FFFFFF
The ten-character string from ls -l has a file type followed by three triplets for owner, group and other. For -rwxr-xr--, the first character is the type (- file, d directory, l symlink), and the triplets are rwx, r-x and r--. Each permission has a value, and you add them up per triplet:
| Permission | Letter | Value |
|---|---|---|
| Read | r | 4 |
| Write | w | 2 |
| Execute | x | 1 |
So rwx r-x r-- is 7, 5 and 4, which is octal 754. These are the modes you will use most:
| Mode | String | Typical use |
|---|---|---|
| 644 | -rw-r--r-- |
Config files, HTML, static assets |
| 640 | -rw-r----- |
Sensitive config readable by one group |
| 600 | -rw------- |
SSH private keys and secrets |
| 755 | -rwxr-xr-x |
Scripts, binaries, public directories |
| 750 | -rwxr-x--- |
Restricted scripts and app directories |
| 700 | -rwx------ |
The ~/.ssh directory |
Never use 777 in production, since it lets every user on the machine change the file.
Execute means something different on a directory. It means "can enter and traverse", so without it you cannot cd into the directory or reach anything inside, even files you could otherwise read. Read on a directory lets you list names, and write lets you create and delete entries.
Three special bits change how files and directories behave:
| Bit | Octal | On a file | On a directory |
|---|---|---|---|
| setuid | 4000 | Runs as the file's owner | No effect |
| setgid | 2000 | Runs with the file's group | New files inherit the directory's group |
| sticky | 1000 | No effect | Users can delete only their own files |
Examples are /usr/bin/passwd with setuid, which lets users change their own password, and /tmp with the sticky bit. In ls -l the bits show as s or t in the execute position, and as capital S or T when the execute bit itself is missing. Linux ignores setuid on shell scripts, so it works only on compiled binaries.
Detailed Step-by-Step Practical Lab
Step 1 - Create a Lab Directory and Read Permissions
mkdir -p /tmp/permissions-lab/subdir /tmp/permissions-lab/app/config /tmp/permissions-lab/logscd /tmp/permissions-labtouch file1.txt file2.sh ls -la # drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 14 10:00 subdir # Detailed view of one file, with the octal modestat -c '%a %U:%G %n' file1.txtStep 2 - Change Permissions with chmod
Symbolic mode uses u (owner), g (group), o (other) and a (all), with + to add, - to remove and = to set exactly:
chmod u+x file2.shls -l file2.sh # -rwxr--r-- chmod go-w file1.txtchmod 644 file1.txt # -rw-r--r--chmod 755 file2.sh # -rwxr-xr-xchmod 700 subdir # drwx------Recursive changes need care. Using one octal mode for a whole tree is a classic mistake, because directories need the execute bit and plain files usually do not:
chmod -R 644 app # wrong: removes x from the directories toocd app # Permission denied # Right: capital X adds execute only to directories# (and to files that are already executable)chmod -R u=rwX,go=rX appcd app && cd ..Step 3 - Change Ownership with chown
sudo useradd -r -s /usr/sbin/nologin app-service sudo chown app-service file1.txt # change ownersudo chown app-service:app-service file2.sh # owner and groupsudo chown :app-service subdir # group only (same as chgrp) ls -l file1.txt file2.sh# -rw-r--r-- 1 app-service ubuntu 0 Oct 14 10:00 file1.txt# -rwxr-xr-x 1 app-service app-service 0 Oct 14 10:00 file2.sh # You are no longer the owner, so the owner bits no longer apply to youecho "test" >> file1.txt # Permission denied # Recursive ownership change for a directory treesudo chown -R app-service:app-service appStep 4 - Control Defaults with umask
The umask clears bits from the starting mode of new objects, which is 666 for files and 777 for directories. Think of it as a mask, not subtraction. A umask of 022 gives 644 files and 755 directories, 027 gives 640 and 750, and 077 gives 600 and 700.
umask # 0022 # Test a stricter umask in a subshell so your own shell is unchanged(umask 027; touch f-027; mkdir d-027; ls -ld f-027 d-027)# -rw-r----- 1 ubuntu ubuntu 0 ... f-027# drwxr-x--- 2 ubuntu ubuntu 4096 ... d-027A umask in /etc/profile.d/ would affect every login shell for every user, and service accounts that never log in would not read it. For a systemd service, set it in the unit instead:
[Service]UMask=0027Step 5 - Grant Per-User Access with ACLs
ACLs let you give a specific user or group access without changing the owner or group. They need the acl package and a filesystem that supports them, which ext4 and XFS do by default:
sudo apt install aclsudo useradd -m deploy touch acl-demo.txtgetfacl acl-demo.txt # Give the deploy user read and write accesssetfacl -m u:deploy:rw acl-demo.txtgetfacl acl-demo.txt# user::rw-# user:deploy:rw-# group::r--# mask::rw-# other::r-- ls -l acl-demo.txt # a trailing + marks a file with an ACLThe mask line caps what named users and groups can receive, and chmod on the group bits changes the mask. If an ACL seems ignored, check getfacl for #effective: notes. Set a default ACL on a directory so new files inherit it, then remove entries:
setfacl -d -m u:deploy:rw logstouch logs/new.loggetfacl logs/new.log setfacl -x u:deploy acl-demo.txt # remove one entrysetfacl -b acl-demo.txt # remove all ACLsTip
cp -aandtar --aclspreserve ACLs, but a plaincportarcan silently drop them, so check backups and deploy tools before relying on ACLs.
Step 6 - Use the Special Bits
A setgid directory makes shared folders work, because new files inherit the directory's group instead of the creator's primary group:
sudo groupadd devsmkdir sharedsudo chgrp devs sharedchmod 2775 shared touch shared/report.txtls -ld shared # drwxrwsr-x ... devsls -l shared/report.txt # -rw-r--r-- ... devs ls -ld /tmp # drwxrwxrwt: the t is the sticky bitls -l /usr/bin/passwd # -rwsr-xr-x: the s is setuid # Audit setuid and setgid files, staying on one filesystemsudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/nullSecuritySetuid and setgid binaries are a common privilege escalation route. Audit them regularly, and investigate any you did not expect.
Step 7 - Apply Production Permission Patterns
Run these on a test server. They show the shape of each pattern, so adjust the names to your system.
Web root. The deploy user owns the files and the web server can only read them, so a compromised web process cannot rewrite the site:
sudo chown -R deploy:www-data /var/www/htmlsudo chmod -R u=rwX,g=rX,o= /var/www/htmlApplication directory and config files:
sudo chown -R app-service:app-service /opt/myappsudo chmod 750 /opt/myappsudo chmod 640 /opt/myapp/config/*.confSSH. OpenSSH refuses keys when the files or the home directory are too open:
mkdir -p ~/.sshchmod 700 ~/.sshtouch ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys ~/.ssh/id_ed25519chmod 644 ~/.ssh/id_ed25519.pubchmod go-w ~ # the home directory must not be group or world writableLogs. The adm group reads logs on Debian and Ubuntu, and the setgid bit keeps new log files in that group:
sudo chown app-service:adm /var/log/myappsudo chmod 2750 /var/log/myappStep 8 - Clean Up
cd ~rm -rf /tmp/permissions-labsudo userdel app-servicesudo userdel -r deploysudo groupdel devsProduction Best Practices & Common Pitfalls
Common MistakeRunning
chmod 777to make a permission error go away. It fixes the symptom by giving every user write access. Find which user needs access, then set the minimum owner, group and mode.
TipWhen diagnosing a permission problem, check every directory in the path, not just the file. Each directory needs execute for the user, so a
644file inside a700directory is unreachable to everyone but the directory's owner. The commandnamei -l /full/path/to/fileshows the permissions of every component at once.
RememberOctal and symbolic modes do the same thing. Octal is quicker to type and symbolic is clearer to read, and you will see both in production scripts.
| Mistake | Problem | Fix |
|---|---|---|
chmod -R 644 on a directory tree |
Strips execute from directories, so nobody can enter them | chmod -R u=rwX,go=rX |
| SSH private key too open | SSH refuses to use it | chmod 600 ~/.ssh/id_ed25519 |
Forgetting -R on a tree |
Only the top-level item changes | Use -R, and check with ls -lR |
| Web server owns its own files | A compromise can rewrite the site | Owner is the deploy user, web server reads via group |
Setting umask in /etc/profile.d for a service |
Services never read it | UMask= in the systemd unit |
| Assuming an ACL grants access | The mask can cut it back | Read getfacl and its effective rights |
- Look beyond the mode bits. If the permissions look right but access still fails, check SELinux or AppArmor (
ls -Z,aa-status), anoexecor read-only mount, and the immutable flag (lsattr). - Prefer groups over wide modes. Create a group for the people or services that need access, rather than opening the file to everyone.
- Keep ownership minimal. Files a service only reads should not be owned by that service.
Quick Reference & Troubleshooting Commands
| Task | Command |
|---|---|
| View permissions | ls -la |
| Show mode as octal | stat -c '%a %U:%G %n' FILE |
| Check permissions along a path | namei -l /full/path/to/file |
| Set a mode | chmod 640 FILE or chmod u=rw,g=r,o= FILE |
| Recursive safe mode | chmod -R u=rwX,go=rX DIR |
| Change owner and group | sudo chown USER:GROUP FILE |
| Show the current umask | umask |
| Show ACLs | getfacl FILE |
| Add or remove an ACL entry | setfacl -m u:USER:rw FILE and setfacl -x u:USER FILE |
| Find setuid files | sudo find / -xdev -type f -perm -4000 |
| Symptom | First command | What to look for |
|---|---|---|
| Permission denied writing | namei -l PATH |
Write bit for the user or group, and execute on every directory |
SSH ignores authorized_keys |
ls -la ~/.ssh |
~/.ssh is 700, keys and authorized_keys are 600 |
| Web server returns 403 | namei -l /var/www/html/index.html |
The web user can read the file and traverse every directory |
| Script works by hand, fails in cron | ls -l SCRIPT |
Execute bit for the cron user, and path permissions |
| App cannot read its config | ls -l CONFIG |
Owner or group matches the app user |
| Modes look right but access fails | ls -Z, lsattr, mount |
SELinux context, immutable flag, noexec mount |