Mastering Linux File Permissions: A Practical Guide for Every User

Expertise in Cloud, Networking & DevOps
Photo by Simon Berger / Unsplash

πŸ› οΈπŸ§

If you’ve ever encountered a "Permission Denied" error in Linux, you’re not alone. Understanding file permissions is crucial for controlling access, maintaining security, and ensuring the right users can read, write, or execute files. Whether you're a beginner or an experienced sysadmin, mastering file permissions will make your Linux journey much smoother. πŸš€

This guide will break down Linux file permissions, their symbolic and numeric notations, and how you can modify them with the chmod command. Let’s dive in! πŸ”₯


πŸ” What Are Linux File Permissions?

Linux is a multi-user operating system, which means that different users may have different levels of access to files and directories. Each file in Linux is associated with three types of users:

  • Owner (User) πŸ‘€ β†’ The user who created the file.
  • Group πŸ‘₯ β†’ A set of users who share access rights.
  • Others 🌍 β†’ Anyone who is not the owner or in the group.

Each of these user types can have three types of permissions:

Symbol Permission Meaning
r Read The file can be read (e.g., cat, less, more).
w Write The file can be modified (e.g., nano, vim).
x Execute The file can be executed as a program or script.

Permissions are typically represented in symbolic notation (e.g., rw-r--r--) or in octal (numeric) notation (e.g., 644). Let's break them down.


πŸ“– Understanding File Permission Syntax

When you run the command:

ls -l

You'll see output similar to this:

drwxrwxr-x  16 dan  jcrew  4096  Apr 6  23:43  work
-rw-rw-r--   1 dan  jcrew  5247  Apr 9  19:58  README.txt
-rw-rw-r--   2 dan  jcrew  355821 Apr 17  21:21  backup.tgz
-rwxrw-r--   2 dan  jcrew  1132  Apr 15  21:22  run.py

Breaking it down:

Field Example Meaning
File Type d (directory) / - (file) If it starts with d, it's a directory. If -, it's a file.
User (Owner) Permissions rwx The owner can read, write, and execute.
Group Permissions rw- Members of the group can read and write, but not execute.
Others (Public) Permissions r-- All other users can only read.
Hard Links 2 Number of links to the file.
Owner Name dan The user who owns the file.
Group Name jcrew The group that has access to the file.
File Size 1132 Size of the file in bytes.
Last Modified Date Apr 15 21:22 The last time the file was modified.
File Name run.py The actual file name.

πŸ“ Example:
-rwxrw-r-- means:

  • rwx β†’ Owner (dan) can read, write, execute.
  • rw- β†’ Group (jcrew) can read, write, but not execute.
  • r-- β†’ Others can only read.

πŸ”’ Numeric (Octal) File Permissions

Instead of rwx notation, Linux also represents permissions using numbers:

Permission Symbolic Octal
No permission --- 0
Execute --x 1
Write -w- 2
Write + Execute -wx 3
Read r-- 4
Read + Execute r-x 5
Read + Write rw- 6
Read + Write + Execute rwx 7

Using this system, file permissions like rwxrw-r-- can be represented numerically as 764.

chmod 764 run.py
  • 7 (Owner) β†’ rwx
  • 6 (Group) β†’ rw-
  • 4 (Others) β†’ r--

πŸ› οΈ Changing File Permissions with chmod

The chmod command lets you modify file permissions.

Using Numeric Notation

chmod 755 script.sh

πŸ”Ή 755 means:

  • Owner (7) β†’ Read, Write, Execute (rwx).
  • Group (5) β†’ Read, Execute (r-x).
  • Others (5) β†’ Read, Execute (r-x).

Using Symbolic Notation

chmod u+x script.sh  # Give execute permission to the owner
chmod g-w script.sh  # Remove write permission from the group
chmod o-r script.sh  # Remove read permission from others
  • u β†’ Owner (User).
  • g β†’ Group.
  • o β†’ Others.
  • a β†’ All (User, Group, Others).

Example: To remove write permissions for others:

chmod o-w myfile.txt

πŸ›‘ Securing Files: chmod Best Practices

βœ”οΈ Private files: Use chmod 600 to allow only the owner to read/write.
βœ”οΈ Public-readable files: Use chmod 644 (e.g., HTML pages).
βœ”οΈ Executable scripts: Use chmod 755 to allow everyone to execute but only the owner to modify.
βœ”οΈ Critical system files: Never give world-write permissions (chmod 777 is dangerous! 🚨).

Example: Secure SSH keys

chmod 600 ~/.ssh/id_rsa

This ensures only you can read/write your private key.


⏭️ Advanced File Permissions: chown and chgrp

Changing File Owner with chown

chown newuser myfile.txt

Now newuser owns myfile.txt.

Changing File Group with chgrp

chgrp newgroup myfile.txt

Now newgroup has group permissions.

πŸ”Ή You can combine both:

chown newuser:newgroup myfile.txt

πŸ” Conclusion: Take Control of Linux File Permissions

Understanding Linux file permissions is a must-have skill for system administrators, developers, and DevOps engineers. By correctly managing permissions, you can enhance security, prevent unauthorized access, and ensure system integrity.

πŸš€ Key Takeaways

βœ… Use ls -l to check file permissions.
βœ… Understand symbolic (rwxr-xr--) and numeric (755) notation.
βœ… Modify permissions using chmod.
βœ… Secure sensitive files with strict permissions.

πŸ“’ What’s your favorite Linux security tip? Drop a comment below! πŸ‘‡

πŸ”” Follow CloudNetOps.tech for more Linux tutorials! 🐧✨

Read more