Permissions¶
Understanding Unix permissions is essential for security and proper system administration.
The Permission Model¶
Every file and directory has:
- An owner (user)
- A group
- Permissions for owner, group, and others
Breaking down -rw-r--r--:
| Position | Meaning |
|---|---|
- | File type (- file, d directory, l link) |
rw- | Owner permissions |
r-- | Group permissions |
--- | Other permissions |
Permission Types¶
| Symbol | Meaning | File | Directory |
|---|---|---|---|
r | Read | View contents | List contents |
w | Write | Modify contents | Create/delete files |
x | Execute | Run as program | Enter directory |
- | None | Permission denied | Permission denied |
Directory Permissions Explained¶
For directories, permissions work differently:
- Read (r): Can list files with
ls - Write (w): Can create, delete, rename files inside
- Execute (x): Can
cdinto directory, access files
Common Gotcha
A directory needs x permission to access anything inside, even with r:
Numeric (Octal) Notation¶
Permissions can be expressed as numbers:
| Permission | Value |
|---|---|
| Read (r) | 4 |
| Write (w) | 2 |
| Execute (x) | 1 |
| None (-) | 0 |
Add values for each category:
| Octal | Permissions | Meaning |
|---|---|---|
7 | rwx | Full access |
6 | rw- | Read and write |
5 | r-x | Read and execute |
4 | r-- | Read only |
3 | -wx | Write and execute |
2 | -w- | Write only |
1 | --x | Execute only |
0 | --- | No access |
Common permission sets:
| Octal | Symbolic | Typical Use |
|---|---|---|
755 | rwxr-xr-x | Executables, directories |
644 | rw-r--r-- | Regular files |
700 | rwx------ | Private directories |
600 | rw------- | Private files |
777 | rwxrwxrwx | Full access (avoid!) |
chmod - Change Mode¶
Symbolic Mode¶
chmod u+x script.sh # Add execute for user
chmod g-w file.txt # Remove write for group
chmod o=r file.txt # Set others to read only
chmod a+r file.txt # Add read for all
chmod u+x,g-w file.txt # Multiple changes
Symbols:
| Symbol | Meaning |
|---|---|
u | User (owner) |
g | Group |
o | Others |
a | All (u+g+o) |
+ | Add permission |
- | Remove permission |
= | Set exactly |
Numeric Mode¶
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 secret.txt # rw-------
chmod 700 private_dir # rwx------
Recursive Changes¶
Reference Another File¶
chown - Change Owner¶
Change file ownership:
chown alice file.txt # Change owner
chown alice:staff file.txt # Change owner and group
chown :staff file.txt # Change group only
chown -R alice:staff directory/ # Recursive
chgrp - Change Group¶
umask - Default Permissions¶
umask sets default permissions for new files:
The umask is subtracted from maximum permissions:
- Files max:
666(no execute by default) - Directories max:
777
| umask | File Result | Directory Result |
|---|---|---|
022 | 644 | 755 |
077 | 600 | 700 |
002 | 664 | 775 |
Set umask:
Add to .bashrc for persistence.
Special Permissions¶
Setuid (Set User ID)¶
When executed, runs as the file owner:
The s in owner execute position indicates setuid.
Setgid (Set Group ID)¶
For files: runs as file's group For directories: new files inherit directory's group
Sticky Bit¶
Only owner (or root) can delete files in directory:
The t indicates sticky bit.
Combined Special Permissions¶
chmod 4755 file # setuid + rwxr-xr-x
chmod 2755 dir # setgid + rwxr-xr-x
chmod 1777 dir # sticky + rwxrwxrwx
Users and Groups¶
View Current User¶
List Groups¶
Managing Groups (Linux)¶
sudo groupadd developers
sudo usermod -aG developers alice # Add user to group
newgrp developers # Switch primary group
Common Permission Scenarios¶
Make Script Executable¶
Secure SSH Keys¶
Web Server Files¶
Shared Directory¶
Private Directory¶
Checking Effective Permissions¶
test Command¶
[[ -r file.txt ]] && echo "Readable"
[[ -w file.txt ]] && echo "Writable"
[[ -x file.txt ]] && echo "Executable"
stat Command¶
ACLs (Access Control Lists)¶
For more granular control beyond owner/group/other:
macOS¶
ls -le file.txt # View ACLs
chmod +a "alice allow read" file.txt
chmod -a "alice allow read" file.txt # Remove
Linux¶
Try It¶
-
Create test files:
-
Examine permissions:
-
Modify permissions:
-
Test access:
-
Clean up:
Summary¶
| Command | Purpose |
|---|---|
chmod | Change permissions |
chown | Change owner |
chgrp | Change group |
umask | Set default permissions |
ls -l | View permissions |
stat | Detailed file info |
id | User and group info |
| Octal | Symbolic | Meaning |
|---|---|---|
755 | rwxr-xr-x | Standard executable/directory |
644 | rw-r--r-- | Standard file |
700 | rwx------ | Private directory |
600 | rw------- | Private file |