DEV Community

DevOps Man
DevOps Man

Posted on • Edited on

Mastering Linux File Permissions: The Ultimate Guide.

Linux file permissions can feel cryptic at first, but once you break them down, they’re not only simple — they’re powerful. Whether you're building systems, writing scripts, or deploying code, understanding how to control file access is a must-have skill. This guide will walk you through everything you need to know.


Why File Permissions Matter

Linux is a multi-user system. Proper permissions:

  • Keep your system secure

  • Prevent accidental or malicious changes

  • Allow collaboration without compromising safety


Understanding rwx and User Classes
Permissions are shown as:

-rwxr-xr--

Breakdown:

  • First char: - (file) or d (directory)

  • Next 3: rwx for User (owner)

  • Next 3: r-x for Group

  • Last 3: r-- for Others

User Classes

  • u = User (owner)

  • g = Group

  • o = Others

  • a = All (u+g+o)

Permission Types

  • r = Read

  • w = Write

  • x = Execute

  • -= No permission


Viewing File Permissions

Using ls

ls -l /etc/passwd
-rw-r--r-- 1 root root 2871 Aug 22 14:43 /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Using stat

stat /etc/shadow
Access: (0640/-rw-r-----)  Uid: (0/root)   Gid: (42/shadow)
Enter fullscreen mode Exit fullscreen mode

Modifying Permissions

Symbolic (Relative) Mode

chmod u+x file.txt        # Add execute for user
chmod g-w file.txt        # Remove write from group
chmod o-r file.txt        # Remove read from others
chmod a+r file.txt        # Add read for all
Enter fullscreen mode Exit fullscreen mode

Octal (Absolute) Mode

User Group Other Octal Command Meaning
rwx rwx rwx 777 chmod 777 file.txt Full permissions to everyone
rwx r-x r-x 755 chmod 755 file.txt Owner: all, Others: read/exec
rw- r-- r-- 644 chmod 644 file.txt Owner: read/write, rest: read

Setting Permissions from Another File

chmod --reference=file1 file2
Enter fullscreen mode Exit fullscreen mode

Recursive Permissions

chmod -R u+rw,o-rwx mydir/
Enter fullscreen mode Exit fullscreen mode

Special Permissions

SUID (Set User ID)
Executes file with owner’s privileges.

chmod u+s file
chmod 4755 file
Enter fullscreen mode Exit fullscreen mode

Example:

ls -l /usr/bin/umount
-rwsr-xr-x 1 root root 39144 /usr/bin/umount
Enter fullscreen mode Exit fullscreen mode

SGID (Set Group ID)
Runs with group’s privileges, or maintains group ownership in directories.

chmod g+s dir
chmod 2750 dir
Enter fullscreen mode Exit fullscreen mode

Sticky Bit
Only file owner can delete their files in shared directories.

chmod +t dir
chmod 1777 dir
Enter fullscreen mode Exit fullscreen mode

Example:

ls -ld /tmp
drwxrwxrwt 10 root root 4096 /tmp
Enter fullscreen mode Exit fullscreen mode

UMASK: Default Permissions

View Current UMASK

umask
Enter fullscreen mode Exit fullscreen mode

Set New UMASK

umask 0022
Enter fullscreen mode Exit fullscreen mode

How it Works
UMASK subtracts permissions from 666 (files) or 777 (dirs).


Ownership Commands

Change Owner

chown new_user file
Enter fullscreen mode Exit fullscreen mode

Change Group

chgrp new_group file
Enter fullscreen mode Exit fullscreen mode

Change Both

chown user:group file
Enter fullscreen mode Exit fullscreen mode

Recursive Ownership Change

chown -R user:group dir
Enter fullscreen mode Exit fullscreen mode

Bonus: File Attributes (Advanced Layer)

View Attributes

lsattr file
Enter fullscreen mode Exit fullscreen mode

Change Attributes

sudo chattr +i file   # Make file immutable
sudo chattr -i file   # Make it editable again
Enter fullscreen mode Exit fullscreen mode

Cheat Sheet Summary

  • chmod → change permissions

  • chown / chgrp → change ownership

  • umask → set default permissions

  • ls -l / stat → view permissions

  • +x, -w, a+r → symbolic changes

  • 644, 755, 777 → octal changes

  • SUID, SGID, Sticky Bit → special bits


Conclusion

Linux permissions are a superpower once you understand the logic. Master these commands, practice regularly, and you’ll never get caught off-guard by a “Permission denied” again.

Top comments (0)