Skip to main content

Linux File Permissions: A Comprehensive Guide

RAFSuNX
5 mins to read

Introduction

Understanding Linux file permissions is foundational for any systems administrator, DevOps engineer, or cybersecurity professional seeking to build and maintain secure, stable Unix-like environments. Rooted in decades of development, the Linux permission model provides granular control over who can read, write, or execute files and directories on a system.

More than a mechanical framework, file permissions protect against common security threats such as privilege escalation, unauthorized access, and configuration drift. Whether in a personal Linux environment or a scaled-out enterprise infrastructure, mastering file permissions is essential.

In this comprehensive guide, you’ll learn:

  • The meaning of Linux file permissions (rwx)
  • User, group, and others-based access models
  • The role of special bits (setuid, setgid, and sticky)
  • How to manage permissions using chmod, chown, and chgrp
  • Real-world examples and best practices
  • How to audit permissions and automate security

Let’s begin with the basics.

Understanding the Linux Permission Model

In Linux, every file and directory is owned and governed by:

  • User (Owner)
  • Group
  • Others (everyone else)

Each of these entities is granted permissions in three categories:

  • Read (r) – View file contents or list directory contents
  • Write (w) – Modify file content or create/delete files in a directory
  • Execute (x) – Run a file as a program or access directory structure

You can view file permissions using ls -l:

$ ls -l report.txt
-rw-r--r--  1 alice staff  1520 Apr  3 10:12 report.txt

The leftmost part (-rw-r--r--) shows the permissions:

  • - = regular file
  • rw- = owner can read/write
  • r-- = group can read
  • r-- = others can read

Numeric (Octal) Notation

Permissions also map to numeric values:

Symbol Value
r 4
w 2
x 1

Each permission set (user, group, others) is summed:

rwx = 4+2+1 = 7
rw- = 4+2+0 = 6
r-- = 4+0+0 = 4

So, -rw-r--r-- becomes 644:

chmod 644 report.txt

Core Tools to Manage Permissions

Linux provides three main commands to control permissions:

1. chmod: Change file mode (permissions)

  • Symbolic:
chmod u+x script.sh      # Add execute to user
chmod g-w shared.txt     # Remove write from group
chmod o=r notes.txt      # Others get read only
  • Octal:
chmod 755 program.sh     # rwxr-xr-x
chmod 600 config.json    # rw-------

2. chown: Change file owner and/or group

chown root file.txt               # Change owner to root
chown devops:engineers logs.txt  # Change owner and group

3. chgrp: Change group ownership

chgrp admins /var/log/syslog

These commands form the foundation of Linux access control.

Special Permission Bits: setuid, setgid, and Sticky Bit

Linux supports three special permissions that alter process execution or directory behavior:

Special Bit Applies To Effect
setuid Files Run with owner’s privileges (e.g., root)
setgid Files/Dirs Files: Run with group privilegesDirs: New files inherit group
Sticky Dirs Only file owner can delete their files

Example: setuid

ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 54256 Apr  3 09:31 /usr/bin/passwd
  • s in place of x for user → setuid is enabled.
  • Lets a regular user run passwd, which modifies protected files like /etc/shadow.

Set with:

chmod u+s filename

Example: setgid

On directories, this ensures group consistency:

chmod g+s shared_dir

Any new file in shared_dir inherits the directory group.

Example: Sticky Bit

Critical for shared directories like /tmp:

chmod +t /tmp

Only file owners can delete their own files in that directory.

Real-World Linux Permission Use Cases

Let’s explore practical scenarios that demonstrate permission management.

1. Shared Development Directory

Your team’s directory /var/www/project needs every developer to:

  • Read/write files
  • Retain group consistency
sudo chgrp -R devgroup /var/www/project
sudo chmod -R 2775 /var/www/project
  • 2 in 2775 = setgid bit

2. Logging: Restrict Access for Compliance

You want only the syslog group to read logs, no external writes.

sudo chown root:syslog /var/log/messages
sudo chmod 640 /var/log/messages

3. Prevent Accidental Editing of Critical Files

Make scripts executable but not writable:

chmod 555 deploy.sh   # r-xr-xr-x

4. Secure Temporary File Storage

Force sticky bit on /tmp-like directories:

chmod 1777 /tmp

5. Mistaken Full Permissions (Dangerous)

A file with chmod 777 is fully open. Avoid:

chmod 777 script.sh  # BAD: anyone can write and execute - security risk

Use chmod 755 for executable scripts owned by root.

Advanced Tips & Best Practices

Tip Description
Avoid 777 Always use minimum required permissions
Audit Regularly Use find or tools like Lynis to scan
Use Groups Manage access via groups, not individual users
Automate Use Ansible or scripts to set permissions in CI/CD
Immutable Flags Use chattr +i for high protection files

How to Find Potentially Exploitable Files

# Find all setuid files
find / -perm -4000 -type f 2>/dev/null

# Find writable files by others
find / -perm -002 -type f

Use ACL for Fine-grained Permissions

Access Control Lists (ACL) allow beyond-owner-group-others setup:

setfacl -m u:joe:rwx file.txt
getfacl file.txt

Resources & Next Steps

Conclusion

Linux file permissions are not just about setting rwx flags - they form the scaffolding of your operating system’s security posture. Whether making a script executable, or ensuring production logs remain confidential, understanding and applying permission principles helps safeguard against misconfiguration and breach.

Key Takeaways:

  • Linux permissions control access via user, group, and others
  • chmod, chown, and chgrp are your core tools
  • Special bits (setuid, setgid, sticky) unlock advanced behavior
  • Real-world usage demands care, auditing, and automation
  • Avoid full permissions (777), use groups wisely, and protect critical directories

Security starts with your filesystem. Make Linux permissions a priority - not an afterthought.