CHMOD Cheat Sheet
Here’s a usage cheat sheet for the chmod
command in Linux, covering basic to advanced usage:
Basic Usage:
- Syntax:
chmod permissions filename
Permission Syntax:
- Symbolic Method:
u
: User (owner)g
: Groupo
: Others (everyone else)a
: All (equivalent tougo
)+
: Add permission-
: Remove permission=
: Set permission explicitly- Numeric Method:
0
: No permission1
: Execute permission2
: Write permission4
: Read permission
Basic Permissions:
- Read (r):
4
- Write (w):
2
- Execute (x):
1
Examples:
- Give Read and Write Permissions to Owner:
chmod u+rw filename
- Remove Execute Permission from Group:
chmod g-x filename
Advanced Usage:
- Recursively Change Permissions for Directories and Files:
chmod -R permissions directory
- Set Default Permissions for Newly Created Files:
umask 022
- Symbolic Method with Multiple Permissions:
chmod u+r,g+w,o-x filename
- Numeric Method with Multiple Permissions:
chmod 764 filename
- Set SUID, SGID, or Sticky Bit:
- SUID (Set User ID):
4
- SGID (Set Group ID):
2
- Sticky Bit:
1
chmod +s filename
- Combine Symbolic and Numeric Methods:
chmod u+x,g=rw,o-r filename
- Copy Permissions from One File to Another:
chmod --reference=reference_file target_file
- Set ACL (Access Control List):
setfacl -m u:username:permissions filename
Remember to always be cautious when using chmod
as incorrect permissions can lead to security vulnerabilities or unexpected behavior. It’s essential to understand the implications of changing permissions before applying them.