Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

04

Permissions & Ownership

  • Fix permission denied on deploy user
  • Explain rwx bits and ownership
  • Set mode without chmod 777
chmodchownchgrpumaskidnamei

Must-know cold

  • ls -l → mode, owner, group · ls -ld DIR
  • chmod 644 file · chmod 755 dir/script · chmod u+x file
  • chown user:group file · chown -R user:group dir
  • umask · id · never default to 777 / chmod -R 777

Mode quick map

Definition: Common octal modes and what rwx means for files vs directories.

OctalrwxCommon use
644rw-r--r--Normal file
600rw-------Secrets / keys
755rwxr-xr-xDirs / public scripts
750rwxr-x---Dir; group exec, others none
700rwx------Private dir (~/.ssh)
400r--------Read-only secret

Bits:

  • user group other
  • r=4 w=2 x=1
  • Dir x = enter/traverse

Commands

chmod

Definition: Change file mode bits (permissions) using octal or symbolic modes.

OptionArgumentMeaningExample
(none)MODE FILESet mode (octal or symbolic)chmod 644 f
-RMODE PATHRecursivechmod -R u+rwX dir/
-vMODE FILEVerbosechmod -v 755 x
-cMODE FILEReport only when changedchmod -c 644 f
--referenceRFILE FILECopy mode from RFILEchmod --reference=a b

Symbolic MODE: [ugoa][+-=][rwx]
Examples: u+x · go-w · a+r · u=rwx,g=rx,o=

Flag combos

ComboMeaningExample
chmod 600 ~/.ssh/id_rsaPrivate keyRequired for SSH
chmod 700 ~/.sshSSH dir
chmod -R u+rwX,go-w dirSafer recursive than 777Deploy trees

chown

Definition: Change file owner and/or group.

OptionArgumentMeaningExample
(none)USER FILESet ownerchown jenkins f
(none)USER:GROUP FILESet owner and groupchown app:app f
(none):GROUP FILESet group only (same as chgrp)chown :deploy f
-ROWNER PATHRecursivechown -R app:app /opt/app
-hOWNER LINKAffect symlink itself not targetchown -h u:g link
-vOWNER FILEVerbosechown -v u f
--referenceRFILE FILECopy ownership from RFILEchown --reference=a b

Flag combos

ComboMeaningExample
chown -R app:app /var/appService tree ownershipAfter extract
sudo chown root:root fReturn to root

chgrp

Definition: Change the group ownership of a file.

OptionArgumentMeaningExample
(none)GROUP FILEChange group onlychgrp deploy f
-RGROUP PATHRecursivechgrp -R deploy dir
-hGROUP LINKSymlink itselfchgrp -h g link
-vGROUP FILEVerbosechgrp -v g f

Flag combos

ComboMeaningExample
chgrp -R staff /sharedShared project groupWith setgid dir

umask

Definition: Set or display the mask that subtracts permissions from newly created files/dirs.

OptionArgumentMeaningExample
(none)—Print current umaskumask
(none)MODESet umask (subtract from defaults)umask 022
-S—Symbolic displayumask -S
-p—Output reusable as inputumask -p

How to read: umask subtracts from 666 (files) / 777 (dirs).

  • 022 → files 644, dirs 755.
  • 077 → files 600, dirs 700.

Flag combos

ComboMeaningExample
umask 027Group-readable; others noneShared team hosts

id

Definition: Print real/effective UID, GID, and group memberships.

OptionArgumentMeaningExample
(none)—Current UID/GID/groupsid
(none)USERIdentity of USERid jenkins
-u—Effective UID onlyid -u
-g—Effective GID onlyid -g
-gn—Group nameid -gn
-un—User nameid -un
-G—All group IDsid -G
-Gn—All group namesid -Gn
-n—Names instead of numbers (with -ugG)id -un

Flag combos

ComboMeaningExample
id -Gn userGroups for membership debugAfter usermod -aG

namei

Definition: Walk each path component and show permissions (finds where access fails). Optional package — install if missing.

OptionArgumentMeaningExample
(none)PATHResolve each path componentnamei /var/app/conf
-lPATHLong; show owner/mode each stepnamei -l /path
-mPATHMode of each componentnamei -m /path

Flag combos

ComboMeaningExample
namei -l /a/b/cFind which component blocks traversePermission denied RCA

Common recipes

GoalCommand
See mode/ownerls -l file · ls -ld dir
Normal filechmod 644 file
Executable scriptchmod 755 script.sh
Private keychmod 600 key · chmod 700 ~/.ssh
App owns treesudo chown -R app:app /opt/app
Who am I / groupsid · id deploy
Path walknamei -l /var/app/data/file
Current umaskumask

Pitfalls

  • Dir without x for a user → cannot cd or access children even if the file is 777.
  • chmod -R 777 breaks security and often masks the root cause — don’t.
  • Recursive chown on / or the wrong path is catastrophic — verify the path.
  • Group membership changes need a new login (or new session) to apply fully.
  • Symlinks: permissions on the target matter for access; the link mode is often ignored for access checks.

For more details, try man <command> in your terminal.

Previous03 View & Inspect FilesNext05 Find & Locate