Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

chmod Command Guide for Changing File Permissions

chmod changes the permissions on a file or directory — who is allowed to read it, write to it, or execute it.


What is chmod, and Why Use It?

Every file and directory on a Linux system carries a small set of permission bits: nine flags that say whether the owner, the group, and everyone else can read, write, or execute it. chmod (“change mode”) is the command that edits those flags.

There are two ways to write permissions:

  • Symbolic — chmod u+x script.sh (“add execute for the user”)
  • Octal — chmod 755 script.sh (three digits: owner, group, others)

Both do exactly the same thing; the octal form is just more compact once you know the numbers.

Why Learn chmod?

  • You’ll hit “Permission denied” constantly until you understand this — it’s the most common error a beginner sees.
  • Scripts won’t run until you mark them executable (chmod +x).
  • Servers and configs need locking down — a world-writable config or a public 777 directory is a security hole.
  • It’s interview and sysadmin-fu basics — knowing what 755 and 644 mean is assumed knowledge on Linux jobs.

Permission bits are the foundation of everything else in this section: chown and chgrp decide who you are, chmod decides what they can do.


1. See the Permissions First

Before changing anything, look at what’s there. ls -l shows the permission string in column one:

ls -l notes.txt

Example:

$ ls -l deploy.sh
-rw-r--r-- 1 alice devs 412 Dec  4 09:12 deploy.sh

Read the string in three chunks of three:

-    rw-    r--    r--
│     │      │      └── others (everyone else)
│     │      └───────── group
│     └──────────────── owner (alice)
└────────────────────── type: - file, d directory, l symlink
  • r = read, w = write, x = execute (or “traverse” for a directory), - = no permission.
  • The first character is the file type, not a permission.

2. Add or Remove Permissions (Symbolic Mode)

The symbolic form uses u (user/owner), g (group), o (others), a (all), plus + to add, - to remove, and = to set exactly:

chmod u+x file
chmod g-w file
chmod o=r file

Example:

# Make a script executable for its owner only
chmod u+x deploy.sh

# Take write access away from everyone except the owner
chmod go-w secrets.txt

# Make a config readable by everyone, writable by nobody else
chmod a=r,go=w config.txt
  • This is the safest style for small changes: it says exactly what changes and leaves the rest untouched.
  • You can chain several clauses with commas: chmod u=rw,g+r,o=r file.

3. Set Permissions with Octal Numbers

Each of the three chunks becomes one digit, built from r=4, w=2, x=1:

PermissionBitsDigit
---0000
r--1004
rw-1106
r-x1015
rwx1117

Three digits = owner, group, others:

chmod 755 script.sh
chmod 644 notes.txt

Example:

$ chmod 755 deploy.sh
$ ls -l deploy.sh
-rwxr-xr-x 1 alice devs 412 Dec  4 09:12 deploy.sh
  • 755 = owner can do everything (rwx), group and others can read and execute (r-x) — the standard for scripts and executables.
  • 644 = owner can read and write, everyone else can only read — the standard for regular files.
  • 600 = only the owner — for private files like keys and secrets.
  • 700 = only the owner, executable — for private scripts.

4. Change Permissions Recursively

Add -R to apply the change to a directory and everything inside it:

chmod -R 755 /srv/web/

Example:

# Give a whole project directory standard permissions
chmod -R u+rwX,go+rX project/
  • ⚠️ Always preview first with find when recursing over system directories — a wrong chmod -R can break a package:
# See what a recursive change WOULD touch before running it
find /srv/web/ -type f -name "*.html"
  • Note the capital X above: it means “execute only if already executable or a directory”, which avoids marking every file executable.

5. Make a Script Executable

This is the single most common reason beginners learn chmod:

chmod +x script.sh
./script.sh

Example:

$ touch backup.sh
$ ls -l backup.sh
-rw-r--r-- 1 alice devs 0 Dec  4 09:20 backup.sh
$ chmod +x backup.sh
$ ls -l backup.sh
-rwxr-xr-x 1 alice devs 0 Dec  4 09:20 backup.sh
  • Without x, the shell refuses to run the file (“Permission denied”) — the file exists, you just haven’t marked it executable.
  • chmod +x without a u/g/o prefix applies to all three classes at once.

6. Lock Down a Directory or File

Tightening permissions is the mirror image of opening them:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519

Example:

# Only alice can enter her SSH directory or read her private key
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/authorized_keys
  • sshd refuses to work if private keys are readable by others — 600 is the required setting.
  • Home directories are conventionally 750 or 700 on multi-user systems.

Common Pitfalls

  • chmod 777 is almost never the answer. It gives every user on the system full control of the file. If you’re reaching for 777 to fix a “Permission denied”, the real fix is almost always chown or a narrower chmod.
  • Forgetting -R on directories. Changing a directory’s permissions does not automatically change the files inside it.
  • Marking everything executable. chmod -R +x dir/ puts x on data files too; use +X (capital) or find -type f -exec to target only what should run.
  • Running chmod on system files as root. A wrong change to /etc/passwd or an init script can make the system unbootable. Check what you’re changing with ls -l first.
  • Expecting chmod to affect a running process. Permission changes apply to new accesses; already-open file handles keep working until reopened.

PageWhy it’s related
chownChange who owns a file — the other half of access control
chgrpChange a file’s group
umaskControls the default permissions new files get
lsls -l is how you read the permissions
idShows which user and groups you are when setting permissions

chmod Cheatsheet

CommandMeaning
chmod u+x fileAdd execute for the owner
chmod g-w fileRemove write for the group
chmod o=r fileSet others to read-only
chmod a+x fileAdd execute for everyone
chmod 755 fileOwner full; group/others read + execute
chmod 644 fileOwner read/write; others read-only
chmod 600 fileOwner only — private files
chmod 700 dirOwner only, traversable — private directories
chmod -R 755 dir/Apply recursively (preview first!)
chmod +x script.shMake a script executable
ls -l fileRead the current permissions