umask 077 vs chmod o-rwx for securing directories
0 reputation · 29 Dec 2022, 00:41 UTC
0 reputation · 29 Dec 2022, 00:41 UTC
The objective is to ensure newly created files and directories cannot be read or executed by group or others, thereby avoiding accidental public access on Unix systems.
Umask 077 applies a default mask at process creation, automatically restricting permissions, but it propagates to subshells and scripts, risking leaks if not reset. Explicit `chmod o-rwx` removes other permissions only on selected directories, requiring manual changes and potentially conflicting with set‑uid or set‑gid binaries.
When should an administrator rely on a system‑wide umask versus per‑directory chmod? How can temporary elevation be safely reset after script execution?
35275 reputation · 29 Dec 2022, 05:37 UTC
Use system-wide umask 077 as a baseline for all interactive sessions and services when you want every new file and directory to default to 600/700 (owner-only). Use per-directory chmod o-rwx (or default ACLs) for specific paths that must stay private regardless of the creating process's umask—such as shared application data directories, /home/user/private, or any location written by multiple users or automated jobs.
For temporary elevation inside a script, run the looser-umask block in a subshell so the parent shell's umask is never altered: ( umask 022; commands... ). If you must change umask in the current shell, save it first and restore it in a trap on EXIT.
/etc/profile, /etc/login.defs UMASK, or PAM pam_umask.so).rw------- (600); new directories become rwx------ (700).chmod o-rwx / Default ACLschmod o-rwx /path strips existing "others" bits only; it does not affect future creations.setfacl -d -m o::0 /path (requires filesystem ACL support: ext4/xfs/btrfs with acl mount option).--acls, -A) are used.( umask 022
# commands that need group/other readability
create_shared_cache
)The parent shell's umask is untouched.old_umask=$(umask)
umask 022
trap 'umask "$old_umask"' EXIT
# commands...
# umask restored automatically on exit, error, or signal. script.sh shares the process).umask -S — shows current mask symbolically (expect u=rwx,g=,o= for 077).touch testfile && mkdir testdir && ls -l — confirms 600/700 on new objects.getfacl -d /target/dir — verifies default ACLs are present.stat -c '%a %n' /path/* — audits actual permissions on existing entries.What is the current system-wide umask (run umask in a fresh login shell) and does the target filesystem support ACLs (mount | grep acl)? The answer determines whether a global umask 077 is viable or if you must rely on default ACLs for shared directories.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.