Table of Contents
- What Are Linux Permissions?
- Permission Types: Read, Write, Execute
- Permission Classes: User, Group, Others
- Permission Notation: Symbolic vs. Octal
- Viewing Permissions with
ls -l - Changing Permissions:
chmodCommand - Changing Ownership:
chownandchgrp - Special Permissions: SUID, SGID, and Sticky Bit
- Practical Scenarios: Applying Permissions in Real Life
- Troubleshooting Permission Issues
- Conclusion
- References
1. What Are Linux Permissions?
Linux is a multi-user operating system, meaning multiple users can interact with the same system simultaneously. Permissions are a core security mechanism that defines who can do what with a file or directory. They prevent unauthorized access, data tampering, and accidental deletion, ensuring the integrity and confidentiality of system resources.
At their core, permissions answer three questions for every file/directory:
- Who owns the file?
- What group does the file belong to?
- What actions (read, write, execute) are allowed for the owner, group, and others?
2. Permission Types: Read, Write, Execute
Linux permissions are categorized into three primary actions, each applicable to files and directories (with subtle differences):
Read (r)
- Files: Allows viewing the content (e.g.,
cat file.txt,less file.txt). - Directories: Allows listing the contents of the directory (e.g.,
ls directory/).
Write (w)
- Files: Allows modifying or deleting the file (e.g.,
nano file.txt,rm file.txt). - Directories: Allows creating, renaming, or deleting files/directories within it (requires execute permission too!).
Execute (x)
- Files: Allows running the file as a program/script (e.g.,
./script.sh,python3 app.py). - Directories: Allows accessing the directory (e.g.,
cd directory/) or executing files within it (even if the files have execute permissions).
3. Permission Classes: User, Group, Others
Permissions are assigned to three distinct classes of users, ensuring granular control:
User (u): The File Owner
The user who created the file (or was explicitly assigned ownership). By default, the creator is the owner.
Group (g): A Shared Group
A collection of users with shared permissions. Files belong to one primary group, and all members of that group inherit the group-level permissions.
Others (o): Everyone Else
All users on the system who are not the owner or part of the file’s group. This is the most restrictive class by default.
All (a): Shorthand for User + Group + Others
Used in commands to apply permissions to all three classes at once (e.g., chmod a+r file.txt gives read access to everyone).
4. Permission Notation: Symbolic vs. Octal
Linux represents permissions in two formats: symbolic (human-readable text) and octal (numeric codes). Both are used with the chmod command to modify permissions.
Symbolic Notation
Uses letters and symbols to describe permissions:
- Classes:
u(user),g(group),o(others),a(all). - Operations:
+(add),-(remove),=(set exactly). - Permissions:
r(read),w(write),x(execute).
Examples:
u+x: Add execute permission for the owner.g-w: Remove write permission for the group.o=r: Set others’ permissions to read-only.
Octal Notation
Uses 3-digit numbers (0-777) where each digit represents permissions for user, group, and others, respectively. Each digit is a sum of:
4(read,r),2(write,w),1(execute,x).
Common Octal Values:
| Octal | Binary | Symbolic | Meaning |
|---|---|---|---|
| 0 | 000 | --- | No permissions |
| 1 | 001 | —x | Execute only |
| 2 | 010 | -w- | Write only |
| 3 | 011 | -wx | Write + execute |
| 4 | 100 | r— | Read only |
| 5 | 101 | r-x | Read + execute |
| 6 | 110 | rw- | Read + write |
| 7 | 111 | rwx | Read + write + execute |
Examples:
755:rwxr-xr-x(owner: full access; group/others: read + execute).644:rw-r--r--(owner: read/write; group/others: read-only).
5. Viewing Permissions with ls -l
To check permissions for a file or directory, use ls -l (long listing). Let’s break down the output:
$ ls -l example.txt
-rw-r--r-- 1 alice developers 1024 Oct 5 14:30 example.txt
Breakdown of ls -l Output
| Field | Meaning |
|---|---|
-rw-r--r-- | Permission string (10 characters: file type + 3 classes of permissions). |
1 | Number of hard links. |
alice | Owner (user). |
developers | Group. |
1024 | File size (bytes). |
Oct 5 14:30 | Last modified timestamp. |
example.txt | Filename. |
Permission String Breakdown
The first 10 characters (-rw-r--r--):
- 1st character: File type (
-for regular file,dfor directory,lfor symlink, etc.). - Next 3: User permissions (
rw-= read + write). - Next 3: Group permissions (
r--= read-only). - Last 3: Others permissions (
r--= read-only).
6. Changing Permissions: chmod Command
The chmod (change mode) command modifies file/directory permissions. It supports both symbolic and octal notation.
Basic Syntax
chmod [options] permissions file/directory
Symbolic Mode Examples
- Add execute for the owner:
chmod u+x script.sh - Remove write for group and others:
chmod go-w document.pdf - Set read/write for owner, read-only for group/others:
chmod u=rw,go=r data.csv
Octal Mode Examples
- Give owner full access, group/read others read/execute:
chmod 755 app.py # rwxr-xr-x - Restrict a file to owner-only read/write:
chmod 600 secret.key # rw------- - Make a directory readable/writable/executable by all (use cautiously!):
chmod 777 shared_folder/ # rwxrwxrwx
7. Changing Ownership: chown and chgrp
Permissions depend on ownership. Use chown (change owner) and chgrp (change group) to modify who owns a file or which group it belongs to.
chown: Change Owner (and Group)
Syntax:
chown [options] owner:group file/directory
Examples:
- Change owner to
bob:chown bob report.txt - Change owner to
aliceand group toengineers:chown alice:engineers project/ - Recursively change ownership of a directory and its contents:
chown -R carol:design assets/
chgrp: Change Group Only
Syntax:
chgrp [options] group file/directory
Example:
Change group to marketing:
chgrp marketing campaign.png
Note: Only the root user or the file’s current owner can change ownership. Use
sudofor administrative changes.
8. Special Permissions: SUID, SGID, and Sticky Bit
Beyond standard permissions, Linux supports three special permissions for advanced use cases: SUID, SGID, and Sticky Bit.
SUID (Set User ID)
- Effect: When a file with SUID is executed, it runs with the permissions of the file’s owner (not the user running it).
- Symbolic Notation:
u+s(orsin the user execute position ofls -l). - Octal Value: Add
4to the front of the octal code (e.g.,4755).
Example: The /usr/bin/passwd command has SUID:
ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 Jun 1 2023 /usr/bin/passwd
Here, rws (instead of rwx) indicates SUID. When a user runs passwd, it temporarily gains root privileges to modify /etc/shadow.
SGID (Set Group ID)
- Effect on Files: Executed files run with the permissions of the file’s group.
- Effect on Directories: New files created in the directory inherit the directory’s group (instead of the user’s primary group).
- Symbolic Notation:
g+s(orsin the group execute position). - Octal Value: Add
2to the front (e.g.,2775).
Example: A shared project directory with SGID:
chmod g+s project/ # or chmod 2775 project/
Now, any file created in project/ will belong to the project group, ensuring all members can access it.
Sticky Bit
- Effect: On directories, prevents users from deleting/renaming files owned by others (even if they have write access to the directory).
- Symbolic Notation:
o+t(ortin the others execute position). - Octal Value: Add
1to the front (e.g.,1777).
Example: The /tmp directory uses the Sticky Bit:
ls -ld /tmp
drwxrwxrwt 10 root root 4096 Oct 5 15:00 /tmp
Here, rwt (instead of rwx) indicates the Sticky Bit. Users can create files in /tmp but cannot delete files owned by other users.
9. Practical Scenarios: Applying Permissions in Real Life
Scenario 1: Setting Up a Shared Team Folder
Goal: Allow the dev-team group to read/write files, others to read-only.
- Create the directory:
mkdir /shared/dev-project - Set group ownership to
dev-team:chown :dev-team /shared/dev-project - Enable SGID so new files inherit the
dev-teamgroup:chmod g+s /shared/dev-project - Set permissions: owner/group read/write/execute, others read/execute:
chmod 2775 /shared/dev-project # 2=SGID, 7=user rwx, 7=group rwx, 5=others rx
Scenario 2: Securing a Web Server File
Goal: Restrict access to a sensitive config file (/var/www/secret.conf) so only the web server user (www-data) can read it.
- Set owner to
www-dataand group towww-data:chown www-data:www-data /var/www/secret.conf - Allow owner read/write, block group/others:
chmod 600 /var/www/secret.conf # rw-------
10. Troubleshooting Permission Issues
Common Error: “Permission Denied”
If you see this, check:
- Are you the owner or in the correct group? Use
idto verify your user/group. - Does the file/directory have the required permission (e.g., execute for
cd)? - Use
ls -lato check permissions (including hidden files).
Tools for Debugging
ls -la: List all files (including hidden) with permissions.namei -l /path/to/file: Trace permissions along the file path (e.g., missing execute on a parent directory can block access).stat file.txt: Show detailed ownership/permissions info.
Example: Fixing a “Permission Denied” When Running a Script
If ./script.sh fails:
- Check permissions:
ls -l script.sh # -rw-r--r-- (no execute!) - Add execute for the owner:
chmod u+x script.sh
11. Conclusion
Linux permissions are a cornerstone of system security and resource management. By mastering user/group/others classes, read/write/execute actions, and special permissions like SUID/SGID/Sticky Bit, you can enforce granular access control, prevent data breaches, and ensure smooth collaboration in multi-user environments.
Always follow the principle of least privilege: grant only the permissions necessary for a user/group to perform their tasks. Regularly audit permissions with ls -l and stat, and use tools like chmod and chown to maintain a secure system.
12. References
man chmod: Linux manual page forchmod.man chown: Linux manual page forchown.- Linux Permissions Guide (Linux.com).
- SUID, SGID, and Sticky Bits (Red Hat Sysadmin Blog).
- The Linux Command Line (book by William Shotts, free online).