chmod Command Guide
The chmod (change mode) command controls file permissions in Linux. Learn how to manage read, write, and execute permissions effectively.
7 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Symbolic
chmod u+x fileAdd execute for ownerchmod g-w fileRemove write for groupchmod o=r fileSet read only for othersOctal
chmod 755 filerwxr-xr-x (executable)chmod 644 filerw-r--r-- (normal file)chmod 600 filerw------- (private)Common
chmod +x script.shMake executablechmod -R 755 dir/Recursive changechmod a+r fileReadable by allSpecial
chmod u+s fileSet SUID bitchmod g+s dirSet SGID bitchmod +t dirSet sticky bitDownloadable Image Preview
Failed to generate preview
Understanding Permissions
Linux file permissions are divided into three categories for three groups:
Permission Types
| r (read) | View file contents or list directory |
| w (write) | Modify file or create/delete in directory |
| x (execute) | Run file or access directory |
Permission Groups
| u (user) | The file owner |
| g (group) | Users in the file group |
| o (others) | All other users |
| a (all) | All users (u, g, and o) |
View current permissions with ls -l:
bash
ls -l file.txt
-rw-r--r-- 1 user group 1024 Jan 15 10:30 file.txtSymbolic Mode
Symbolic mode uses letters to modify permissions.
Add permission (+)
bash
# Add execute for owner
chmod u+x script.sh
# Add read for group
chmod g+r file.txt
# Add write for others
chmod o+w file.txtRemove permission (-)
bash
# Remove write from group
chmod g-w file.txt
# Remove all permissions for others
chmod o-rwx file.txtSet exact permission (=)
bash
# Set exact permissions for user
chmod u=rwx file.txt
# Set read-only for all
chmod a=r file.txtCombined operations
bash
# Multiple changes at once
chmod u+x,g-w,o-rwx file.txt
# Same permission for multiple groups
chmod ug+x file.txtOctal (Numeric) Mode
Octal mode uses numbers to set all permissions at once.
Octal Values
| 4 | Read (r) |
| 2 | Write (w) |
| 1 | Execute (x) |
| 0 | No permission |
Add the values for each permission group (owner, group, others):
bash
# 755 = rwxr-xr-x (owner: all, group/others: read+execute)
chmod 755 script.sh
# 644 = rw-r--r-- (owner: read+write, group/others: read)
chmod 644 file.txt
# 700 = rwx------ (owner: all, group/others: none)
chmod 700 private.txt
# 777 = rwxrwxrwx (everyone: all) - Use with caution!
chmod 777 file.txtCommon Octal Permissions
| 755 | rwxr-xr-x - Standard for executables |
| 644 | rw-r--r-- - Standard for files |
| 700 | rwx------ - Private executables |
| 600 | rw------- - Private files |
| 775 | rwxrwxr-x - Group writable |
| 664 | rw-rw-r-- - Group writable files |
Warning
Avoid using
chmod 777 as it gives everyone full access. Use the minimum permissions necessary.Recursive Changes
Use -R to change permissions recursively.
bash
# Change all files in a directory
chmod -R 755 /path/to/directory
# Make all scripts executable
chmod -R u+x scripts/Tip
When applying permissions recursively, consider using find to target only files or directories:
bash
# Set 755 for directories only
find /path -type d -exec chmod 755 {} \;
# Set 644 for files only
find /path -type f -exec chmod 644 {} \;Practical Examples
Make a script executable
bash
chmod +x script.sh
# or
chmod 755 script.shProtect sensitive files
bash
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pubWeb server permissions
bash
# HTML files readable by all
chmod 644 /var/www/html/*.html
# CGI scripts executable
chmod 755 /var/www/cgi-bin/*.cgiShared directory for a group
bash
chmod 775 /shared/project
chmod g+s /shared/project # Set group ID bitSpecial Permissions
Special Permission Bits
| setuid (4000) | Run as file owner |
| setgid (2000) | Run as file group |
| sticky (1000) | Only owner can delete |
bash
# Set setuid
chmod u+s file
chmod 4755 file
# Set setgid on directory
chmod g+s directory
chmod 2755 directory
# Set sticky bit (common on /tmp)
chmod +t directory
chmod 1755 directoryInfo
The sticky bit on a directory prevents users from deleting files they don't own, even if they have write permission on the directory.
Summary
Understanding chmod is essential for Linux system administration. Key takeaways:
- Use symbolic mode (u+x) for simple changes
- Use octal mode (755) for setting complete permissions
- Common patterns: 755 for scripts, 644 for files, 700/600 for private
- Use
-Rfor recursive changes - Always use minimum necessary permissions for security