Visual Permission Calculation
Understanding octal permissions is critical for production work:
| Symbolic | Binary | Octal | Description |
|---|---|---|---|
| rwx | 111 | 7 | Read + Write + Execute |
| rw- | 110 | 6 | Read + Write |
| r-x | 101 | 5 | Read + Execute |
| r-- | 100 | 4 | Read only |
| -wx | 011 | 3 | Write + Execute |
| -w- | 010 | 2 | Write only |
| --x | 001 | 1 | Execute only |
| --- | 000 | 0 | No permissions |
Example: chmod 755 = rwxr-xr-x = Owner: rwx, Group: r-x, Others: r-x
Learning Objectives
- Explain Linux ownership (owner, group, others) and permission bits.
- Read
ls -lpermission strings and map them to octal modes. - Use
chmod,chown, andchgrpsafely in production. - Apply umask, SUID/SGID, sticky bit, and basic ACLs when needed.
- Troubleshoot common “Permission denied” failures.
Prerequisites
Complete Chapter 2 (filesystem navigation and basic commands). You should be comfortable with ls, cd, and editing a file in a terminal.
1. Introduction
Linux is a multi-user operating system. Hundreds of users and services may share the same server, yet every file remains protected through ownership and permissions. Understanding this model is essential before changing permissions or administering production systems.
2. Why Linux Uses Permissions
- Protect confidential data.
- Prevent accidental modifications.
- Allow multiple users to safely share a server.
- Restrict application access.
- Improve overall system security.
User Request
│
▼
Permission Check
│
▼
Allow or Deny
3. Linux Ownership Model
Every file and directory belongs to:
File │ ├── Owner ├── Group └── Others
| Entity | Description |
|---|---|
| Owner | User who owns the file. |
| Group | Collection of users sharing access. |
| Others | Everyone else on the system. |
4. Users, Groups & Others
Owner
Usually the user who created the file.
Group
Provides shared access without changing ownership.
Others
Represents every remaining user.
$ ls -l report.txt
-rw-r----- 1 ashu developers 2480 Jul 9 report.txt
Owner: ashu
Group: developers
5. Permission Types
| Permission | Meaning | Files | Directories |
|---|---|---|---|
| r | Read | View contents | List directory names (also needs x to access entries) |
| w | Write | Modify file | Create/delete/rename entries (requires x on the directory) |
| x | Execute | Run program | Traverse/enter the directory |
Directory write without execute is rarely useful because you cannot enter the directory to use that write access. With the sticky bit set on a shared writable directory, users can still create files, but may delete or rename only files they own (or if they own the directory, or are privileged).
6. Reading Permission Strings
-rwxr-xr--
| Section | Meaning |
|---|---|
| - | Regular file |
| rwx | Owner permissions |
| r-x | Group permissions |
| r-- | Others permissions |
7. How Linux Checks Permissions
User Access Request
│
▼
Is User Owner?
│
Yes ─────► Owner Permission
│
No
▼
Member of Group?
│
Yes ─────► Group Permission
│
No
▼
Others Permission
In the basic owner/group/other model, Linux stops checking as soon as it finds the matching permission category. When ACLs are present (covered later in this chapter), evaluation includes those ACL entries as well — the simple three-class walk is not the whole story.
8. Real Production Example
-rw-r----- root appadmins application.log
- Owner (root) has full read/write access.
- Members of appadmins can read the log.
- Other users have no access.
9. Useful Commands
whoami
id
groups
ls -l
| Command | Purpose |
|---|---|
| whoami | Current logged-in user. |
| id | User ID and groups. |
| groups | List group membership. |
| ls -l | Display ownership and permissions. |
10. Hands-on: Permission Basics
- Run
whoami. - Run
id. - Run
groups. - Create a test file.
- View its permissions using
ls -l. - Identify the owner, group and permission string.
11. Common Mistakes
- Confusing the root user with the root directory.
- Assuming file creator always has permanent ownership.
- Ignoring group permissions.
- Reading only the owner permissions from
ls -l.
12. Basics Summary
Key Takeaways
- Every file has an owner, group and others category.
- Permissions are represented using r, w and x.
- Linux checks owner first, then group, then others.
- Use
ls -l,id,groupsandwhoamito inspect permissions. - Next you will modify permissions with
chmod,chownandchgrp.
13. The chmod Command
chmod changes file or directory permissions.
chmod 755 script.sh
chmod 644 config.conf
chmod 600 secret.txt
# Prefer separate modes for directories vs files (recursive 755 marks every file executable):
find website/ -type d -exec chmod 755 {} +
find website/ -type f -exec chmod 644 {} +
| Option | Description |
|---|---|
| 755 | Owner full access, Group/Others read & execute. |
| 644 | Owner read/write, Group/Others read only. |
| 600 | Owner only. |
| -R | Apply recursively — use carefully; it applies one mode to both files and directories. |
14. Symbolic Mode
chmod u+x script.sh
chmod g-w report.txt
chmod o-r secret.txt
chmod a+r file.txt
| Symbol | Meaning |
|---|---|
| u | User (Owner) |
| g | Group |
| o | Others |
| a | All |
| + | Add permission |
| - | Remove permission |
| = | Set exact permission |
15. Numeric Permission Values
| Value | Permission |
|---|---|
| 7 | rwx |
| 6 | rw- |
| 5 | r-x |
| 4 | r-- |
| 0 | --- |
r = 4 w = 2 x = 1 7 = 4+2+1 = rwx 5 = 4+1 = r-x
16. The chown Command
Change the owner of a file or directory.
chown root file.txt
chown ashu report.txt
chown oracle:dba database.db
# Lab example only — do not give the web service account ownership of an entire web tree:
# chown -R apache:apache /var/www # insecure for production application code
# Prefer root (or a deploy user) for code, and the service user only on writable paths
# (see the production web scenario later in this chapter).
chown root:root /var/www/app/index.html
chown www-data:www-data /var/www/app/uploads # Debian/Ubuntu; RHEL often apache or nginx
17. The chgrp Command
chgrp developers report.txt
chgrp dba database.db
chgrp -R webteam website/
Use chgrp when only the group ownership needs to change.
18. Real Production Workflow
ls -l app.conf
cp app.conf app.conf.bak
chown root:appadmins app.conf
chmod 640 app.conf
ls -l app.conf
A common workflow is to inspect permissions, create a backup, update ownership, apply secure permissions and verify the final result.
19. chmod Best Practices
- Grant the minimum permissions required.
- Back up important configuration files before changes.
- Verify with
ls -lafter usingchmodorchown. - Avoid using
777unless absolutely necessary.
20. Hands-on: chmod chown chgrp
- Create a test file.
- Assign permission 644.
- Change it to 755.
- Use symbolic mode to add execute permission.
- Change the group ownership.
- Verify the changes using
ls -l.
21. chmod and Ownership Summary
Key Takeaways
chmodchanges permissions.- Use numeric or symbolic permission modes.
chownchanges owner and group.chgrpchanges only the group.- Always verify permissions after making changes.
22. Default Permissions with umask
New files start from base mode 666 and new directories from 777. The umask value clears permission bits from those bases (execute bits are not set on newly created regular files).
umask
umask 022
umask 027
umask -S
| umask | File (from 666) | Directory (from 777) |
|---|---|---|
| 022 | 644 | 755 |
| 027 | 640 | 750 |
| 077 | 600 | 700 |
23. SUID (Set User ID)
SUID allows an executable to run with the permissions of its owner rather than the user executing it.
chmod 4755 program
ls -l program
-rwsr-xr-x
24. SGID (Set Group ID)
SGID has two common uses:
- On a directory: new files and directories inherit the directory’s group. Useful for shared project trees.
- On an executable: the process runs with the file’s group as its effective GID (similar to how SUID elevates to the file’s owner).
# Shared directory — inherit group AND allow group members to create files
chmod 2775 shared
# drwxrwsr-x (2755 would inherit group but deny group write)
# Executable — run with file’s group privileges (use sparingly)
chmod 2755 helper
# -rwxr-sr-x
Collaborative project directories commonly use SGID with group write (for example 2775) so team members can create files that inherit the shared group without constantly running chgrp.
25. Sticky Bit
On a sticky directory, users may only delete or rename files they own (unless they own the directory or have privileged capabilities). Other users with write access to the directory cannot remove someone else’s files.
chmod 1777 /shared
drwxrwxrwt
/tmp is the most common example of a directory protected by the sticky bit.
26. Access Control Lists (ACLs)
ACLs provide more flexible permissions than the standard owner, group and others model.
getfacl report.txt
setfacl -m u:alice:rwx report.txt
setfacl -x u:alice report.txt
| Command | Purpose |
|---|---|
| getfacl | Display ACL entries. |
| setfacl | Add, modify or remove ACLs. |
27. Production Example
mkdir finance
chmod 2775 finance
setfacl -m u:auditor:rx finance
setfacl -d -m u:auditor:r-- finance
setfacl -m u:auditor:r-- finance/report.txt
getfacl -e finance
getfacl -e finance/report.txt
SGID keeps a shared group on new files in finance. The auditor’s directory ACL grants traversal/listing (rx). Reading file contents still requires a file ACL (or default ACL inheritance) — directory execute alone is not enough. Check the effective rights with getfacl -e because the ACL mask can limit permissions.
28. Special Bits Best Practices
- Use restrictive
umaskvalues on production servers. - Limit SUID executables to trusted programs.
- Use SGID for team collaboration directories.
- Use the sticky bit on shared writable directories.
- Document ACL changes for easier maintenance.
29. Hands-on: umask SUID SGID ACL
- Check the current
umask. - Create a test directory and observe default permissions.
- Apply SGID to a directory.
- Configure a test ACL using
setfacl. - Verify the ACL using
getfacl.
30. Special Bits and ACL Summary
Key Takeaways
umaskcontrols default permissions.- SUID executes programs with the owner's privileges.
- SGID inherits a directory’s group for new files, and on executables runs with the file’s group.
- The sticky bit restricts delete/rename in shared writable directories.
- ACLs provide fine-grained permission control.
31. Production Permission Troubleshooting
Permission issues are among the most common Linux administration problems. A structured approach helps identify the root cause quickly.
Permission Denied
│
▼
Check Owner
│
▼
Check Group
│
▼
Check Permissions
│
▼
Check ACL
whoami
id
ls -l file.txt
getfacl file.txt
32. Common Permission Denied Scenarios
| Problem | Possible Cause | Recommended Check |
|---|---|---|
| Cannot open file | No read permission | ls -l |
| Cannot modify file | No write permission | chmod / ownership |
| Cannot execute script | Missing execute bit | chmod +x |
| Application won't start | Incorrect ownership | chown |
| Unexpected access | ACL or 777 permissions | getfacl |
33. Production Workflow
whoami
id
ls -l application.conf
getfacl application.conf
chmod 640 application.conf
chown root:appadmins application.conf
ls -l application.conf
This workflow verifies identity, inspects permissions, checks ACLs, applies corrections and validates the final result.
34. Security Best Practices
- Apply the principle of least privilege.
- Avoid using
777permissions. - Grant execute permission only when required.
- Review SUID executables periodically.
- Audit ACL entries regularly.
- Document ownership changes.
35. Common Beginner Mistakes
- Using
chmod 777to solve every problem. - Changing ownership without understanding application requirements.
- Ignoring inherited group permissions.
- Forgetting recursive changes affect every file.
- Not verifying changes using
ls -l.
36. Interview Questions
- What is the difference between owner, group and others?
- Explain
755and644. - What does SUID do?
- When should SGID be used?
- Why is the sticky bit important?
- What is the purpose of
umask? - What are ACLs?
- Difference between
chmod,chownandchgrp?
37. Hands-on Lab: Troubleshooting
- Create a shared directory.
- Apply permission
755. - Change ownership to another user.
- Assign a new group.
- Configure SGID.
- Create an ACL for one user.
- Verify all changes.
38. FAQ
Should I use 777 permissions?
No. Use the minimum permissions required.
When should ACLs be preferred?
When a few users need additional access without changing ownership or primary groups.
Why is my script not executable?
Most commonly because the execute permission has not been granted.
39. Glossary
| Term | Meaning |
|---|---|
| Owner | User who owns a file. |
| Group | Shared access category. |
| ACL | Access Control List. |
| SUID | Runs with owner's privileges. |
| SGID | Directory: inherit group; executable: run with file’s group. |
| Sticky bit | Restricts delete/rename in shared writable directories. |
40. Chapter 3 Summary
Key Takeaways
- Linux secures files through ownership and permissions.
- Use
chmod,chownandchgrpcarefully. umaskcontrols default permissions.- SUID, SGID, sticky bit and ACLs provide advanced access control.
- Always verify permission changes before production deployment.
Ready for the Quiz?
You've completed Chapter 3. Test your knowledge before moving to the next chapter.
Start Chapter 3 Quiz →Commands Covered in This Chapter
chmod- Change file mode bitschown- Change file owner and groupchgrp- Change group ownershipumask- Set default file creation permissionsls -l- Display permissionsgetfacl- Get file access control listssetfacl- Set file access control listswhoami/id/groups- Inspect current user and group membership
Production Scenario: Securing Web Application
Scenario: Deploy a PHP app under /var/www/app with least privilege. Do not grant the web-service account write access to the entire application tree.
# Prefer a deploy/root-owned tree; web user only needs read (and execute on dirs)
# Debian/Ubuntu web user is often www-data; RHEL-family often apache or nginx
sudo chown -R root:root /var/www/app
sudo find /var/www/app -type d -exec chmod 755 {} \;
sudo find /var/www/app -type f -exec chmod 644 {} \;
# Secrets: readable only by the service account (adjust WEBUSER for your distro)
# WEBUSER=www-data # Debian/Ubuntu
# WEBUSER=apache # RHEL/Rocky with httpd (or nginx for nginx)
WEBUSER=www-data
sudo chown root:"$WEBUSER" /var/www/app/config/database.php
sudo chmod 640 /var/www/app/config/database.php
# Writable runtime path only (uploads/cache) — not the whole codebase
sudo mkdir -p /var/www/app/uploads
sudo chown "$WEBUSER":"$WEBUSER" /var/www/app/uploads
sudo chmod 750 /var/www/app/uploads
This follows least privilege: application code stays non-writable by the service account, while uploads remain writable. Correct ownership reduces damage from a compromised process; it does not by itself prevent web shells or other application vulnerabilities.