Understand File Permissions
- In Linux / Unix, everything is a file
- Files
- Directories
- Devices (usually referenced via nodes)
- All files in the system have permissions that allow or prevent others from:
- Viewing (read)
- Modifying (write)
- Executing (execute)
- If a file is a directory, permissions affect different actions than regular files or device nodes.
Superuser
- Superuser (root) has access to any file on the system.
Ownership & Permissions
- Each file has access restrictions based on:
- Owner
- Group
- Others
- Permissions are referred to as bits.
Permission bits
Each file/directory has three permissions:
- r = read
- w = write
- x = execute
Administrative Access
- To change or edit files that are owned by root, sudo must be used
- See man sudo for details
Type of Access Permissions
There are three types of access permissions:
| Permission | Action | Symbol | Numeric value |
|---|---|---|---|
| Read | View | r | 4 |
| Write | Edit | w | 2 |
| Execute | Execute | x | 1 |
Type of User Restrictions
There are three types of users:
- Owner
- Group
- Other
ls output example:
-rwxrwxrwx
- First 3 bits → Owner
- Next 3 bits → Group
- Last 3 bits → Other
Example: Permission
If owner read & execute bits are on, then the permissions are:
-r-x------
Scope of Restrictions
- Restriction scope is not inherited
- File owner is not affected by restrictions set for:
- Group
- Others
Directory Permissions
- Directories have directory permissions
- Directory permissions restrict different actions than files or device nodes
Explain Permission values
Linux adds permission values together:
r = 4w = 2x = 1
Examples:
rwx = 4 + 2 + 1 = 7r-x = 4 + 0 + 1 = 5rw- = 4 + 2 + 0 = 6
So:
chmod 755 file
Means:
- Owner → rwx (7)
- Group → r-x (5)
- Other → r-x (5)
Directory Permissions (Meaning of r, w, x on folders)
| Permission | Meaning (Directory) | Example command |
|---|---|---|
| read (r) | View directory contents | ls |
| write (w) | Create or remove files in directory | touch, rm |
| execute (x) | Enter directory | cd |
⚠️ Without execute (x) on a directory, you cannot access files inside, even if you know their names.
Directory read permission
- Allows viewing directory contents
- Example:
ls directory_name
Directory write permission
- Allows creating and deleting files inside the directory
- ⚠️ Important concept:
- Write permission on a directory allows deleting a file even if the user does NOT have write permission on the file itself
Directory execute permission
- Allows entering (changing into) the directory
- Example:
cd directory_name
Critical Rule (⚠️)
All folders (directories) MUST have execute (x) permission
- If a directory does not have execute permission:
- You cannot access files inside it
- It may disappear from file browsers
- It will not function as a directory
Real Example:
Example 1: /etc/hosts
-rw-r--r-- 1 root root 288 Jan 13 19:24 /etc/hosts
- Owner: root → read & write (
rw-) - Group: root → read (
r--) - Others: read (
r--)
➡️ This file is:
- Owned by root
- Belongs to root group
- Editable only by root
To edit:
sudo nano /etc/hosts
Key Concepts (Exam)
Directory vs file permissions
| Permission | File | Directory |
|---|---|---|
| r | Read file | List contents |
| w | Modify file | Create/delete files |
| x | Execute file | Enter directory |
Why directories “disappear”
If a directory has:
rw-------
but no execute (x) → it becomes unusable
Correct:
rwx------
Others matters for exams & samba
- Samba does not override Linux permissions
- If Linux blocks access → Samba blocks access
- Missing execute (x) on a directory = Access Denied, even if file permissions look correct
- Most permission problems come from directory execute bits missing
One-line golden rule ⭐
For directories, execute (x) is MORE important than read (r)
Change Permissions
chmod
- Used to modify file or directory permissions
- There are two ways to change permissions:
- With letters (easier to understand)
- With numbers (octal method)
⚠️ Change permissions very carefully, otherwise security can be breached
Important Security File
/etc/shadow- Stores encrypted user passwords
- Must NOT be accessible to regular users
Example 1:
cd /etc
ls -l | grep shadow
chmod with Letters (Symbolic mode)
Syntax
chmod [options] filename
User options
| Symbol | Meaning |
|---|---|
| u | owner |
| g | group |
| o | others |
| a | all (same as ugo) |
Permission options
| Symbol | Meaning |
|---|---|
| r | read |
| w | write |
| x | execute |
Operators
| Symbol | Meaning |
|---|---|
| + | add permission |
| – | remove permission |
| = | set exact permission |
Examples:
Add execute permission to owner
chmod u+x script.sh
Remove write permission from group
chmod g-w file.txt
Set exact permissions for others
chmod o=r file.txt
Give read permission to everyone
chmod a+r file.txt
Numeric (Octal) reminder
| Permission | Value |
|---|---|
| r | 4 |
| w | 2 |
| x | 1 |
Example:
chmod 644 file.txt
- Owner → rw-
- Group → r–
- Others → r–
Why /etc/shadow is mentioned (exam trap ⚠️)
-rw-r----- 1 root shadow /etc/shadow
- Only root can read/write
- Group
shadowcan read - Others → NO access
❌ Never do:
chmod 644 /etc/shadow
✔ Correct:
-rw-r----- (640)
Key exam points ⭐
chmodchanges permissions, not ownership- Wrong permissions can expose sensitive files
- Symbolic mode = readable
- Numeric mode = faster
One-line golden rule ⭐
Never loosen permissions on system files unless you fully understand the impact
chmod with Letters (Symbolic Mode)
Examples:
Add execute permission to the owner
chmod u+x file
ls -l # verify
Before:
-rw-r--r--
After:
-rwxr--r--
Add write + execute permission to others
chmod o+wx file
or
chmod o+w,o+x file
Result:
-rw-r--rwx
Remove read permission from group
👉 To remove permissions, use - instead of +.
chmod g-r file
Example:
Before:
-rw-r--r--
After:
-rw----r--
Add read, write, execute to everyone
chmod a+rwx file
Result:
-rwxrwxrwx
⚠️ Dangerous on system files — used mostly for testing or temporary cases.
chmod with Numbers (Numeric / Octal Mode)
Permission values recap
| Permission | Value |
|---|---|
| read (r) | 4 |
| write (w) | 2 |
| execute (x) | 1 |
Ownership position recap
chmod XYZ file
- X → owner
- Y → group
- Z → others
Add Read + write for everyone
chmod 666 file
Breakdown:
- Owner → 6 (rw-)
- Group → 6 (rw-)
- Others → 6 (rw-)
Result:
-rw-rw-rw-
Add Owner full, others read & execute
chmod 755 file
Result:
-rwxr-xr-x
Secure private file
chmod 600 file
Result:
-rw-------
Mapping numbers to permissions
| Number | Permission |
|---|---|
| 7 | rwx |
| 6 | rw- |
| 5 | r-x |
| 4 | r– |
| 0 | — |
Key exam & practical notes ⭐
- Symbolic mode (
u+x) is safer and easier to read - Numeric mode (
755) is faster chmoddoes not change ownershipa+rwxgives full access to everyone → ⚠️ risky- Always verify with:
ls -l
One-line golden rule ⭐
Use the least permissions necessary — never more
Permission Values Recap with Multiple Examples
Permission Values Recap and Examples
| Permission | Value |
|---|---|
| read (r) | 4 |
| write (w) | 2 |
| execute (x) | 1 |
Add them together.
2️⃣ Example breakdown
Example 1
chmod 667 file1
Breakdown:
Owner: read + write → 4 + 2 = 6
Group: read + write → 4 + 2 = 6
Others: read + write + execute → 4 + 2 + 1 = 7
Result:
-rw-rw-rwx
Example 2
chmod 251 file1
Breakdown:
- Owner →
2→-w- - Group →
5→r-x - Others →
1→--x
Result:
--w-r-x--x
✅ This is valid but rarely used in practice
Example 3
chmod 644 file1 file2 file3
Breakdown:
Owner: read + write → 6
Group: read → 4
Others: read → 4
Result:
-rw-r--r--
✔ Very common (default file permission)
3️⃣ Multiple files with chmod
You can apply permissions to multiple files at once:
chmod 644 file1 file2 file3
4️⃣ Default permissions (important concept)
Typical default permissions:
- Files:
-rw-r--r--→644 - Directories:
drwxr-xr-x→755
⚠️ Files do not get execute permission by default
5️⃣ Adding owner execute bit (correct way)
If file is:
-rw-r--r--
Add execute for owner:
chmod u+x file
Result:
-rwxr--r--
6️⃣ Adding execute using numbers
chmod 744 file1 file2 file3
Breakdown:
- Owner →
7→ rwx - Group →
4→ r– - Others →
4→ r–
Result:
-rwxr--r--
7️⃣ Add write + execute to others
chmod 747 file1 file2 file3
Breakdown:
- Owner → 7 → rwx
- Group → 4 → r–
- Others → 7 → rwx
Result:
-rwxr--rwx
⚠️ Dangerous on shared systems
8️⃣ Removing group read bit (numeric thinking)
If file is:
-rw-r--r--
Remove group read:
chmod 604 file
Result:
-rw----r--
(or safer with symbolic:)
chmod g-r file
Add read, write & execute to everyone
chmod 777 file1 file2 file3
⚠️ Very insecure – only for testing or temporary use.
9️⃣ Quick numeric reference (exam gold ⭐)
| Number | Meaning |
|---|---|
| 7 | rwx |
| 6 | rw- |
| 5 | r-x |
| 4 | r– |
| 3 | -wx |
| 2 | -w- |
| 1 | –x |
| 0 | — |
🔑 Golden rules (very important)
- Files don’t need execute unless they’re scripts
- Directories MUST have execute (
x) to work chmod 777= ❌ security risk- Always verify:
ls -l
chmod with sudo
- If you do not own a file, you cannot change its permissions
- Use
sudoto modify permissions of files owned by root - Be extremely careful: wrong permissions can break the system
Example:
sudo chmod o+x /usr/local/bin/file
Recursive Permission Changes
Purpose
To change permissions of multiple files and directories at once
⚠️ Warning: Recursive chmod can be dangerous if used incorrectly
Recursive chmod using -R
sudo chmod -R 777 /test/test1
- Changes permissions of:
/test/test1- All subdirectories
- All files inside
Verify
ls -l
Explanation in simple terms
Why sudo chmod is risky ⚠️
- System files (e.g.
/bin,/usr,/etc) depend on strict permissions - Accidentally running:
sudo chmod -R 777 /
❌ Will destroy your system
Best Practice
Use symbolic permissions
sudo chmod -R u+rwX,go+rX /some/dir
This:
- Adds execute only to directories
- Avoids making regular files executable
Common safe patterns
Give owner full control, others read-only
chmod -R 755 directory
Private directory
chmod -R 700 directory
Key exam & practical rules ⭐
sudo chmod→ only when you are not the owner-R→ applies to everything insidechmod 777→ ❌ insecure- Always verify:
ls -l
Golden rules (⭐)
- Never use recursive chmod on system directories
- Never use 777 unless you fully understand why
- Prefer symbolic mode when using
-R - Test on one file first
Recursive chmod using find, pipes & sudo
Problem with chmod -R
chmod -R 755 directory
❌ This applies the same permissions to:
- files
- directories
But:
- Files usually should be
644 - Directories usually should be
755
So we need more control.
Correct Permissions
Files → 644
Owner : read + write
Group : read
Others: read
Directories → 755
Owner : read + write + execute
Group : read + execute
Others: read + execute
👉 Execute (x) on directories means enter/access directory
Solution: Use find + chmod
Change permissions of only files
sudo find /path/to/directory -type f -print0 | xargs -0 sudo chmod 644
Explanation:
find→ finds files-type f→ only files-print0→ safe for filenames with spacesxargs -0→ passes them safelychmod 644→ applies permission
Change permissions of only directories
sudo find /path/to/directory -type d -print0 | xargs -0 sudo chmod 755
Why not use plain chmod -R?
| Method | Risk |
|---|---|
chmod -R 755 dir | Files become executable ❌ |
chmod -R 644 dir | Directories become unusable ❌ |
find + chmod | Safe & professional ✅ |
Combined Real-World Fix
sudo find /project -type f -exec chmod 644 {} \;
sudo find /project -type d -exec chmod 755 {} \;
✔ Correct
✔ Secure
✔ Industry standard
Warning with recursive chmod ⚠️
Before running any recursive command:
✔ Double-check the path
✔ Test on a small directory
❌ Never run on /, /usr, /etc, /bin
🚨 THIS COMMAND WILL DESTROY A SYSTEM
sudo chmod -R 777 /
Key exam points ⭐
chmod -Rapplies same permissions to everythingfind -type f→ files onlyfind -type d→ directories only-print0 | xargs -0→ handles spaces safely- Preferred over recursive
chmod
One-line summary ⭐
Use
chmod -Ronly for simple cases.
Usefindwhen files and directories need different permissions.
Changing the File Owner and Group
In Linux, every file has:
- Owner (user)
- Group
To change them, you usually need root (sudo) privileges.
1️⃣ Changing the file owner (chown)
Syntax
sudo chown user file
Example
sudo chown vivek file3
✔ Changes the owner of file3 to vivek
✔ Group remains unchanged
Change owner of multiple files / directories
sudo chown vivek file1 file2
sudo chown vivek /test
Recursive owner change
sudo chown -R vivek /test
✔ Changes owner of all files and folders inside /test
2️⃣ Changing the group owner (chgrp)
Syntax
sudo chgrp group file
Example
sudo chgrp tgsgroup file1
✔ Changes only the group
✔ Owner remains unchanged
Recursive group change
sudo chgrp -R tgsgroup /test
✔ Changes group owner of all files and folders inside /test
3️⃣ Changing owner AND group together (single command)
Syntax
sudo chown USER:GROUP file
Example
sudo chown vivek:tgsgroup file
✔ Owner → vivek
✔ Group → tgsgroup
Recursive owner + group change
sudo chown -R vivek:tgsgroup /test
✔ Changes owner and group of all files and folders inside /test
Change group owner by chown command
sudo chown :tgsgroup file
Important Notes (Exam-Relevant ⭐)
🔹 Why sudo is required?
- Normal users cannot change ownership
- Only root can change owner/group
✔ That’s why:
By default you must use sudo to change a file owner or group
Summary Table ⭐
| Task | Command |
|---|---|
| Change owner | sudo chown user file |
| Change group | sudo chgrp group file |
| Change owner + group | sudo chown user:group file |
| Recursive change | sudo chown -R user:group dir |
One-line exam summary 📝
chownchanges owner (and optionally group),chgrpchanges only group, and both usually require sudo.










