In this guide, we will explain the chmod command in Linux. Managing file permissions is one of the most important tasks in Linux. Whether you are a developer, system administrator, or a regular user, you must know how to control who can read, write, or execute a file. This is where the chmod command comes in.
What is chmod?
chmod stands for Change Mode.
It is used to change the permissions of a file or directory in Linux.
Permissions decide who can:
- Read (r) → View the file or list directory
- Write (w) → Edit the file or create/delete files inside a directory
- Execute (x) → Run the file (if it’s a script or program)
Types of Users in Linux
Permissions are set for three types of users:
| User Type | Meaning |
|---|---|
| u | user (owner) |
| g | group |
| o | others |
| a | all users |
Types of Permissions
| Permission | Symbol | Meaning |
|---|---|---|
| Read | r | Can view file content |
| Write | w | Can modify the file |
| Execute | x | Can run the file |
How to View File Permissions
Use:
ls -lExample output:
-rwxr-xr-- 1 root root 1200 Jan 01 test.shBreakdown:
- rwx → Owner permissions
- r-x → Group permissions
- r– → Others’ permissions
How chmod Works
You can set permissions using:
- Symbolic method (u, g, o with +, -, =)
- Numeric/Octal method (777, 755, 644, etc.)
Let’s learn both.
Symbolic Method Examples
Give execute permission to the user
chmod u+x file.shRemove write permission from group
chmod g-w file.txtGive read and write permission to others
chmod o+rw file.txtSet exact permissions
chmod u=rwx,g=rx,o=r file.shNumeric (Octal) Method
In numeric mode:
| Permission | Number |
|---|---|
| r | 4 |
| w | 2 |
| x | 1 |
You add numbers to create permissions.
Common permission values:
- 7 = 4+2+1 → rwx
- 6 = 4+2 → rw
- 5 = 4+1 → r-x
- 4 = read only
- 0 = no permissions
Common chmod Commands
777 – Full permission for everyone
chmod 777 file.txtEveryone can read/write/execute.
⚠️ Not safe for production.
755 – Common for folders and scripts
chmod 755 file.shMeaning:
- Owner → rwx
- Group → r-x
- Others → r-x
644 – Common for HTML, PHP, text files
chmod 644 index.htmlMeaning:
- Owner → rw-
- Group → r–
- Others → r–
Recursive chmod (apply to all files in the folder)
chmod -R 755 /var/www/htmlchmod for Directories vs Files
Directories need execute permission to allow entering the directory.
Example:
chmod 755 myfolderFiles don’t need execute permission unless they are scripts or programs.
chmod Practical Examples
Make a script executable
chmod +x backup.shRemove all permissions from others
chmod o= file.txtGive all permissions to the user only
chmod 700 secrets.txtchmod Security Tips
- Use 755 for directories
- Use 644 for files
- Use 600 for private keys
- Avoid 777 unless very necessary
- Always check permissions with ls -l
Conclusion
The chmod command is essential for controlling access to files and directories in Linux. By understanding permission types and using numeric or symbolic formats, you can easily secure your system and manage access exactly the way you want.
