How to Change File Permissions in Linux: Simple Guide
To change file permissions in Linux, use the
chmod command followed by permission settings and the file name. Permissions can be set using symbolic (e.g., u+rwx) or numeric modes (e.g., 755).Syntax
The basic syntax of the chmod command is:
chmod [options] mode file
Here, mode defines the permissions to set, and file is the target file or directory.
Modes can be symbolic (like u+rwx) or numeric (like 755).
bash
chmod [options] mode file
Example
This example shows how to give the owner full permissions and others read and execute permissions on a file named script.sh using numeric mode.
bash
chmod 755 script.sh
ls -l script.shOutput
-rwxr-xr-x 1 user user 0 Jun 10 12:00 script.sh
Common Pitfalls
Common mistakes include:
- Using incorrect numeric codes that don't match desired permissions.
- Forgetting to specify the file name.
- Not having the required permissions to change the file mode.
- Confusing symbolic operators like
+,-, and=.
Example of wrong and right usage:
bash
chmod 777 script.sh # Gives all permissions to everyone (usually unsafe)
chmod u=rwx,g=rx,o=rx script.sh # Safer, explicit permissionsQuick Reference
| Symbolic Mode | Meaning | Numeric Mode |
|---|---|---|
| u | User (owner) | |
| g | Group | |
| o | Others | |
| r | Read permission | 4 |
| w | Write permission | 2 |
| x | Execute permission | 1 |
| + | Add permission | |
| - | Remove permission | |
| = | Set exact permission | |
| 7 | rwx (read, write, execute) | 7 |
| 6 | rw- (read, write) | 6 |
| 5 | r-x (read, execute) | 5 |
| 4 | r-- (read only) | 4 |
| 0 | --- (no permission) | 0 |
Key Takeaways
Use the chmod command with symbolic or numeric modes to change file permissions.
Numeric mode uses digits 0-7 to represent read, write, and execute permissions.
Symbolic mode uses letters u, g, o and operators +, -, = to modify permissions.
Always verify permissions with ls -l after changing them.
Avoid using overly permissive settings like 777 for security reasons.