Chmod in Linux: file permissions cheat sheet
File permissions in Linux determine who may read, write and execute files, and the chmod command gives you full control over them. In this guide you'll learn step by step how chmod works, including numeric and symbolic settings, common mistakes and security tips.
1. What are file permissions in Linux?
In Linux and Unix, every file and every directory has certain permissions that determine who has access and what they can do with it. These permissions are divided into three categories:
- Owner (user, u): The user who created the file.
- Group (group, g): Other users who belong to the same group.
- Others (others, o): Everyone who is not the owner and not in the group.
Each file has three permissions:
- r (read): Read a file or view the contents of a directory.
- w (write): Change or delete a file.
- x (execute): Run a file (for example a script or program).
You can view a file's permissions with:
ls -l file.txt
This shows, for example:
-rw-r--r-- 1 user group 1234 Mar 17 10:00 file.txt
Here you see:
- rw- (read and write for the owner)
- r-- (read only for the group)
- r-- (read only for others)
2. Basic use of chmod
The general syntax of chmod is:
chmod [options] permissions file
Examples:
- chmod 755 script.sh → makes a script executable for everyone.
- chmod 644 document.txt → only the owner can write, others can read.
3. Numeric mode: chmod with numbers
You can set permissions with octal notation (numbers). Each permission has a numeric value:
- r = 4 (read)
- w = 2 (write)
- x = 1 (execute)
Combinations:
- 7 (rwx) → Read, write and execute.
- 6 (rw-) → Read and write.
- 5 (r-x) → Read and execute.
- 4 (r--) → Read only.
Example:
chmod 755 script.sh
Here the owner gets full permissions (rwx = 7), while the group and others only get read and execute permissions (r-x = 5).
4. Symbolic mode: chmod with letters
Instead of numbers, you can also change permissions with symbols:
- u (user, the owner)
- g (group)
- o (others, other users)
- a (all, everyone)
And with operators:
- + (add permissions)
- - (remove permissions)
- = (set permissions exactly)
Examples:
chmod u+x script.sh # Gives the owner execute permission chmod g-w file.txt # Removes write permission from the group chmod o= file.txt # Removes all permissions from others 5. Common chmod settings
Here are some standard settings and when to use them:
| chmod code | Description | Use case |
|---|---|---|
| 755 | Owner has everything, others only read/execute | Scripts and programs |
| 644 | Owner may write, others may read | Configuration files, web pages |
| 700 | Only the owner has access | Private files, SSH keys |
| 600 | Only the owner may read/write | Sensitive files such as passwords |
| 777 | Everyone has full permissions (dangerous!) | Only in exceptional cases |
6. Advanced options
a) Using chmod recursively (-R)
Do you want to apply chmod to an entire directory, including its subdirectories and files? Use the -R option:
chmod -R 755 mydir/
This makes sure that all files and subdirectories inside mydir/ get the same permissions.
b) SUID, SGID and the sticky bit
In addition to the standard permissions, there are three special bits:
- SUID (Set User ID, 4xxx): Always runs a file as its owner.
- SGID (Set Group ID, 2xxx): New files in a directory automatically get the directory's group.
- Sticky bit (1xxx): Prevents others from deleting files in a shared directory.
Example:
chmod 4755 script.sh # Set SUID chmod 2755 mydir/ # Set SGID chmod 1777 /tmp # Set the sticky bit (common for the /tmp directory) 7. Practical applications
a) Making a script executable
Do you have a script you want to run? Give it execute permission:
chmod +x myscript.sh ./myscript.sh
b) Protecting files against changes
Do you want to protect a file against unwanted changes?
chmod 444 important.txt # Read-only for everyone
c) Correct permissions for SSH keys
SSH keys require specific permissions:
chmod 600 ~/.ssh/id_rsa # Private key (only the owner may read/write) chmod 644 ~/.ssh/id_rsa.pub # Public key (everyone may read) 8. Common mistakes and security risks
a) Why chmod 777 is usually a bad idea
Giving full permissions (chmod 777) to a file or directory means that everyone may do anything with it. This is a major security risk, especially on web servers:
chmod 777 /var/www/html/index.php # Wrong! Anyone can modify this file.
Better:
chmod 644 /var/www/html/index.php # Only the owner can write.
b) Common mistakes
- Forgetting to give a script execute permission:
Solution:chmod +x script.sh - Wrong permissions on SSH keys:
Solution:chmod 600 ~/.ssh/id_rsa
9. Extra tools for managing permissions
Besides chmod, there are other handy commands:
- chown: Changes the owner of a file.
chown user:group file.txt - chgrp: Changes only the group of a file.
chgrp groupname file.txt - umask: Determines the default permissions when new files are created.
umask 022 # New files get 644 by default, directories 755
10. Conclusion: best practices
- Be careful with chmod 777: only use it when it's truly necessary.
- Use numeric or symbolic notation depending on the situation.
- Make scripts executable with chmod +x.
- Protect sensitive files such as SSH keys with chmod 600.
- Only use chmod -R if you're sure what you're doing.
With this knowledge you can manage file permissions in Linux safely and effectively. 🚀
Looking for a dedicated server? See what we offer.
0 van 0 vonden dit nuttig