Mastering Linux File Permissions: How to Use Chmod and Chown Like a Pro
Whether you are hosting a personal website, deploying code on an AWS cloud server, or troubleshooting an application error, you have probably run into the dreaded message: Permission Denied.
It is one of the most common hurdles every developer and system administrator faces. You write a script, try to execute it, and Linux instantly blocks you. Understanding how file permissions work is what separates beginners from confident tech professionals.
In this guide, we are going to break down Linux file permissions (chmod and chown) in plain, human English so you never have to guess your way through a permission error again.
1. The Three Pillars of Linux Permissions: Read, Write, Execute
In the Linux world, everything is a file. And every single file and folder is protected by three basic types of permissions:
- Read (r): Allows a user to view or read the contents of a file (or list the contents of a directory). Represented by the number
4. - Write (w): Allows a user to modify, edit, or delete a file (or create/delete files inside a directory). Represented by the number
2. - Execute (x): Allows a user to run a file as a program or script (or enter into a directory). Represented by the number
1.
2. Who Owns the File? (The Three User Classes)
Permissions are divided into three distinct groups or classes:
- User / Owner (u): The specific user who created or owns the file.
- Group (g): A collection of users who share access rights to the file.
- Others (o): Everyone else on the system who is neither the owner nor part of the group.
If you run ls -l in your terminal, you will see permissions displayed like this: -rwxr-xr--.
- The first character tells you the file type.
- The next three (
rwx) belong to the User. - The middle three (
r-x) belong to the Group. - The last three (
r--) belong to Others.
3. chmod – Changing File Permissions
The chmod (change mode) command is used to alter what users can do with a file or folder. You can use it in two ways: symbols or numbers.
Method A: The Numeric (Octal) Way
By adding up the values of Read (4), Write (2), and Execute (1), you get a 3-digit permission code.
- Give full control to owner, read/execute to group and others (
755):Bashchmod 755 script.sh - Lock down a private configuration file so only the owner can read/write (
600):Bashchmod 600 .env
Method B: The Symbolic Way
If numbers aren’t your thing, you can use letters to add or remove specific permissions on the fly.
- Make a script executable for everyone:Bash
chmod +x script.sh - Remove write permissions for “others”:Bash
chmod o-w secret.txt
4. chown – Changing File Ownership
Sometimes changing permissions isn’t enough; you need to change who owns the file entirely. This is crucial when web servers (like Nginx or Apache) need access to files owned by your user account.
- Change the owner of a file:Bash
sudo chown sunil filename.txt - Change both the owner and the group simultaneously:Bash
sudo chown sunil:www-data index.php - Change ownership recursively for an entire folder and its contents:Bash
sudo chown -R sunil:sunil /var/www/html/
Next Steps in Your Linux Journey
Now that you have mastered how to manage file security and permissions, keeping your environment locked down against external threats is your next crucial milestone. Check out our comprehensive guide on Linux Server Hardening Best Practices to secure your system like an expert.
For additional documentation and core utilities reference, you can also explore the official GNU Coreutils Documentation.
Conclusion
File permissions can feel like a puzzle at first, but once you understand the math behind chmod and the logic of chown, managing a Linux system becomes second nature. Practice safely in your local terminal or cloud instance, and you’ll never fear a permission error again.
Got questions about user roles or server setups? Drop a comment below and let’s talk about it on SunilRana.in!