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.
- Whole sheet251
- Navigate & list13
- Create, copy, move, delete13
- View & compare files9
- Permissions & ownership31
- Help & shell basics11
- Users & groups6
- Install packages7
- Package & software versions29
- System info9
- Network basics & SSH10
- Zip & compress4
- Shell operators6
- Processes6
- Files & search8
- Text processing pipelines4
- Disk3
- Network5
- Services & logs6
- Archives & copying4
- Firewall7
- Scheduling: cron & systemd timers6
- Storage, mounts & LVM10
- Kernel, hardware & limits8
- Performance & tracing10
- TLS & certificates8
- Packet capture & network debugging10
- Terminal tools8
Permissions & ownership (chmod, chown, umask, ACL)31
ls -l <file>Read a permission stringWhat it means
On a directory: r = list names, w = create/delete entries, x = enter it / reach things inside.
What each part does
-rwxr-xr-xexample output; 1st char is the type: - file, d directory, l symlinkrwxchars 2-4, owner (user): r read, w write, x executer-xchars 5-7, group: - means "not allowed"r-xchars 8-10, others (everyone else)s / ts in an x slot = setuid/setgid, t in the last slot = sticky (capital S/T = bit set but no x)+a trailing + means extra ACL entries exist (see getfacl)
stat -c '%a %A %U:%G %n' <file>See permissions as numbersWhat each part does
stat -cprint chosen fields%apermissions in octal, e.g. 755%Athe same as letters, e.g. -rwxr-xr-x%U:%Gowner user and group%nfile name
chmod 755 <file-or-dir>Octal modes explained: 755What each part does
chmodchange mode (permissions)7owner = 4 read + 2 write + 1 execute = rwx5group = 4 read + 1 execute = r-x5others = r-x
Tip Typical for folders, scripts and binaries.
chmod 644 <file>Regular files: owner writes, everyone readsWhat each part does
6owner rw- (4+2)4group r--4others r--
Tip Typical for config files, web content, documents.
chmod 600 <file>Private file: only the ownerWhat each part does
6owner rw-0group: nothing0others: nothing
Tip Use for secrets: .env files, private keys, kubeconfig, credentials.
chmod 700 <dir>Private folder / script: only the ownerWhat each part does
7owner rwx0 0group and others: no access at all
chmod 750 <dir>; chmod 640 <file>Shared with the group, hidden from othersWhat each part does
750dir: owner rwx, group r-x (can enter and read), others none640file: owner rw-, group r--, others none
chmod 400 <file>Read-only for the ownerWhat each part does
4owner r-- only00nobody else
Tip AWS .pem key files are expected to be 400 or 600.
chmod 777 <path>777: everyone can do everything (avoid)DestructiveWhat each part does
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 itWhat each part does
+xadd execute (no who-letter = for all, minus the umask bits)&&then./<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/othersWhat each part does
u+xu = owner (user): add execute,separate clausesg-wg = group: remove writeo-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 permissionsWhat each part does
u=rwxowner gets exactly rwxgo=rxgroup and others get exactly r-x (same as 755)
chmod a+r <file>Give everyone read accessWhat each part does
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
-R--recursive: the folder and everything insideu+rwXowner: read, write, and execute only for folders / files already executablego+rXgroup/others: read, and enter foldersgo-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)CautionWhat each part does
find <dir> -type devery directory under <dir>-exec chmod 755 {} +run chmod on them in batches ({} = the found names)&&then-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 fileWhat each part does
--reference=<source-file>use that file's mode instead of typing one
sudo chown <user> <path>Change the ownerWhat each part does
chown <user>new owner; the group stays the same
sudo chown -R <user>:<group> <path>Change owner and group, recursivelyWhat each part does
-Rrecursive<user>:<group>both at once (no spaces). <user>: alone = also set the user's login group
sudo chgrp -R <group> <path>Change only the groupWhat each part does
chgrpchange group (same as chown :<group>)-Rrecursive
sudo chown -R --from=<old-user> <new-user> <dir>Re-own only files that belong to someone elseWhat each part does
--from=<old-user>change only files currently owned by <old-user><new-user>new owner
sudo chgrp <group> <dir> && sudo chmod 2775 <dir>Shared team folder: new files keep the folder groupWhat each part does
chgrp <group> <dir>folder belongs to the team group2setgid bit: new files/folders inside inherit the folder's group775owner 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 filesWhat each part does
1sticky bit: only the file owner (or dir owner/root) can delete or rename entries777everyone 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
-xdevstay on one filesystem-type ffiles only-perm /4000any file with the setuid bit (runs as its owner, often root)2>/dev/nullhide "Permission denied" noise
find <dir> -type f -perm -o+wFind world-writable filesWhat each part does
-perm -o+w"-" = all listed bits must be set: others have write
umask; umask 027Default permissions for new files (umask)What each part does
umaskshow the current mask (usually 0022)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
setfaclset file access control lists-m--modify: add or change an entryu:<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 filesWhat each part does
-Rapply to existing files recursively-d--default: entries that NEW files and folders inside will inheritrwXX = execute only on folders/executables
getfacl <path>Show ACLsWhat each part does
getfaclowner, group, and every ACL entry with its effective rights (mask applies)
setfacl -x u:<user> <path>; setfacl -b <path>Remove ACL entriesWhat each part does
-x u:<user>remove that user's entry (no permissions in the spec)-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 themWhat each part does
700 ~/.sshonly you can enter or list the folder600 authorized_keysonly you can read/write the list of allowed keys600 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 pathWhat each part does
namei -llist owner, group and mode of each component: missing x on any parent blocks access
Nothing matches.
My DevOps Playbook by Angelo John Calleja