chmod
Overview
The chmod command changes file and directory permissions in Linux. It controls read, write, and execute permissions for owner, group, and others.
Syntax
chmod [options] mode file...
chmod [options] octal-mode file...
chmod [options] --reference=rfile file...Common Options
| Option | Description |
|---|---|
-R |
Recursive operation |
-v |
Verbose output |
-c |
Report changes only |
-f |
Suppress error messages |
--reference=file |
Use file’s permissions |
--preserve-root |
Protect root directory |
Permission Modes
| Symbol | Meaning |
|---|---|
r |
Read permission (4) |
w |
Write permission (2) |
x |
Execute permission (1) |
u |
User/owner |
g |
Group |
o |
Others |
a |
All (u+g+o) |
Octal Notation
| Octal | Binary | Permissions |
|---|---|---|
| 0 | 000 | — |
| 1 | 001 | –x |
| 2 | 010 | -w- |
| 3 | 011 | -wx |
| 4 | 100 | r– |
| 5 | 101 | r-x |
| 6 | 110 | rw- |
| 7 | 111 | rwx |
Key Use Cases
- Set file permissions
- Make files executable
- Secure sensitive files
- Configure directory access
- Batch permission changes
Examples with Explanations
Example 1: Make File Executable
chmod +x script.shAdds execute permission for all users
Example 2: Set Specific Permissions
chmod 755 file.txtSets rwxr-xr-x permissions (owner: rwx, group/others: r-x)
Example 3: Recursive Directory Permissions
chmod -R 644 /path/to/directorySets rw-r–r– permissions recursively
Understanding Permission Strings
Format: drwxrwxrwx - First character: file type (d=directory, -=file, l=link) - Next 3: owner permissions (rwx) - Next 3: group permissions (rwx) - Last 3: other permissions (rwx)
Common Usage Patterns
Secure private file:
chmod 600 private.txtPublic readable directory:
chmod 755 public_dir/Remove all permissions:
chmod 000 restricted_file
Special Permissions
| Permission | Octal | Description |
|---|---|---|
| Setuid | 4000 | Run as owner |
| Setgid | 2000 | Run as group |
| Sticky bit | 1000 | Restrict deletion |
Performance Analysis
- Minimal performance impact
- Recursive operations can be slow on large directories
- Use find with -exec for complex permission changes
- Consider using parallel processing for large datasets
Additional Resources
Best Practices
- Use least privilege principle
- Be careful with recursive operations
- Test permissions before applying
- Document permission requirements
- Use symbolic notation for clarity
Security Considerations
- Avoid 777 permissions
- Protect sensitive files (600/700)
- Use setuid/setgid carefully
- Regular permission audits
- Monitor permission changes