Skip to content

Latest commit

ย 

History

History
243 lines (189 loc) ยท 6.12 KB

File metadata and controls

243 lines (189 loc) ยท 6.12 KB

๐Ÿ” Linux Permissions โ€“ Complete Guide

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.


๐Ÿ”น 1. Understanding Linux File Permissions

Permissions in Linux are represented in a 10-character field.

๐Ÿ“Œ Example: -rw-r--r--

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

๐Ÿ”น 2. Viewing File Permissions ๐Ÿ‘€

To check file permissions, use the ls -l command:

ls -l

๐Ÿ“Œ Example Output:

-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

๐Ÿ”น 3. Changing File Permissions ๐Ÿ› ๏ธ

โœ… Method 1: Symbolic Mode

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

๐Ÿ“Œ Examples:

1๏ธโƒฃ Add execute permission for the owner:

chmod u+x file.txt

2๏ธโƒฃ Remove write permission for others:

chmod o-w file.txt

3๏ธโƒฃ Set read and write for the group:

chmod g=rw file.txt

โœ… Method 2: Numeric Mode (Octal Format) ๐Ÿ”ข

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)

๐Ÿ“Œ Examples:

1๏ธโƒฃ Set read and write for the owner, read-only for others:

chmod 644 file.txt

2๏ธโƒฃ Set full permissions for the owner, read/execute for others:

chmod 755 file.txt

๐Ÿ”น 4. Changing File Ownership ๐Ÿท๏ธ (chown Command)

To change file owner and group, use the chown command.

๐Ÿ“Œ Syntax:

chown new_owner:new_group file

๐Ÿ“Œ Examples:

1๏ธโƒฃ Change the owner to john:

chown john file.txt

2๏ธโƒฃ Change the owner to john and group to developers:

chown john:developers file.txt

3๏ธโƒฃ Change all files in a directory recursively:

chown -R john:developers /myfolder

๐Ÿ”น 5. Special Permissions ๐Ÿ”ฅ

1๏ธโƒฃ SetUID (s) โ€“ Execute as File Owner ๐Ÿ‘ค

If 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.sh

2๏ธโƒฃ SetGID (s) โ€“ Execute as Group ๐Ÿ‘ฅ

If 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_folder

3๏ธโƒฃ Sticky Bit (t) โ€“ Protect Files in Shared Folders ๐Ÿ“‚

If 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 /tmp
drwxrwxrwt 10 root root 4096 Feb 9 12:34 /tmp

โœ… The last t indicates the sticky bit is set.


๐Ÿ”น 6. Troubleshooting Permissions & Errors ๐Ÿšจ

1๏ธโƒฃ Permission Denied?

ls -l script.sh

If you see -rw-r--r--, add execute permissions:

chmod +x script.sh

2๏ธโƒฃ Cannot Delete Files in /tmp?
Check if the sticky bit is set:

ls -ld /tmp

3๏ธโƒฃ Find all files owned by a user (john)

find / -user john

4๏ธโƒฃ Find all files with SetUID (s) permissions

find / -perm -4000

๐Ÿ“Š Summary Table of Commands

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)

๐ŸŽฏ Final Thoughts

  • Permissions are crucial for system security ๐Ÿ”.
  • Always follow the principle of least privilege.
  • Use special permissions (SetUID, SetGID, Sticky Bit) when necessary.

โšก