Linux File and Permission Management - A Complete Guide
In a multi-user environment like Linux, file and permission management is crucial for maintaining security, privacy, and system integrity. Whether you're a system administrator, a developer, or a security analyst, mastering this topic is essential.
This article delves into every aspect of Linux file permissions and ownership, explaining not just the what and how, but also the why—backed with practical commands and outputs.
Table of Contents
- Introduction to Linux Permissions
- Linux File Types
- Understanding File Permission Structure
- File Ownership: User, Group, and Others
- Viewing Permissions with
ls
andstat
- Modifying Permissions with
chmod
- Numeric and Symbolic Permission Modes
- Changing Ownership with
chown
andchgrp
- Special Permissions: SUID, SGID, Sticky Bit
- Directory Permissions vs File Permissions
- Default Permissions and
umask
- Recursive Permissions and Ownership
- ACLs (Access Control Lists)
- Real-World Permission Scenarios
- Summary and Best Practices
1. Introduction to Linux Permissions
Linux is a multi-user operating system, meaning different users can access the system simultaneously. Linux must enforce who can read, write, or execute a file or directory. This is done through permissions and ownership.
Every file and directory has:
- A user owner
- A group owner
- Permissions for:
- User (u)
- Group (g)
- Others (o)
Permissions are:
- r (read)
- w (write)
- x (execute)
2. Linux File Types
The first character in a long ls -l
output indicates the file type:
Symbol | Type |
---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
s | Socket |
p | Named pipe |
$ ls -l -rw-r--r-- 1 alice staff 1024 May 7 12:00 report.txt drwxr-xr-x 2 alice staff 4096 May 7 12:10 scripts
3. Understanding File Permission Structure
A full permission string looks like this:
-rwxr-xr--
Breakdown:
- First character: file type (
-
,d
,l
, etc.) - Next three: user permissions (
rwx
) - Next three: group permissions (
r-x
) - Last three: others permissions (
r--
)
$ ls -l hello.sh -rwxr-xr-- 1 alice devs 123 May 7 12:00 hello.sh
4. File Ownership: User, Group, and Others
Each file is associated with:
- A user owner (who created the file by default)
- A group owner (either primary group or explicitly assigned)
Example:
$ ls -l /var/log/syslog -rw-r----- 1 root adm 10240 May 7 13:00 /var/log/syslog
5. Viewing Permissions
Using ls -l
$ ls -l notes.txt -rw-r--r-- 1 alice users 123 May 7 12:30 notes.txt
Using stat
$ stat notes.txt File: notes.txt Size: 123 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 1048576 Links: 1 Access: 2025-05-07 12:30:00.000000000 +0530 Modify: 2025-05-07 12:28:00.000000000 +0530 Change: 2025-05-07 12:29:00.000000000 +0530 Birth: -
6. Modifying Permissions with chmod
chmod
is used to change file permissions.
Symbolic Mode
chmod u+x script.sh # Add execute to user chmod g-w config.txt # Remove write from group chmod o= file.txt # Remove all permissions from others
Numeric (Octal) Mode
Permissions are represented as numbers:
Permission | Value |
---|---|
read (r) | 4 |
write (w) | 2 |
execute (x) | 1 |
chmod 755 script.sh # rwx for user, rx for group and others chmod 644 file.txt # rw for user, r for group and others
7. Numeric and Symbolic Permission Modes
Symbolic | Numeric | Meaning |
---|---|---|
rwxrwxrwx | 777 | Full access |
rwxr-xr-x | 755 | User full, others read+execute |
rw-r--r-- | 644 | User read/write, others read |
rw------- | 600 | User only access |
r-------- | 400 | Read-only |
8. Changing Ownership with chown
and chgrp
chown
– Change File Owner
sudo chown bob report.txt sudo chown bob:staff report.txt # user and group
chgrp
– Change Group Ownership
sudo chgrp developers code.py
9. Special Permissions
SUID – Set User ID
Executes file as owner.
sudo chmod u+s /usr/bin/passwd ls -l /usr/bin/passwd -rwsr-xr-x 1 root root ...
SGID – Set Group ID
On files: executes with group permissions. On directories: new files inherit the group.
sudo chmod g+s /shared ls -ld /shared drwxr-sr-x ...
Sticky Bit
Only file owner can delete files in a directory.
sudo chmod +t /public ls -ld /public drwxrwxrwt ...
10. Directory Permissions vs File Permissions
Permission | File Behavior | Directory Behavior |
---|---|---|
read (r) | View file contents | List directory contents |
write (w) | Modify file | Create/delete files in directory |
execute (x) | Run as program | Enter/access directory |
To access a directory: x
is mandatory.
11. Default Permissions and umask
When a file is created, it starts with:
- Files:
666
(rw-rw-rw-
) - Directories:
777
(rwxrwxrwx
)
umask
subtracts from the default.
Check umask:
$ umask 0022
12. Recursive Permissions and Ownership
Change ownership recursively:
sudo chown -R alice:devs /project
Change permissions recursively:
chmod -R 755 /project
13. ACLs (Access Control Lists)
ACLs allow fine-grained access control beyond traditional owner-group-other.
Enable ACL (ext4, XFS, etc.):
mount -o remount,acl /dev/sda1 /
Set ACL:
setfacl -m u:bob:rwx file.txt
View ACL:
getfacl file.txt
Remove ACL:
setfacl -x u:bob file.txt
14. Real-World Permission Scenarios
Scenario 1: Secure config file
chmod 600 config.ini chown root:root config.ini
Scenario 2: Shared team folder
mkdir /devteam chgrp devs /devteam chmod 2770 /devteam # SGID + rwx for group
Scenario 3: Public upload directory
mkdir /uploads chmod 1777 /uploads # Sticky bit + full access
15. Summary and Best Practices
- Use minimal permissions necessary.
- Be cautious with
777
, especially on web servers. - Use
chown
andchmod
correctly for user access. - Implement SGID and Sticky Bit for team and public use.
- For more granular control, use ACLs.
Always validate permission settings regularly to prevent security breaches or access issues.