The chmod (change mode) command is used to modify file and directory permissions. It controls who can read, write, or execute a file.
๐ Permission Basics
Each file has three permission groups:
- u (user/owner)
- g (group)
- o (others)
And three permission types:
- r (read) โ view file
- w (write) โ modify file
- x (execute) โ run file
โถ๏ธ Basic Syntax
chmod [options] permissions file
๐ข Numeric (Octal) Mode
Permissions use numbers:
4= read2= write1= execute
Example:
chmod 755 script.sh
โ Owner: read, write, execute
โ Group: read, execute
โ Others: read, execute
๐ค Symbolic Mode
Add permission
chmod +x script.sh
Remove permission
chmod -w file.txt
Set exact permissions
chmod u=rwx,g=rx,o=r file.txt
๐ Common Examples
Make a script executable
chmod +x script.sh
Secure a private file
chmod 600 file.txt
Full access for owner only
chmod 700 folder
๐ Apply Recursively
chmod -R 755 /var/www/html
โ Changes permissions for all files and folders inside
โ ๏ธ Common Issues
Permission denied
- File isnโt executable or writable
- Fix:
chmod +x filename
Script wonโt run
- Ensure it has execute permission and correct shebang (
#!/bin/bash)
๐ง When to Use chmod
- Running scripts
- Securing sensitive files
- Fixing permission errors
- Managing web server directories
๐ก Pro Tip
Avoid 777 unless absolutely necessaryโit gives full access to everyone and can create security risks.