Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
The chmod
(Change Mode) command is used in Linux to modify file and directory permissions. It controls who can read (r), write (w), and execute (x) a file or directory.
Understanding Permissions in Linux
Each file and directory in Linux has three types of users:
- Owner (u) โ The person who created the file.
- Group (g) โ Users who belong to the same group.
- Others (o) โ Everyone else.
Each file has permission settings like:
-rwxr-xr-- 1 user group 1234 Jan 30 12:00 example.sh
r
โ Readw
โ Writex
โ Execute-
โ No permission
Now, let’s explore 10 powerful chmod
commands with real-world explanations.
1. Give Read, Write, and Execute Permissions to the Owner
chmod u+rwx file.txt
โ What it does:
- Grants read (r), write (w), and execute (x) permissions to the owner (
u
). - Does not affect group (
g
) or others (o
).
๐ Example Before (ls -l file.txt
):
-r--r--r-- 1 user group 1234 Jan 30 12:00 file.txt
๐ Example After:
-rwxr--r-- 1 user group 1234 Jan 30 12:00 file.txt
2. Give Execute Permission to Everyone
chmod +x script.sh
โ What it does:
- Grants execute (
x
) permission to the owner, group, and others.
๐ Before:
-rw-r--r-- script.sh
๐ After:
-rwxr-xr-x script.sh
๐ก Now you can run ./script.sh
instead of bash script.sh
.
3. Remove Write Permission from Others
chmod o-w file.txt
โ What it does:
- Removes write (
w
) permission from others (o
).
๐ Before:
-rw-rw-rw- file.txt
๐ After:
-rw-rw-r-- file.txt
4. Set Full Permissions for Owner and Read-Only for Others
chmod 744 file.txt
โ What it does:
7
(Owner) โ Read (4) + Write (2) + Execute (1) = rwx4
(Group) โ Read (4) = r–4
(Others) โ Read (4) = r–
๐ Before:
-rw-rw-rw- file.txt
๐ After:
-rwxr--r-- file.txt
5. Give All Permissions to Everyone
chmod 777 file.txt
โ What it does:
7
(Owner) โ rwx7
(Group) โ rwx7
(Others) โ rwx
๐ Before:
-rw-r--r-- file.txt
๐ After:
-rwxrwxrwx file.txt
๐ก Warning: This makes the file executable by anyone, which is risky!
6. Remove All Permissions from Group and Others
chmod 700 file.txt
โ What it does:
7
(Owner) โ rwx0
(Group) โ No permissions0
(Others) โ No permissions
๐ Before:
-rw-r--r-- file.txt
๐ After:
-rwx------ file.txt
๐ก Best for private files!
7. Make a Directory Accessible by Everyone
chmod 755 mydir
โ What it does:
7
(Owner) โ rwx (Full control)5
(Group) โ r-x (Read & Execute)5
(Others) โ r-x (Read & Execute)
๐ Before:
drwxr-x--- mydir
๐ After:
drwxr-xr-x mydir
๐ก Great for public directories, allowing others to enter (x
).
8. Remove Execute Permission from a Directory
chmod a-x mydir
โ What it does:
- Removes
x
(execute permission) from all users (a
=ugo
). - Now, users cannot enter the directory.
๐ Before:
drwxr-xr-x mydir
๐ After:
drw-r--r-- mydir
๐ก Useful for restricting access while keeping files readable.
9. Apply Permissions Recursively to All Files and Folders
chmod -R 755 /var/www/html/
โ What it does:
-R
applies changes recursively to all files and subdirectories in/var/www/html/
.
๐ Example: Before:
-rw-r--r-- index.html
-rw------- config.php
drwx------ private/
After:
-rwxr-xr-x index.html
-rwxr-xr-x config.php
drwxr-xr-x private/
๐ก Commonly used for web server files.
10. Set a File to be Read-Only for Everyone
chmod 444 file.txt
โ What it does:
4
(Owner) โ Read only (r–)4
(Group) โ Read only (r–)4
(Others) โ Read only (r–)
๐ Before:
-rw-rw-rw- file.txt
๐ After:
-r--r--r-- file.txt
๐ก Useful for protecting important system files.
Final Thoughts
chmod
is essential for Linux security.- Use symbolic (
u, g, o, a
) and numeric (777, 755, 644
) modes wisely. - Never use
chmod 777
on sensitive files unless absolutely necessary!