Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

chmod

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

Niveau
RHCSA-examen
Groep
Rechten
Root nodig
Voor wijzigingen
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Belangrijkste delen

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

Opties 9

Meest gebruikt

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

Voorbeelden 12

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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).

Exitcodes

0 :: Done. 1 :: Error.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man chmod