chmod Command in Linux

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 TypeMeaning
uuser (owner)
ggroup
oothers
aall users

Types of Permissions

PermissionSymbolMeaning
ReadrCan view file content
WritewCan modify the file
ExecutexCan run the file

How to View File Permissions

Use:

ls -l

Example output:

-rwxr-xr-- 1 root root 1200 Jan 01 test.sh

Breakdown:

  • rwx → Owner permissions
  • r-x → Group permissions
  • r– → Others’ permissions

How chmod Works

You can set permissions using:

  1.  Symbolic method (u, g, o with +, -, =)
  2. Numeric/Octal method (777, 755, 644, etc.)

Let’s learn both.

Symbolic Method Examples

Give execute permission to the user

chmod u+x file.sh

Remove write permission from group

chmod g-w file.txt

Give read and write permission to others

chmod o+rw file.txt

Set exact permissions

chmod u=rwx,g=rx,o=r file.sh

Numeric (Octal) Method

In numeric mode:

PermissionNumber
r4
w2
x1

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.txt

Everyone can read/write/execute.
⚠️ Not safe for production.

755 – Common for folders and scripts

chmod 755 file.sh

Meaning:

  • Owner → rwx
  • Group → r-x
  • Others → r-x

644 – Common for HTML, PHP, text files

chmod 644 index.html

Meaning:

  • Owner → rw-
  • Group → r–
  • Others → r–

Recursive chmod (apply to all files in the folder)

chmod -R 755 /var/www/html

chmod for Directories vs Files

Directories need execute permission to allow entering the directory.

Example:

chmod 755 myfolder

Files don’t need execute permission unless they are scripts or programs.

chmod Practical Examples

Make a script executable

chmod +x backup.sh

Remove all permissions from others

chmod o= file.txt

Give all permissions to the user only

chmod 700 secrets.txt

chmod 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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *