File Permissions (chmod)
فهم وإدارة صلاحيات الملفات (Permissions) في لينكس (Read, Write, Execute) للـ (User, Group, Other).
ملخص المحاضرة
🐧 Lecture 6: File Permissions (chmod)
This lecture covers the crucial topic of file permissions in Linux, explaining what they mean and how to change them using the chmod command.
The Permission Structure
Every file and directory in Linux has permissions set for three distinct "actors":
- Owner (User): The user who created or owns the file.
- Group: The specific group that has permissions on the file.
- Others (All): Every other user on the system.
Each actor can be granted three types of permissions:
- Read (r): Ability to read a file's contents or list a directory's contents (
ls). - Write (w): Ability to modify or delete a file or add/delete files within a directory.
- Execute (x): Ability to run a file as a program or enter a directory (
cd).
Reading Permissions (ls -l)
When you type ls -l, you see permissions like -rwxr-xr--:
- First Character: The file type.
dis a directory,-is a regular file. - Chars 2-4 (rwx): Permissions for the Owner (User).
- Chars 5-7 (r-x): Permissions for the Group.
- Chars 8-10 (r--): Permissions for Others.
Changing Permissions (chmod)
The chmod command is used to change permissions in two ways:
1. Symbolic Mode (Letters) This mode uses letters to represent who, what action, and what permission.
- Who:
u(user),g(group),o(other),a(all). - Action:
+(add),-(remove),=(set exactly). - Permission:
r(read),w(write),x(execute). - Examples:
chmod g+x file.txt: Adds execute permission for the group.chmod u-w file.txt: Removes write permission from the user.chmod ug-x script.sh: Removes execute permission from both user and group.chmod ugo+r file.txt: Adds read permission for everyone.
2. Absolute Mode (Numbers) This mode uses octal (base-8) numbers to represent the permissions for each actor.
r(read) = 4w(write) = 2x(execute) = 1---(none) = 0
You add the numbers for each actor:
-
rwx= 4 + 2 + 1 = 7 -
rw-= 4 + 2 + 0 = 6 -
r-x= 4 + 0 + 1 = 5 -
r--= 4 + 0 + 0 = 4 -
-wx= 0 + 2 + 1 = 3 -
-w-= 0 + 2 + 0 = 2 -
--x= 0 + 0 + 1 = 1 -
Example 1:
chmod 755 index.html- User: 7 (rwx)
- Group: 5 (r-x)
- Other: 5 (r-x)
- Result:
-rwxr-xr-x
-
Example 2:
chmod 644 index.html- User: 6 (rw-)
- Group: 4 (r--)
- Other: 4 (r--)
- Result:
-rw-r--r--