File Permissions
In this exercise, we will explore how file permissions work in Linux and learn how to view and modify them. Understanding file permissions is crucial for managing access to files and directories in a Linux environment.
Background
Every file and directory in Linux has three permission groups:
| Group | Who |
|---|---|
| Owner (u) | The user who owns the file |
| Group (g) | Users in the file's group |
| Others (o) | Everyone else |
Each group has three permission bits:
| Bit | Symbol | Binary | Octal |
|---|---|---|---|
| Read | r | 100 | 4 |
| Write | w | 010 | 2 |
| Execute | x | 001 | 1 |
Permissions are combined by adding the values. For example rwx = 4+2+1 = 7, r-x = 4+0+1 = 5, r-- = 4+0+0 = 4.
A full permission string like rwxr-xr-- breaks into three octal digits: 7 5 4.
rwxr-xr--
^^^ owner: rwx = 111 = 7
^^^ group: r-x = 101 = 5
^^^ others: r-- = 100 = 4
So chmod 754 file sets those exact permissions.
Goal
Practice reading and setting file permissions using both symbolic (chmod u+x) and octal (chmod 755) notation.
Step 1: View Permissions
Create a file and inspect its default permissions:
touch myfile.txt
ls -l myfile.txt
The output will look like:
-rw-r--r-- 1 user group 0 Jan 1 12:00 myfile.txt
The leading - means it is a regular file (a d would mean directory). The next 9 characters are the permission bits: rw- (owner), r-- (group), r-- (others).
Question: What is the octal representation of rw-r--r--?
Answer
rw- = 110 = 6, r-- = 100 = 4, r-- = 100 = 4 → 644
Step 2: Symbolic Mode
Use symbolic notation to modify permissions:
# Add execute for the owner
chmod u+x myfile.txt
ls -l myfile.txt
# Remove write for group
chmod g-w myfile.txt
ls -l myfile.txt
# Add read+write for others
chmod o+rw myfile.txt
ls -l myfile.txt
Question: After all three commands, what is the symbolic permission string and its octal equivalent?
Answer
rwx (owner) + r-- (group) + rw- (others) = rwxr--rw-
Octal: 7 4 6 → 746
Step 3: Octal Mode
Reset and apply permissions directly with octal values:
chmod 644 myfile.txt # rw-r--r-- (default for files)
chmod 755 myfile.txt # rwxr-xr-x (executable / directory)
chmod 700 myfile.txt # rwx------ (private executable)
chmod 600 myfile.txt # rw------- (private file)
For each command above, write out the binary and symbolic form before running it to verify your understanding.
Question: What octal value gives rwxrwxrwx (full permissions for everyone)?
Answer
111 111 111 = 7 7 7 → 777
Step 4: Directory Permissions
Permissions behave slightly differently for directories:
| Bit | Meaning on a directory |
|---|---|
r | List contents (ls) |
w | Create/delete files inside |
x | Enter the directory (cd) |
mkdir mydir
ls -ld mydir
# Remove execute — try to enter
chmod u-x mydir
cd mydir # should fail
# Restore
chmod u+x mydir