Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chmod

Change the permissions (mode) of files and directories, with numbers or letters.

Level
RHCSA exam
Group
Permissions
Needs root
For changes
Package
coreutils

What it does

Permissions are three groups of r w x: for the owner (u), the group (g) and others (o). Use numbers (chmod 750 dir, r = 4, w = 2, x = 1) or letters (chmod g+w,o-rwx dir).

A fourth digit in front sets the special bits: 4 = SUID, 2 = SGID, 1 = sticky. Only the owner of a file (or root) may change its mode.

How to write it

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

Main parts

u g o a
Who: user (owner), group, others, all.
+ - =
Add, remove, set exactly.
r w x
Read, write, execute.
X
x only for directories and files that already have an x.
s
SUID (with u) or SGID (with g).
t
Sticky bit.
NNN / NNNN
Numbers: owner, group, others (and the special bits in front).

Options 9

Most used

-R, --recursive
Change the whole tree.
-v, --verbose
Report every file.
-c, --changes
Report only changes.
-f, --silent, --quiet
Do not show most errors.
--reference=RFILE
Use the mode of RFILE.
--preserve-root
Refuse to work recursively on /.
--no-preserve-root
Do not treat / special (default).
--help
Show help.
--version
Show the version.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Make a script executable

    chmod +x backup.sh
    ./backup.sh

    +x = a+x (filtered by the umask).

  2. 2Normal file and private file

    chmod 644 index.html
    chmod 600 ~/.ssh/id_ed25519

    SSH refuses private keys that others can read.

  3. 3Team folder without access for others

    chmod 770 /srv/sales

    Owner and group full, others nothing.

  4. 4Shared folder with SGID and sticky

    chmod 3770 /srv/sales
    ls -ld /srv/sales
    drwxrws--T. 2 root sales 6 Oct 25 10:40 /srv/sales

    New files get the group; users delete only their own files.

  5. 5Add and remove in one command

    chmod g+w,o-rwx shared.txt

    Letters change only what you name.

  6. 6Give folders x but not files

    chmod -R u=rwX,g=rX,o= /srv/project

    Capital X: folders get x, normal files do not.

  7. 7Copy the mode of another file

    chmod --reference=index.html about.html

    Same permissions.

  8. 8Remove a dangerous SUID bit

    chmod u-s /usr/local/bin/oldtool

    SUID programs run as their owner — a big risk if the owner is root.

  9. 9Find world-writable files (security)

    find / -xdev -type f -perm -0002 -ls 2>/dev/null

    Anybody can change these files.

  10. 10Find SUID and SGID programs (security)

    find / -xdev -type f \( -perm -4000 -o -perm -2000 \) -ls 2>/dev/null

    Compare the list with a known good list.

  11. 11Protect a config with secrets

    chmod 640 /etc/myapp/db.conf; chgrp myapp /etc/myapp/db.conf

    The service group reads it; others cannot.

  12. 12See the mode as a number

    stat -c '%a %A %n' /etc/shadow /tmp
    0 ---------- /etc/shadow
    1777 drwxrwxrwt /tmp

    %a = number, %A = letters.

Watch out

  • chmod -R 777 is never a fix: it gives everybody full access.
  • chmod -R 644 dir removes the x from folders: nobody can enter them. Use X.
  • S or T in ls means the special bit without x (often a mistake, except on a folder with no access for others).

Exit codes

0Done.
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man chmod