Linux File Permissions Explained
Linux file permissions control who can read, modify, or execute files and directories. Understanding permissions is essential for managing Linux systems securely and preventing unauthorized access.
What are Linux File Permissions?
Linux assigns permissions to files and directories based on the owner, group, and other users. Each permission determines what a particular user category can do with a resource.
-rwxr-xr-- 1 user developers 2048 app.sh
Types of Linux Permissions
- Read (r) allows a file to be viewed
- Write (w) allows a file to be modified
- Execute (x) allows a file to be executed
Linux Permission Categories
- User represents the file owner
- Group represents users assigned to the file group
- Others represents all remaining users
How to Check File Permissions
The ls -l command displays file permissions, ownership, size, and other information.
ls -l
-rw-r--r-- 1 user developers 1200 notes.txt
Understanding Permission Notation
rwx | r-x | r--
User Group Others
The first permission group applies to the owner, the second applies to the group, and the third applies to other users.
Using chmod
The chmod command changes file and directory permissions.
chmod 755 script.sh
chmod u+x script.sh
chmod 644 notes.txt
Numeric Linux Permissions
| Permission | Value | Meaning |
|---|---|---|
| Read | 4 | View or read data |
| Write | 2 | Modify data |
| Execute | 1 | Execute or access |
| None | 0 | No permission |
Common chmod Examples
chmod 600 private.txt
chmod 644 document.txt
chmod 755 script.sh
chmod 700 secure-folder
Using chown
The chown command changes the owner and group associated with a file or directory.
sudo chown alice file.txt
sudo chown alice:developers file.txt
File Permissions vs Directory Permissions
| Permission | File | Directory |
|---|---|---|
| Read | View contents | List contents |
| Write | Modify contents | Create or delete entries |
| Execute | Run the file | Enter/access the directory |
Common Permission Examples
- 644 is commonly used for regular files
- 755 is commonly used for executable scripts and directories
- 600 restricts access to the owner
- 700 gives full access only to the owner
Common Mistakes to Avoid
- Using chmod 777 unnecessarily
- Changing ownership without understanding the impact
- Giving write access to too many users
- Ignoring sensitive configuration files
- Running every command as root
Advanced Linux Permission Concepts
- Special permissions
- Setuid
- Setgid
- Sticky bit
- Access Control Lists
- Default directory permissions
Practice Exercises
- Create a file and inspect its permissions
- Change permissions using chmod
- Change ownership using chown
- Create a shared directory for a group
- Practice restrictive permissions on sensitive files
Conclusion
Linux file permissions provide a simple but powerful mechanism for controlling access to files and directories. Learning chmod, chown, ownership, and permission notation is essential for Linux administration and system security.