Linux

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 : Group
  • o : Others (everyone else)
  • a : All (equivalent to ugo)
  • + : Add permission
  • - : Remove permission
  • = : Set permission explicitly
  • Numeric Method:
  • 0 : No permission
  • 1 : Execute permission
  • 2 : Write permission
  • 4 : Read permission

Basic Permissions:

  • Read (r): 4
  • Write (w): 2
  • Execute (x): 1

Examples:

  1. Give Read and Write Permissions to Owner:
   chmod u+rw filename
  1. Remove Execute Permission from Group:
   chmod g-x filename

Advanced Usage:

  1. Recursively Change Permissions for Directories and Files:
   chmod -R permissions directory
  1. Set Default Permissions for Newly Created Files:
   umask 022
  1. Symbolic Method with Multiple Permissions:
   chmod u+r,g+w,o-x filename
  1. Numeric Method with Multiple Permissions:
   chmod 764 filename
  1. Set SUID, SGID, or Sticky Bit:
  • SUID (Set User ID): 4
  • SGID (Set Group ID): 2
  • Sticky Bit: 1
   chmod +s filename
  1. Combine Symbolic and Numeric Methods:
   chmod u+x,g=rw,o-r filename
  1. Copy Permissions from One File to Another:
   chmod --reference=reference_file target_file
  1. 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.