chmod = Change Mode
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:
- 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:
chmod u+x file.txt chmod g-w file.txt chmod o=r file.txt
Here, when 7 is set for the owner, it gives full rights to read, write, and execute the file, whereas 5 for the group and others means they are limited to just reading and executing it.
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.
2. 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:
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.
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