Linux Permission Basics
Understanding File Permissions in Linux
In Linux systems, file permissions are a crucial security mechanism that controls access to files and directories. Every file and directory has three types of permissions: read (r), write (w), and execute (x), which can be set for three different user categories.
Permission Categories
Linux defines three user categories for permissions:
User Category |
Description |
Owner |
The user who created the file |
Group |
Users belonging to the file's group |
Others |
All other users on the system |
Permission Types
Each category can have three permission types:
-
Read (r):
- For files: Allows reading file contents
- For directories: Allows listing directory contents
-
Write (w):
- For files: Allows modifying or deleting the file
- For directories: Allows creating or removing files
-
Execute (x):
- For files: Allows executing the file as a program
- For directories: Allows accessing and traversing the directory
Viewing File Permissions
Use the ls -l
command to view file permissions:
$ ls -l example.txt
-rw-r--r-- 1 user group 1024 May 10 10:30 example.txt
Permission Representation
graph LR
A[File Permissions] --> B[Owner Permissions]
A --> C[Group Permissions]
A --> D[Others Permissions]
B --> E[Read]
B --> F[Write]
B --> G[Execute]
C --> H[Read]
C --> I[Write]
C --> J[Execute]
D --> K[Read]
D --> L[Write]
D --> M[Execute]
Numeric Representation of Permissions
Permissions can also be represented numerically:
Permission |
Numeric Value |
Read (r) |
4 |
Write (w) |
2 |
Execute (x) |
1 |
For example, rw-
would be 6 (4+2), r-x
would be 5 (4+1).
Example of Permission Management
## Change file permissions
$ chmod 644 example.txt ## Owner: read/write, Group/Others: read only
$ chmod u+x script.sh ## Add execute permission for the owner
$ chmod go-w file.txt ## Remove write permission for group and others
Best Practices
- Always follow the principle of least privilege
- Regularly audit and update file permissions
- Use
chmod
carefully to maintain system security
By understanding Linux file permissions, users can effectively manage access and protect sensitive data in their systems. LabEx recommends practicing these concepts in a safe, controlled environment to build practical skills.