How does the chmod octal notation work in Linux (e.g. 755, 644)?
Understand how Linux chmod octal notation works: read=4, write=2, execute=1 per owner, group, and other, plus why directories use 755 and files use 644.
Expected Interview Answer
In Linux chmod octal notation, each of the three digits sets permissions for owner, group, and others, where the digit is the sum of read (4), write (2), and execute (1), so 755 means owner rwx and group/others r-x, while 644 means owner rw- and group/others r--.
Permissions come in three classes (user, group, other), each with three bits: read=4, write=2, execute=1. Adding the bits you want gives one octal digit per class, so 7 is rwx, 6 is rw-, 5 is r-x, and 4 is r--. Directories need the execute bit to be enterable, which is why folders are typically 755 and files 644. An optional fourth leading digit sets special bits: setuid (4), setgid (2), and the sticky bit (1).
- Sets all three permission classes in one compact command
- Deterministic: each digit is an exact bit sum
- Ideal for scripts and reproducible deployments
- Maps directly to the rwx bits shown by ls -l
- Supports special bits via an optional fourth digit
AI Mentor Explanation
Think of three access levels to a stadium, players, coaching staff, and general public, each given a wristband encoding three privileges worth 4, 2, and 1. A 755 wristband set means the captain gets full ground access, while staff and public may only enter and watch but not alter the pitch, exactly as owner rwx and others r-x controls who can change versus merely use.
Step-by-Step Explanation
Step 1
Learn the bit values
Assign read=4, write=2, execute=1; add the ones you want for a single class.
Step 2
Compute each class digit
Do this separately for owner, group, and other to build a three-digit number.
Step 3
Combine into octal
For example rwx=7, r-x=5, r--=4, so a directory becomes 755 and a file 644.
Step 4
Apply with chmod
Run chmod 755 script.sh or chmod 644 config.txt to set the mode.
Step 5
Verify with ls -l
Confirm the resulting -rwxr-xr-x or -rw-r--r-- string matches your intent.
What Interviewer Expects
- Knowing read=4, write=2, execute=1
- Mapping each digit to owner, group, other
- Explaining why directories need execute (755)
- Reading the ls -l symbolic string
- Awareness of the fourth digit for setuid/setgid/sticky
Common Mistakes
- Mixing up the order of owner, group, and other
- Giving files 777 out of laziness, a security risk
- Forgetting directories need execute to be entered
- Confusing execute on files with execute on directories
- Not knowing the leading fourth digit controls special bits
Best Answer (HR Friendly)
“chmod octal numbers like 755 set who can read, write, and run a file. Each digit is for the owner, the group, and everyone else, and it is the total of read (4), write (2), and execute (1).”
Code Example
# Owner rwx, group and others r-x -> 755 (typical for scripts/dirs)
chmod 755 deploy.sh
ls -l deploy.sh # -rwxr-xr-x
# Owner rw-, group and others r-- -> 644 (typical for config/files)
chmod 644 app.conf
ls -l app.conf # -rw-r--r--
# Fourth digit: setgid (2) + rwxr-xr-x -> 2755
chmod 2755 shared_dir
# Recursively fix a tree
chmod -R 644 /var/www/htmlFollow-up Questions
- Why do directories typically need 755 instead of 644?
- What does a leading fourth digit like 4755 or 1777 do?
- How does symbolic chmod (u+x) compare to octal?
- Why is chmod 777 considered a security risk?
MCQ Practice
1. What permissions does chmod 644 grant?
6 = rw- (4+2) for the owner, and 4 = r-- for both group and others.
2. In octal notation, what does the execute bit contribute?
Read=4, write=2, execute=1, so execute adds 1 to a permission digit.
3. Why are directories usually set to 755 rather than 644?
On directories the execute bit permits traversal (cd/listing paths), which read alone does not.
Flash Cards
chmod bit values? — read=4, write=2, execute=1, summed per class.
What is 755? — Owner rwx, group r-x, others r-x.
What is 644? — Owner rw-, group r--, others r--.
What does the fourth leading digit do? — Sets special bits: setuid=4, setgid=2, sticky=1.