,

Top 10 Commands with explanation of “chmod” command

Posted by

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

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:

  1. Owner (u) โ€“ The person who created the file.
  2. Group (g) โ€“ Users who belong to the same group.
  3. Others (o) โ€“ Everyone else.

Each file has permission settings like:

-rwxr-xr-- 1 user group  1234 Jan 30 12:00 example.sh
  • r โ†’ Read
  • w โ†’ Write
  • x โ†’ 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) = rwx
  • 4 (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) โ†’ rwx
  • 7 (Group) โ†’ rwx
  • 7 (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) โ†’ rwx
  • 0 (Group) โ†’ No permissions
  • 0 (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!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x