Skip to content

Linux & shell: Permissions & ownership (chmod, chown, umask, ACL)

31 Linux & shell commands: Permissions & ownership (chmod, chown, umask, ACL). Click a command to see what each part does.

Index
31 items
Click a command to see what each part does.

Permissions & ownership (chmod, chown, umask, ACL)31

  • ls -l <file>Read a permission string

    What it means

    On a directory: r = list names, w = create/delete entries, x = enter it / reach things inside.

    What each part does

    1. -rwxr-xr-xexample output; 1st char is the type: - file, d directory, l symlink
    2. rwxchars 2-4, owner (user): r read, w write, x execute
    3. r-xchars 5-7, group: - means "not allowed"
    4. r-xchars 8-10, others (everyone else)
    5. s / ts in an x slot = setuid/setgid, t in the last slot = sticky (capital S/T = bit set but no x)
    6. +a trailing + means extra ACL entries exist (see getfacl)
  • stat -c '%a %A %U:%G %n' <file>See permissions as numbers

    What each part does

    1. stat -cprint chosen fields
    2. %apermissions in octal, e.g. 755
    3. %Athe same as letters, e.g. -rwxr-xr-x
    4. %U:%Gowner user and group
    5. %nfile name
  • chmod 755 <file-or-dir>Octal modes explained: 755

    What each part does

    1. chmodchange mode (permissions)
    2. 7owner = 4 read + 2 write + 1 execute = rwx
    3. 5group = 4 read + 1 execute = r-x
    4. 5others = r-x

    Tip Typical for folders, scripts and binaries.

  • chmod 644 <file>Regular files: owner writes, everyone reads

    What each part does

    1. 6owner rw- (4+2)
    2. 4group r--
    3. 4others r--

    Tip Typical for config files, web content, documents.

  • chmod 600 <file>Private file: only the owner

    What each part does

    1. 6owner rw-
    2. 0group: nothing
    3. 0others: nothing

    Tip Use for secrets: .env files, private keys, kubeconfig, credentials.

  • chmod 700 <dir>Private folder / script: only the owner

    What each part does

    1. 7owner rwx
    2. 0 0group and others: no access at all
  • chmod 750 <dir>; chmod 640 <file>Shared with the group, hidden from others

    What each part does

    1. 750dir: owner rwx, group r-x (can enter and read), others none
    2. 640file: owner rw-, group r--, others none
  • chmod 400 <file>Read-only for the owner

    What each part does

    1. 4owner r-- only
    2. 00nobody else

    Tip AWS .pem key files are expected to be 400 or 600.

  • chmod 777 <path>777: everyone can do everything (avoid)Destructive

    What each part does

    1. 7 7 7rwx for owner, group and ALL other users

    Tip Any user or compromised process can change or replace the file, or delete files in the folder. Fix ownership or group instead (chown / chgrp + 775 or 770).

  • chmod +x <script>.sh && ./<script>.shMake a script executable and run it

    What each part does

    1. +xadd execute (no who-letter = for all, minus the umask bits)
    2. &&then
    3. ./<script>.shrun it from the current folder (. is not on PATH)
  • chmod u+x,g-w,o-rwx <file>Symbolic: add or remove for owner/group/others

    What each part does

    1. u+xu = owner (user): add execute
    2. ,separate clauses
    3. g-wg = group: remove write
    4. o-rwxo = others: remove everything

    Tip Who: u, g, o, a (all). Operator: + add, - remove, = set exactly. Perms: r w x X s t.

  • chmod u=rwx,go=rx <path>Symbolic: set exact permissions

    What each part does

    1. u=rwxowner gets exactly rwx
    2. go=rxgroup and others get exactly r-x (same as 755)
  • chmod a+r <file>Give everyone read access

    What each part does

    1. a+ra = all (owner, group, others): add read
  • chmod -R u+rwX,go+rX,go-w <dir>Recursive, the safe way (capital X)

    What each part does

    1. -R--recursive: the folder and everything inside
    2. u+rwXowner: read, write, and execute only for folders / files already executable
    3. go+rXgroup/others: read, and enter folders
    4. go-wgroup/others: no write

    Tip X (capital) avoids making every plain file executable, which chmod -R 755 would do.

  • find <dir> -type d -exec chmod 755 {} + && find <dir> -type f -exec chmod 644 {} +Folders 755, files 644 (fix a messy tree)Caution

    What each part does

    1. find <dir> -type devery directory under <dir>
    2. -exec chmod 755 {} +run chmod on them in batches ({} = the found names)
    3. &&then
    4. -type f … chmod 644every regular file gets 644

    Tip Re-add +x afterwards to scripts that need it.

  • chmod --reference=<source-file> <file>Copy permissions from another file

    What each part does

    1. --reference=<source-file>use that file's mode instead of typing one
  • sudo chown <user> <path>Change the owner

    What each part does

    1. chown <user>new owner; the group stays the same
  • sudo chown -R <user>:<group> <path>Change owner and group, recursively

    What each part does

    1. -Rrecursive
    2. <user>:<group>both at once (no spaces). <user>: alone = also set the user's login group
  • sudo chgrp -R <group> <path>Change only the group

    What each part does

    1. chgrpchange group (same as chown :<group>)
    2. -Rrecursive
  • sudo chown -R --from=<old-user> <new-user> <dir>Re-own only files that belong to someone else

    What each part does

    1. --from=<old-user>change only files currently owned by <old-user>
    2. <new-user>new owner
  • sudo chgrp <group> <dir> && sudo chmod 2775 <dir>Shared team folder: new files keep the folder group

    What each part does

    1. chgrp <group> <dir>folder belongs to the team group
    2. 2setgid bit: new files/folders inside inherit the folder's group
    3. 775owner and group rwx, others r-x

    Tip Symbolic form: chmod g+s <dir>.

  • sudo chmod 1777 <dir>Sticky folder: users can only delete their own files

    What each part does

    1. 1sticky bit: only the file owner (or dir owner/root) can delete or rename entries
    2. 777everyone can create files

    Tip This is how /tmp is set up (drwxrwxrwt). Symbolic: chmod +t <dir>.

  • sudo find / -xdev -type f -perm /4000 2>/dev/nullFind setuid programs (security audit)

    What each part does

    1. -xdevstay on one filesystem
    2. -type ffiles only
    3. -perm /4000any file with the setuid bit (runs as its owner, often root)
    4. 2>/dev/nullhide "Permission denied" noise
  • find <dir> -type f -perm -o+wFind world-writable files

    What each part does

    1. -perm -o+w"-" = all listed bits must be set: others have write
  • umask; umask 027Default permissions for new files (umask)

    What each part does

    1. umaskshow the current mask (usually 0022)
    2. umask 027set it for this shell: bits in the mask are REMOVED from new files

    Tip New files start from 666, folders from 777. 022 → files 644, dirs 755. 027 → 640 / 750. 077 → 600 / 700. Put it in ~/.bashrc or the service unit (UMask=) to keep it.

  • setfacl -m u:<user>:rwx <path>Give one extra user access (ACL)

    What each part does

    1. setfaclset file access control lists
    2. -m--modify: add or change an entry
    3. u:<user>:rwxentry: u = user, then the name, then permissions (g:<group>:rx for a group)
  • setfacl -R -m u:<user>:rwX <dir> && setfacl -d -m u:<user>:rwX <dir>ACL for everything inside, and for future files

    What each part does

    1. -Rapply to existing files recursively
    2. -d--default: entries that NEW files and folders inside will inherit
    3. rwXX = execute only on folders/executables
  • getfacl <path>Show ACLs

    What each part does

    1. getfaclowner, group, and every ACL entry with its effective rights (mask applies)
  • setfacl -x u:<user> <path>; setfacl -b <path>Remove ACL entries

    What each part does

    1. -x u:<user>remove that user's entry (no permissions in the spec)
    2. -b--remove-all: drop every extra entry, keep owner/group/others
  • chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_ed25519SSH files: fix permissions so sshd accepts them

    What each part does

    1. 700 ~/.sshonly you can enter or list the folder
    2. 600 authorized_keysonly you can read/write the list of allowed keys
    3. 600 id_ed25519private key readable only by you (public .pub can be 644)

    Tip With StrictModes (default yes) sshd refuses keys if these are group/world-writable.

  • namei -l <path>Why is access denied? Check every folder on the path

    What each part does

    1. namei -llist owner, group and mode of each component: missing x on any parent blocks access

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…