Friday, 21 August 2026

chmod and chown Commands in Linux: Complete Guide with Examples (2026)

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.sh

Look 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. A d means 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 = 7
  • rw- = 4 + 2 + 0 = 6
  • r-x = 4 + 0 + 1 = 5
  • r-- = 4 + 0 + 0 = 4

Common Numeric Examples:

Full permissions for owner, read/execute for others:

chmod 755 script.sh

Read and write for owner, read-only for everyone else:

chmod 644 config.txt

Highly restrictive — only the owner can read and write (great for SSH keys):

chmod 600 private_key.pem

2. 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.sh

Remove write permissions from group and others:

chmod go-w document.docx

Add write permission only for the owner:

chmod u+w notes.txt

Fixing 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] filename

Common chown Examples:

Change the owner of a file to your user account:

sudo chown ubuntu app.log

Change the group ownership of a file to "developers":

sudo chown :developers project.conf

Change both owner and group at once:

sudo chown john:sysadmins database.sql

Managing 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/html

Change 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.html

This 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

CommandWhat It Does
chmod 755 fileOwner can do everything; Group/Others can read & execute.
chmod 644 fileOwner can read/write; Group/Others can only read.
chmod u+x fileMakes the file executable for the owner only.
chown user fileChanges the owner of the file to "user".
chown :group fileChanges the group of the file to "group".
chown -R u:g dirRecursively 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.

No comments:

Post a Comment