File Permissions
Challenge Gallery
Quick Reference
Permission values:
| Symbol | Meaning | Octal |
|---|---|---|
r |
Read | 4 |
w |
Write | 2 |
x |
Execute | 1 |
- |
No permission | 0 |
Reading a permission string:
- rwx r-x r-x
│ │ │ └── other: read + execute
│ │ └── group: read + execute
│ └── user/owner: read + write + execute
└── file type (- = file, d = directory, l = symlink)
Common permission patterns:
| Octal | String | Typical Use |
|---|---|---|
755 |
rwxr-xr-x |
Executables, directories |
644 |
rw-r--r-- |
Regular files (source code, text) |
700 |
rwx------ |
Private directory |
600 |
rw------- |
Private file (SSH keys, configs) |
chmod syntax:
| Form | Example | Meaning |
|---|---|---|
| Symbolic | chmod u+x file |
Add execute for user |
| Symbolic | chmod g-w file |
Remove write for group |
| Symbolic | chmod o=r file |
Set other to read only |
| Symbolic | chmod a+r file |
Add read for all |
| Octal | chmod 755 file |
Set rwxr-xr-x |
| Octal | chmod 644 file |
Set rw-r–r– |
File vs. directory permissions:
| Permission | On a File | On a Directory |
|---|---|---|
r (read) |
View contents (cat, less) |
List contents (ls) |
w (write) |
Modify contents | Create/delete files inside |
x (execute) |
Run as a program | Enter the directory (cd) |
How It Works
Reading Permissions in ls -l
student@ubuntu:~/cscd240$ ls -l
-rw-r--r-- 1 student student 78 Mar 30 09:00 hello.c
-rwxr-xr-x 1 student student 8240 Mar 30 09:01 hello
drwxr-xr-x 2 student student 4096 Mar 30 08:45 labs
-rw-r--r-- 1 student student 78 Mar 30 09:00 hello.c
-rwxr-xr-x 1 student student 8240 Mar 30 09:01 hello
drwxr-xr-x 2 student student 4096 Mar 30 08:45 labs
Changing Permissions
# Symbolic: add execute for owner
student@ubuntu:~/cscd240$ chmod u+x script.sh
# Symbolic: remove write for group and others
student@ubuntu:~/cscd240$ chmod go-w hello.c
# Octal: standard executable permissions
student@ubuntu:~/cscd240$ chmod 755 hello
# Octal: private directory
student@ubuntu:~/cscd240$ chmod 700 private_dir
# Verify
student@ubuntu:~/cscd240$ ls -l hello
-rwxr-xr-x 1 student student 8240 Mar 30 09:01 hello
student@ubuntu:~/cscd240$ chmod u+x script.sh
# Symbolic: remove write for group and others
student@ubuntu:~/cscd240$ chmod go-w hello.c
# Octal: standard executable permissions
student@ubuntu:~/cscd240$ chmod 755 hello
# Octal: private directory
student@ubuntu:~/cscd240$ chmod 700 private_dir
# Verify
student@ubuntu:~/cscd240$ ls -l hello
-rwxr-xr-x 1 student student 8240 Mar 30 09:01 hello
Octal is Binary in Disguise
rwx = 111 = 7 r-x = 101 = 5 r-- = 100 = 4
rw- = 110 = 6 -wx = 011 = 3 --- = 000 = 0
Common Pitfalls
chmod 777as a fix-all – Gives everyone full access. Find the real problem instead.- Forgetting x on directories – A directory needs
xtocdinto it.ralone lets you see names but not access files. - Confusing file and directory permissions –
won a directory means create/delete files inside, not modify the directory itself. - Not checking permissions after compilation –
gccsets the execute bit automatically, but if you copy a binary from elsewhere, you may needchmod u+x.