Account
Categories

chmod command


Definition:

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

Set Permissions with chmod (Step-by-Step 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 by Symbolic or Numeric Method

List of permissions:

  • 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: Symbolic Method

Use letters (u, g, o) with +, -, =:

  • u → user/owner
  • g → group
  • o → others
  • + → adds permission
  • - → removes permission
  • = → sets exact permission

Example:

chmod u+x file.txt
chmod g-w file.txt
chmod o=r file.txt

Explanation:

  • u+x → Owner can now execute the file.
  • g-w → Group cannot write to the file anymore.
  • 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.

Step 3: Numeric Method

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

Examples:

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

Explanation:

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

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 will show something like:

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