If you've ever hit a "Permission denied" error on Linux, you're not alone — it's one of the most common roadblocks for anyone managing servers. The good news is that once you understand how Linux permissions work, fixing them takes seconds. Let's break it down.
Understanding Linux Permission Structure
Before running any commands, you need to understand how Linux displays permissions. When you run ls -l in your terminal, you'll see output like this:
-rwxr-xr-- 1 root root 4096 Aug 21 11:15 script.shLook at the first 10 characters (-rwxr-xr--). Here's how to read them, left to right:
- File Type (1st character): A hyphen
-means a regular file. Admeans a directory. - User/Owner Permissions (next 3):
rwx— what the file's creator/owner can do. - Group Permissions (next 3):
r-x— what members of the file's assigned group can do. - Others Permissions (last 3):
r--— what every other user on the system can do.
What Do r, w, and x Mean?
- r (Read): Permission to view file contents or list a directory.
- w (Write): Permission to modify or delete the file, or add/remove files in a directory.
- x (Execute): Permission to run a file as a program/script, or enter a directory.
How to Use the chmod Command
The chmod (Change Mode) command changes what users can do with a file or directory. You can set permissions two ways: Absolute (Numeric) Mode or Symbolic (Text) Mode.
1. Absolute (Numeric) Mode
Numeric mode assigns a value to each permission:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
- No Permission = 0
Add the numbers together to build a permission set:
rwx= 4 + 2 + 1 = 7rw-= 4 + 2 + 0 = 6r-x= 4 + 0 + 1 = 5r--= 4 + 0 + 0 = 4
Common Numeric Examples:
Full permissions for owner, read/execute for others:
chmod 755 script.shRead and write for owner, read-only for everyone else:
chmod 644 config.txtHighly restrictive — only the owner can read and write (great for SSH keys):
chmod 600 private_key.pem2. Symbolic (Text) Mode
Symbolic mode uses letters to target specific groups and actions. It's easier when you just want to add or remove a single permission without doing math.
- References:
u(user/owner),g(group),o(others),a(all) - Operators:
+(add),-(remove),=(set exactly)
Common Symbolic Examples:
Make a script executable for everyone:
chmod a+x deploy.shRemove write permissions from group and others:
chmod go-w document.docxAdd write permission only for the owner:
chmod u+w notes.txtFixing Ownership with the chown Command
While chmod modifies actions, chown (Change Owner) modifies who owns the file or directory. This is usually the first tool to reach for when you see "Permission denied" because a file belongs to root instead of your regular user account.
Only root or a user with sudo privileges can change ownership. Basic syntax:
chown [owner]:[group] filenameCommon chown Examples:
Change the owner of a file to your user account:
sudo chown ubuntu app.logChange the group ownership of a file to "developers":
sudo chown :developers project.confChange both owner and group at once:
sudo chown john:sysadmins database.sqlManaging Entire Folders Recursively
When managing web servers or app deployments, you'll often need to change permissions for a whole directory tree at once. Use the recursive flag -R.
Change ownership of a folder and everything inside it:
sudo chown -R www-data:www-data /var/www/htmlChange permissions of a folder and everything inside it:
chmod -R 755 /home/user/public_html⚠️ Warning: Be careful with -R combined with chmod 777. Giving full read, write, and execute access to everyone on the system creates serious security vulnerabilities. Never use 777on a production server.
Real-World Example: Nginx Web Root
A common deployment task — handing a web root over to the Nginx user so it can serve files correctly:
sudo chown -R www-data:www-data /var/www/mysite
sudo chmod -R 755 /var/www/mysite
sudo chmod 644 /var/www/mysite/index.htmlThis gives Nginx ownership of the directory, sets safe directory/file traversal permissions, and locks static files to read-only for everyone but the owner.
Summary Cheat Sheet
| Command | What It Does |
|---|---|
chmod 755 file | Owner can do everything; Group/Others can read & execute. |
chmod 644 file | Owner can read/write; Group/Others can only read. |
chmod u+x file | Makes the file executable for the owner only. |
chown user file | Changes the owner of the file to "user". |
chown :group file | Changes the group of the file to "group". |
chown -R u:g dir | Recursively changes owner and group for a directory. |
FAQ
Q: What's the difference between chmod and chown?
chmod controls what actions are allowed (read/write/execute). chown controls who owns the file or directory.
Q: Why do I still get "Permission denied" after chmod 777?
777 grants all actions to everyone, but if the file is still owned by root or another user, ownership issues (or SELinux/AppArmor policies) may be the real blocker — try chown first.
Q: Is chmod 777 ever safe to use?
Avoid it on anything production-facing. It's sometimes used briefly for local debugging, but should never be left on a live server.
Q: How do I check current permissions before changing them?
Run ls -l in the file's directory, or ls -ld for a directory itself.
By mastering chmod and chown, you can secure your systems, prevent unauthorized access, and troubleshoot permission issues with confidence.