Permissions in Linux control what actions users can perform on files and directories. They define whether a user can read, write, or execute files and modify directory contents.
Permissions in Linux are represented in a 10-character field.
| Position | Meaning | Description |
|---|---|---|
- |
File Type | - (Regular File), d (Directory), l (Symbolic Link) |
rw- |
Owner (User) Permissions | Read (r), Write (w), Execute (x) |
r-- |
Group Permissions | Read (r), Write (w), Execute (x) |
r-- |
Others Permissions | Read (r), Write (w), Execute (x) |
๐ Breakdown of Characters:
- Owner (User):
rw-โ Read & Write access (โ No execute permission). - Group:
r--โ Read-only access (โ No write or execute). - Others:
r--โ Read-only access (โ No write or execute).
To check file permissions, use the ls -l command:
ls -l-rw-r--r-- 1 user group 4096 Jan 07 10:00 file.txt
| Field | Meaning |
|---|---|
-rw-r--r-- |
File permissions |
1 |
Number of links |
user |
File owner |
group |
File group |
4096 |
File size (bytes) |
Jan 07 10:00 |
Last modification date |
file.txt |
File name |
Syntax:
chmod <user_class><operation><permissions> file| User Class | Meaning |
|---|---|
u |
Owner (User) |
g |
Group |
o |
Others |
a |
All (Owner, Group, Others) |
| Operation | Action |
|---|---|
+ |
Add permission |
- |
Remove permission |
= |
Set exact permissions |
| Permissions | Description |
|---|---|
r |
Read |
w |
Write |
x |
Execute |
1๏ธโฃ Add execute permission for the owner:
chmod u+x file.txt2๏ธโฃ Remove write permission for others:
chmod o-w file.txt3๏ธโฃ Set read and write for the group:
chmod g=rw file.txt| Permission | Numeric Value |
|---|---|
Read (r) |
4 |
Write (w) |
2 |
Execute (x) |
1 |
๐น Each permission group (Owner, Group, Others) is represented as a 3-digit number.
| Permission | Owner (User) | Group | Others |
|---|---|---|---|
chmod 644 |
Read & Write (6) |
Read-only (4) |
Read-only (4) |
chmod 755 |
Full (7) |
Read & Execute (5) |
Read & Execute (5) |
chmod 777 |
Full (7) |
Full (7) |
Full (7) |
1๏ธโฃ Set read and write for the owner, read-only for others:
chmod 644 file.txt2๏ธโฃ Set full permissions for the owner, read/execute for others:
chmod 755 file.txtTo change file owner and group, use the chown command.
chown new_owner:new_group file1๏ธโฃ Change the owner to john:
chown john file.txt2๏ธโฃ Change the owner to john and group to developers:
chown john:developers file.txt3๏ธโฃ Change all files in a directory recursively:
chown -R john:developers /myfolderIf the SetUID (set user ID) bit is set, the file executes with the ownerโs permissions, not the userโs.
๐น Example: Allow normal users to execute ping with root privileges:
ls -l /bin/ping-rwsr-xr-x 1 root root 44160 Feb 9 12:34 /bin/ping
๐น Set SetUID on a script:
chmod u+s script.shIf the SetGID (set group ID) bit is set, the file executes with the groupโs permissions.
๐น Example: Allow all users in developers to share files:
chmod g+s /shared_folderIf the sticky bit is set on a directory, only the fileโs owner can delete their files.
๐น Example: Set a sticky bit on /tmp to prevent file deletion by other users:
chmod +t /tmp๐น Verification:
ls -ld /tmpdrwxrwxrwt 10 root root 4096 Feb 9 12:34 /tmp
โ
The last t indicates the sticky bit is set.
1๏ธโฃ Permission Denied?
ls -l script.shIf you see -rw-r--r--, add execute permissions:
chmod +x script.sh2๏ธโฃ Cannot Delete Files in /tmp?
Check if the sticky bit is set:
ls -ld /tmp3๏ธโฃ Find all files owned by a user (john)
find / -user john4๏ธโฃ Find all files with SetUID (s) permissions
find / -perm -4000| Command | Description |
|---|---|
chmod 755 file.txt |
Set read/write/execute for owner, read/execute for group/others |
chown john file.txt |
Change file owner to john |
chown john:dev file.txt |
Change owner to john and group to dev |
chmod u+s script.sh |
Enable SetUID (execute as owner) |
chmod g+s folder/ |
Enable SetGID (execute as group) |
chmod +t /shared |
Enable Sticky Bit (prevent deletion by others) |
- Permissions are crucial for system security ๐.
- Always follow the principle of least privilege.
- Use special permissions (SetUID, SetGID, Sticky Bit) when necessary.