Linux File Permissions Explained | Complete Guide with Octal Cheatsheet and Commands
Learn everything about Linux file permissions, including symbolic and octal notations, user-group-other levels, SUID, SGID, Sticky Bit, and chmod usage. This guide includes a visual cheatsheet, best practices, and real command-line examples.
Table of Contents
- What Are Linux File Permissions?
- File Permission Structure
- Octal (Numeric) Representation of Permissions
- Special File Permissions: SUID, SGID, Sticky Bit
- Changing Permissions Using chmod
- Viewing Permissions with ls -l
- Best Practices for Managing Permissions
- Common Permission Use Cases
- Conclusion
- Frequently Asked Questions (FAQs)
Understanding Linux file permissions is essential for system administrators, ethical hackers, developers, and cybersecurity professionals. Proper file permissions protect sensitive data, prevent unauthorized access, and define who can read, write, or execute files or directories. In this blog, we’ll break down everything from permission notations to special bits like SUID, SGID, and the Sticky Bit, using real-world examples and terminal commands.
What Are Linux File Permissions?
Linux file permissions determine the level of access users have to a particular file or directory. These permissions are assigned to three categories of users:
-
Owner: The user who owns the file
-
Group: Users who belong to the file's group
-
Others: Everyone else
Each of these categories can be assigned three types of permissions:
| Permission | Symbol | Meaning |
|---|---|---|
| Read | r | View file contents or list directory contents |
| Write | w | Modify file contents or create/delete files in a directory |
| Execute | x | Run a file as a program or enter a directory |
File Permission Structure
A typical Linux permission string looks like:
-rwxr-xr--
Here’s how to read it:
-
First character:
-indicates a regular file;dis for directories. -
Next 3:
rwx— Owner permissions (read, write, execute) -
Next 3:
r-x— Group permissions (read, execute) -
Last 3:
r--— Other permissions (read only)
Octal (Numeric) Representation of Permissions
Permissions can also be represented in binary or octal:
| Binary | Octal | Symbol | Meaning |
|---|---|---|---|
| 000 | 0 | --- | No permissions |
| 001 | 1 | --x | Execute only |
| 010 | 2 | -w- | Write only |
| 011 | 3 | -wx | Write + Execute |
| 100 | 4 | r-- | Read only |
| 101 | 5 | r-x | Read + Execute |
| 110 | 6 | rw- | Read + Write |
| 111 | 7 | rwx | Read + Write + Execute |
So a permission string like rwxr-xr-- is 754 in octal.
Special File Permissions: SUID, SGID, Sticky Bit
Linux offers three special bits that can alter how files are accessed and executed:
SUID (Set User ID)
Allows users to run an executable with the permissions of the file owner.
-
Symbol:
sin the owner execute field -
Example:
-rwsr-xr-x -
Command:
chmod u+s filename
SGID (Set Group ID)
Files: Similar to SUID, but applies to group permissions.
Directories: Newly created files inherit the group of the directory.
-
Symbol:
sin the group execute field -
Example:
-rwxr-sr-x -
Command:
chmod g+s directory
Sticky Bit
Applied to directories to restrict file deletion within them to file owners.
-
Symbol:
tin the other execute field -
Example:
drwxrwxrwt -
Command:
chmod +t directory
Changing Permissions Using chmod
Linux provides the chmod command to change permissions in both symbolic and octal format.
✅ Symbolic Example:
chmod u+x script.sh # Add execute to user
chmod g-w script.sh # Remove write from group
chmod o=r file.txt # Set read-only for others
✅ Octal Example:
chmod 755 filename # rwxr-xr-x
chmod 700 filename # rwx------
chmod 644 file.txt # rw-r--r--
Viewing Permissions with ls -l
ls -l
Sample Output:
-rwxr-xr-- 1 user group 2048 Jun 19 12:30 example.sh
Breakdown:
-
-rwxr-xr--: Permissions -
1: Number of hard links -
user: File owner -
group: File’s group -
2048: File size in bytes -
Jun 19 12:30: Last modified -
example.sh: Filename
Best Practices for Managing Permissions
-
Always follow the principle of least privilege.
-
Avoid using
777unless absolutely necessary. -
Use SUID/SGID/Sticky Bit carefully to avoid privilege escalation.
-
Use groups to manage multiple users with similar permissions.
✅ Common Permission Use Cases
| Use Case | Command |
|---|---|
| Make script executable | chmod +x script.sh |
| Make file read-only | chmod 444 filename.txt |
| Allow full access | chmod 777 testfile |
| Secure config file | chmod 600 config.ini |
| Set SUID | chmod u+s /path/to/file |
| Set SGID | chmod g+s /path/to/dir |
| Set Sticky Bit | chmod +t /tmp/shared_dir |
Conclusion
Linux file permissions are one of the foundational elements of system security. With a deep understanding of permission notations, octal values, and special bits like SUID, SGID, and the Sticky Bit, you'll be equipped to secure any Linux environment effectively. Use the cheatsheet provided to reinforce your knowledge and apply best practices for file access control.
FAQs
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0