Exercise 04 — File Permissions¶
Navigation: ← Exercise 03 | Exercises Index | Next → Exercise 05 Note: 04 — File Permissions
Before You Start¶
Read Note 04 first.
Permissions are one of the most important concepts in Linux security. In this exercise you'll learn to read permission strings, change them, and understand why certain permissions are set the way they are.
Exercise 4.1 — Reading Permission Strings¶
Before changing anything, practice reading permissions.
Step 1: Run this and look carefully at the output:
Expected output (something like):
-rwxr-xr-x 1 root root 147424 Sep 5 2019 /bin/ls
-rw-r--r-- 1 root root 2423 Jun 20 10:00 /etc/passwd
drwxrwxrwt 9 root root 4096 Jun 20 14:00 /tmp
Step 2: Decode each permission string using this template:
Position: 1 234 567 890
d rwx r-x r-x
^ ^^^ ^^^ ^^^
| | | |
| | | └── Others permissions
| | └──────── Group permissions
| └────────────── Owner permissions
└────────────────── File type (d=dir, -=file, l=link)
Fill in this table:
| File | Type | Owner can | Group can | Others can |
|---|---|---|---|---|
/bin/ls |
? | ? | ? | ? |
/etc/passwd |
? | ? | ? | ? |
/tmp |
? | ? | ? | ? |
Step 3: Check a sensitive file:
Expected output:
Questions to think about:
- Why does /etc/shadow have stricter permissions than /etc/passwd?
- What does the t at the end of /tmp's permissions mean? (Hint: see Note 04 — sticky bit)
- Who can write to /tmp? Who can delete files from it?
Exercise 4.2 — Set Up Practice Files¶
Step 1: Create a directory for this exercise:
Step 2: Create three files:
Step 3: Check their current permissions:
Expected output:
-rw-r--r-- 1 jahid jahid 0 Jun 20 14:00 document.txt
-rw-r--r-- 1 jahid jahid 0 Jun 20 14:00 private.txt
-rw-r--r-- 1 jahid jahid 0 Jun 20 14:00 script.sh
All three start with rw-r--r-- (644). This is the default — the owner can read/write, everyone else can only read. Notice none of them are executable yet.
Exercise 4.3 — Change Permissions with chmod (Symbolic)¶
Step 1: Make script.sh executable for the owner:
Expected output:
See how rw- became rwx for the owner position? The x was added.
Step 2: Remove read permission from others on private.txt:
Expected output:
Others went from r-- to ---. Now only the owner and group can read this file.
Step 3: Set the group to have no permissions at all on private.txt:
Expected output:
Now only the owner can read or write it. This is how you protect sensitive files.
Step 4: Use = to set permissions exactly:
o=r means "set others' permissions to exactly read-only". The = replaces whatever was there. This is different from + (which adds) or - (which removes).
Exercise 4.4 — Change Permissions with chmod (Numeric/Octal)¶
Numeric mode is faster once you understand it. Each digit represents owner, group, and others.
The key to remember:
- r = 4
- w = 2
- x = 1
- Add them up: rw- = 4+2+0 = 6, r-x = 4+0+1 = 5, rwx = 4+2+1 = 7
Before running each command below, predict the permission string:
Did you get rwxr-xr-x? (7=rwx, 5=r-x, 5=r-x) ✓
Did you get rw-r--r--? (6=rw-, 4=r--, 4=r--) ✓
Did you get rw-------? (6=rw-, 0=---, 0=---) ✓
Practice: Before running each command, predict the result:
| Command | Predicted result |
|---|---|
chmod 777 script.sh |
? |
chmod 400 private.txt |
? |
chmod 750 document.txt |
? |
Run them, then check:
- 777 → rwxrwxrwx (full access for everyone — rarely a good idea)
- 400 → r-------- (read-only for owner, no access for anyone else)
- 750 → rwxr-x--- (owner full, group read+execute, others nothing)
Exercise 4.5 — Changing Ownership with chown¶
Needs sudo for the first part.
Step 1: View the current owner and group of your practice files:
Everything should show your username as both owner and group.
Step 2: Create a test file as root:
Expected output:
The owner is root and the group is root. You (as jahid) cannot modify this file even though it's in your directory.
Step 3: Try to write to it without sudo:
Expected error:
You're blocked because you don't own the file and don't have write permission as "others".
Step 4: Transfer ownership to yourself:
Expected output:
Now you own it. Try writing to it again:
It works now.
Step 5: Change both owner and group at once:
Expected output:
Exercise 4.6 — Understand the Sticky Bit on /tmp¶
The sticky bit is already in action on your system. Let's see it.
Step 1: Inspect /tmp:
Expected output:
drwxrwxrwt — the t at the end is the sticky bit. The permissions rwxrwxrwx would normally mean anyone can delete any file. The sticky bit changes that: you can only delete files you own.
Step 2: Create two files in /tmp — one as yourself, one as root:
Step 3: Try to delete root's file without sudo:
Expected error:
The sticky bit is preventing you from deleting a file you don't own.
Step 4: Delete your own file (this works fine):
No error — you own it.
Step 5: Clean up root's test file:
Exercise 4.7 — umask — Where Do Default Permissions Come From?¶
Step 1: Check the current umask:
Expected output:
Step 2: Create a file and directory and observe the defaults:
touch ~/perm-practice/default-file.txt
mkdir ~/perm-practice/default-dir
ls -l ~/perm-practice/default-file.txt
ls -ld ~/perm-practice/default-dir
Expected output:
-rw-r--r-- 1 jahid jahid 0 Jun 20 14:00 default-file.txt
drwxr-xr-x 2 jahid jahid 4096 Jun 20 14:00 default-dir/
Verify the math: File default is 666, minus umask 022 = 644 (rw-r--r--). Directory default is 777, minus 022 = 755 (rwxr-xr-x). ✓
Step 3: Change the umask and see the effect:
Expected output:
With umask 077: 666 - 077 = 600 (rw-------). Only the owner can read/write. Nobody else has any access at all.
Step 4: Restore the normal umask:
Important: Changing
umaskonly lasts for the current session. Once you close the terminal, it resets. To make it permanent, addumask 022to your~/.bashrcfile.
Cleanup¶
Challenge — Permission Puzzle¶
Without running any commands first, predict the permission string for each octal:
| Octal | Permission string |
|---|---|
755 |
? |
644 |
? |
600 |
? |
777 |
? |
444 |
? |
711 |
? |
Then run this to check your answers:
touch /tmp/permtest
for perm in 755 644 600 777 444 711; do
chmod $perm /tmp/permtest
echo -n "$perm → "
ls -l /tmp/permtest | awk '{print $1}'
done
rm /tmp/permtest
How many did you get right?
Navigation: ← Exercise 03 | Exercises Index | Next → Exercise 05 Note: 04 — File Permissions