Linux chmod command

|

What is the chmod Command?

chmod stands for change mode. It defines the three modes of a file or directory: read, write, and execute. When you want to control who can perform these actions on a file, you decide and assign specific permissions to each user.

Syntax:

chmod [options] permissions filename

Steps to Set Permissions Using chmod for Files and Folders:


Step 1: Decide who you want to permit:

u → user (the file owner)

g → group

o → others (everyone else)

a → all (user + group + others)


Step 2: Decide what permission you want to give:

r → read (ability to read the file)

w → write (ability to edit or delete the file)

x → execute (ability to run the file/program)


Step 3: Choose how to set the permissions:

1. Symbolic Method: Use letters (u, g, o) with +, -, =

u → user/owner

g → group

o → others

+ → adds permission

- → removes permission

= → sets exact permission

Example 1:

Command:

chmod u+x file.txt

Explanation:

u+x → Owner can now execute the file.


Example 2:

Command:

chmod g-w file.txt

Explanation:

g-w → Group cannot write to the file anymore.


Example 3:

Command:

chmod o=r file.txt

Explanation:

o=r → It lets others read the file only, removing all other permissions.

Note: This method is useful when you want to change specific permissions without affecting others.

2. Numeric Method: Assign numbers to permissions (r=4, w=2, x=1) and combine them to set access.

Example 1:

Command:

chmod 755 file.txt   # owner=rwx, group=r-x, others=r-x

Explanation:

755 means:

  • 7 = 4+2+1 → rwx
  • 5 = 4+1 → r-x
  • 5 = 4+1 → r-x

When the number 7 is given to the owner, they can read, write, and execute the file, while 5 is given to the group and others, which means they can only read and execute it.

Example 2:

Command:

chmod 644 file.txt   # owner=rw-, group=r--, others=r--

Explanation:

644 means:

  • 6 = 4+2 → rw-
  • 4 = 4 → r--
  • 4 = 4 → r--

When the number 6 is given to the owner, they can read and write the file, while 4 is given to the group and others, which means they can only read the file.

Step 4: Apply the chmod command on the file or folder:

chmod [options] permissions filename

Step 5: Check the permissions to verify:

ls -l file.txt

Output:

-rwxr-xr-x 1 user group 0 Apr 05 12:00 file.txt

ONLINE WEB TOOLS

WhatIsMyIpAddress
Shorterner