DEV Community

Cover image for Linux in Action: Understanding Basic File and Directory Permissions
DhavalThakar97
DhavalThakar97

Posted on

3

Linux in Action: Understanding Basic File and Directory Permissions

Introduction

In the world of Linux, permissions serve as a foundational security layer. They determine who can read, write, or execute a directory or file, providing crucial access control in multi-user environments.

In this article, weโ€™ll focus on Basic Permissions โ€” the first of three key types (Basic, ACLs, and Special Permissions). We'll explore how Linux handles ownership, how to view and modify permissions, and real-world tips for using chmod, chown, and chgrp securely.

๐Ÿ“Œ Advanced topics like Access Control Lists (ACLs) and Special Permissions (SetUID, SetGID, Sticky Bit) will be covered in separate articles.

Table of Contents

What Are Directory Permissions?

Permissions in Linux define how users can interact with directories. They are set by the superuser or the owner and are essential for securing shared systems.

There are three permission classes:

  • User (u): The directory owner
  • Group (g): Users in the group assigned to the directory
  • Others (o): Everyone else

And three permission types:

  • Read (r): List directory contents
  • Write (w): Create, delete, or rename files in the directory
  • Execute (x): Access the directory and traverse into it

Understanding the Permission String

When you list directories with ls -ld, the output looks like this:

drwxr-xr-- 3 alice devs 4096 May 25 08:00 /opt/project/
Enter fullscreen mode Exit fullscreen mode

Breakdown:

  • d indicates it's a directory
  • rwx โ€“ user (owner) permissions
  • r-x โ€“ group permissions
  • r-- โ€“ others' permissions

Viewing Permissions

  • Directory permissions:
ls -ld /opt/project/
Enter fullscreen mode Exit fullscreen mode

This allows you to audit directory permissions accurately.

Ownership in Linux

Change Directory Owner:

chown john /opt/project/
Enter fullscreen mode Exit fullscreen mode

Change Group Ownership:

chgrp developers /opt/project/
Enter fullscreen mode Exit fullscreen mode

Change Both Owner and Group:

chown john:developers /opt/project/
Enter fullscreen mode Exit fullscreen mode

Verify with:

ls -ld /opt/project/
Enter fullscreen mode Exit fullscreen mode

Modifying Permissions with chmod

Remove all permissions from others:

chmod o-rwx /opt/project/
Enter fullscreen mode Exit fullscreen mode

Remove write permission from group:

chmod g-w /opt/project/
Enter fullscreen mode Exit fullscreen mode

Remove execute from user:

chmod u-x /opt/project/
Enter fullscreen mode Exit fullscreen mode

Add full permissions to user:

chmod u+rwx /opt/project/
Enter fullscreen mode Exit fullscreen mode

Remove all permissions:

chmod u-rwx,g-rwx,o-rwx /opt/project/
Enter fullscreen mode Exit fullscreen mode

Add all permissions:

chmod ugo+rwx /opt/project/
Enter fullscreen mode Exit fullscreen mode

Set exact permissions:

chmod ugo=r-x /opt/project/
Enter fullscreen mode Exit fullscreen mode

Numeric (Octal) Permissions

Each permission maps to a number:

  • Read = 4
  • Write = 2
  • Execute = 1

You sum these for each class:

chmod 750 /opt/project/
Enter fullscreen mode Exit fullscreen mode
  • User: 7 (rwx)
  • Group: 5 (r-x)
  • Others: 0 (no permissions)

Bonus: The Link Count

When you run ls -ld, the second column is the link count:

drwxr-xr-x 2 john devs 4096 May 25 09:00 /opt/project/
Enter fullscreen mode Exit fullscreen mode

Here, the 2 means this directory has two links: one for itself, and one for . or .. in a subdirectory. This number increases with additional subdirectories, including hidden ones.

๐Ÿ“Œ For files, the link count remains 1 unless hard links are created.

Real-World Use Case: DevOps and RHCSA Practice

In enterprise DevOps pipelines, directory permissions are often managed as part of automation workflows:

  • CI/CD pipelines: Temporary directories created for builds must be secured using chmod 700 to restrict access to build agents only.
  • Infrastructure-as-Code (IaC): Tools like Ansible use file modules to set owner, group, and mode for deployment directories.
  • Multi-tenant servers: RHCSA best practices recommend separating each applicationโ€™s working directory and locking them down to only authorized groups.
  • Security audits: Enterprises rely on regular scans of /opt, /var/log, /srv, and /home directories to detect misconfigured permissions that can expose sensitive data.

These practices ensure:

  • Minimal attack surface
  • Reduced risk of privilege escalation
  • Compliance with internal ITGC and external regulations (PCI-DSS, ISO27001)

Conclusion

Basic directory permissions are an essential part of system hardening and operational efficiency. With a solid grasp of chmod, chown, and permission strings, you can confidently secure your Linux environment.

๐Ÿ” Next up: Weโ€™ll explore Access Control Lists (ACLs) and Special Permissions like SetUID, SetGID, and Sticky Bit.

Stay tuned and stay secure. ๐Ÿ›ก๏ธ

Connect with me on LinkedIn for further discussions and networking opportunities.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more