Overly open permissions make a privilege escalation easier: a readable
/etc/shadow exposes password hashes, a world-writable file lets someone
modify a script run by root, a needless setuid binary becomes a springboard.
This guide shows how to set the UMASK, fix the permissions of sensitive system
files, and hunt down risky files (world-writable, setuid/setgid, ownerless).
For Linux administrators, on Debian/Ubuntu and RHEL/AlmaLinux. Aligned with the
CIS Benchmarks and ANSSI-BP-028. All outputs come from a real test.
What you will learn
- Set a restrictive UMASK (027) persistently
- Fix the permissions of authentication files (
passwd,shadow,group) - Hunt down world-writable files and set the sticky bit
- Inventory setuid/setgid binaries
- Find ownerless files
Prerequisites
Root access (or sudo). The find searches walk the whole file system; run
them off-peak on a heavily loaded server, and keep the -xdev option so as
not to descend into network mounts.
Set the UMASK to 027
The UMASK defines the permissions removed from new files. The system default is 022, which leaves files readable by everyone. The reading on AlmaLinux 10 confirms it:
umaskgrep '^UMASK' /etc/login.defs0022UMASK 022On Debian 13, umask also returns 0022, but /etc/login.defs contains
no UMASK line at all any more: the same search returns nothing, and a
replacement sed would therefore change nothing either.
The recommended value is 027 (nothing for "others", read for the group),
or 077 for full isolation. You set it in /etc/login.defs (used by
useradd and pam_umask) and in interactive shells through a dedicated
profile.d file. The command below replaces the line when it exists, and
adds it otherwise (checked on Debian 13 and AlmaLinux 10,
bash -lc umask then returns 0027):
if grep -q '^UMASK' /etc/login.defs; then sudo sed -i 's/^UMASK.*/UMASK\t\t027/' /etc/login.defselse printf 'UMASK\t\t027\n' | sudo tee -a /etc/login.defsfiecho 'umask 027' | sudo tee /etc/profile.d/99-umask.shThis measure matches the ANSSI-BP-028 R36 rule ("Modify the default UMASK value") and the CIS 5.4.3.3 control ("Ensure default user umask is configured").
027 does not allow team work
A frequent confusion deserves to be cleared before applying this mask
everywhere: 027 gives the group read-only access. A file created under
this mask comes out as -rw-r-----, and a second group member who tries
to write to it gets a Permission denied. Checked on AlmaLinux 10, two
accounts in the same group, on a shared directory in 2770.
It is consistent with the definition given above, but the "collaboration
umask" shortcut you read elsewhere is wrong: for co-editing in a
shared directory, you need 007, which gives -rw-rw---- and lets the
group write.
So the choice is not "which one is the safest" but "what is this directory
for": 027 for personal files on a shared server, 007 for a team
space, 077 for strict isolation.
Permissions of authentication files
/etc/passwd and /etc/group must stay readable (many tools depend on
them) but not modifiable by non-root users. /etc/shadow and /etc/gshadow,
which hold the password hashes, must never be readable by an ordinary
user.
The shadow target differs by distribution, and a "common" guide easily
gets this wrong. The reading with stat shows the real difference between
the two families:
# Debian 13/etc/shadow 640 root:shadow# AlmaLinux 10/etc/shadow 0 root:rootOn Debian/Ubuntu, the shadow group exists (the passwd binary is sgid
shadow), so the target is 640 root:shadow. On RHEL/AlmaLinux, it is
0000 root:root: root accesses the file despite mode 000, because the
kernel ignores permissions for root.
The table gives the hardening targets. The third row is not a default
everywhere: on Debian 13, /etc/ssh/sshd_config ships as 644, on
AlmaLinux 10 as 600.
| File | Debian/Ubuntu | RHEL/AlmaLinux |
|---|---|---|
/etc/passwd, /etc/group | 644 root:root | 644 root:root |
/etc/shadow, /etc/gshadow | 640 root:shadow | 0000 root:root |
/etc/ssh/sshd_config | 600 root:root | 600 root:root |
sudo chown root:root /etc/passwd /etc/groupsudo chmod 644 /etc/passwd /etc/groupsudo chown root:shadow /etc/shadow /etc/gshadowsudo chmod 640 /etc/shadow /etc/gshadowsudo chown root:root /etc/passwd /etc/group /etc/shadow /etc/gshadowsudo chmod 644 /etc/passwd /etc/groupsudo chmod 0000 /etc/shadow /etc/gshadowThese controls match CIS 7.1.x ("System File Permissions", numbered 6.1.x in the v1.0.0 benchmarks) and the ANSSI R50 rule ("Restrict access rights to files and directories").
Hunt down world-writable files
A file writable by everyone (o+w) can be altered by any user or
compromised process. A directory that is world-writable without a sticky
bit lets anyone delete other people's files. You search for them with
find, staying on the current file system (-xdev):
# Files writable by everyonesudo find / -xdev -type f -perm -0002
# Directories writable by everyone WITHOUT a sticky bitsudo find / -xdev -type d -perm -0002 ! -perm -1000With find, -perm -0002 means "the others write bit is set" (not to
be confused with -perm /0002), and ! -perm -1000 excludes directories
that already have the sticky bit.
You fix this by removing "others" write access on files, and by setting the sticky bit on shared directories that must stay writable:
sudo find / -xdev -type f -perm -0002 -exec chmod o-w {} +sudo find / -xdev -type d -perm -0002 ! -perm -1000 -exec chmod a+t {} +The /tmp and /var/tmp directories are world-writable on purpose, but
protected by the sticky bit (1777). The lab confirms it: drwxrwxrwt. This
measure covers CIS 7.1.11 and the ANSSI R54 rule ("Enable the sticky
bit on writable directories").
Inventory setuid and setgid binaries
A setuid root binary runs with root privileges, whoever the caller is.
This is necessary for a few tools (passwd, sudo, mount), but every
needless setuid binary is a privilege escalation vector. You inventory them:
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \)The legitimate list depends on the distribution and the installed
packages, and this matters: comparing yours with that of another family
produces false positives. On a Debian 13 with sudo and openssh-server,
the command returns 15 entries:
/usr/bin/chage /usr/bin/gpasswd /usr/bin/ssh-agent/usr/bin/chfn /usr/bin/mount /usr/bin/su/usr/bin/chsh /usr/bin/newgrp /usr/bin/sudo/usr/bin/expiry /usr/bin/passwd /usr/bin/umount/usr/lib/dbus-1.0/dbus-daemon-launch-helper/usr/lib/openssh/ssh-keysign/usr/sbin/unix_chkpwdOn AlmaLinux 10, the same command returns 19 entries, several of which
are missing from the Debian list: pkexec and polkit-agent-helper-1
(PolicyKit), crontab, grub2-set-bootflag, pam_timestamp_check, write,
mount.nfs and utempter.
The practical conclusion does not change, but the method does: do not compare with a list written elsewhere, compare with the fingerprint of your own fleet, taken on a freshly installed machine. This is exactly what the baseline below does.
For a binary judged useless after review, you remove the bit with
chmod u-s (setuid) or chmod g-s (setgid). This approach matches the
ANSSI R56 and R57 rules ("Avoid using executables with the special setuid
and setgid rights").
Find files without an owner
A file whose UID or GID no longer exists (deleted account) can be "recovered" by a future user reusing the same numeric identifier, who would then inherit unexpected rights. You hunt them down:
sudo find / -xdev \( -nouser -o -nogroup \)The parentheses in this command do not change the result: -xdev is a
global option, valid for the whole traversal wherever it is placed, and
find adds a -print of its own around the entire expression.
They become essential as soon as an action follows. In
find / -xdev -nouser -o -nogroup -ls, the -o operator has the lowest
precedence, and -ls only applies to the -nogroup branch. So get into the
habit of grouping alternatives with \( ... \) before adding -ls or
-exec.
After investigation, you reassign each file to a legitimate owner or delete it. This measure covers CIS 7.1.12 and the ANSSI R53 rule ("Avoid files or directories without a known user or group").
Check
These controls are run afterwards, on an already configured system, and they are the only ones that confirm the instruction really took effect.
# UMASKgrep '^UMASK' /etc/login.defs # UMASK 027bash -lc umask # 0027
# Permissions of sensitive filesstat -Lc '%n %a %U:%G' /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/ssh/sshd_config
# The 3 searches must return empty output (apart from known legitimate setuid)sudo find / -xdev -type f -perm -0002sudo find / -xdev -type d -perm -0002 ! -perm -1000sudo find / -xdev \( -nouser -o -nogroup \)Security and points of attention
The three points below are those where overzealous hardening breaks
something, and they look alike: in all three cases, the most restrictive value
is not the right default answer. The mask, the mode of /etc/shadow and
the setuid binaries each require knowing the machine's real use before
deciding.
- UMASK 027, 007 or 077: 027 lets the group read without writing; 007 allows group co-editing; 077 isolates each user, but can get in the way of services that share files by group.
- shadow at
000on RHEL: do not "fix" it to 640 by reflex, it is the expected behaviour and root still accesses it. - setuid: inventory and document, never remove in bulk.
Troubleshooting
These four symptoms show up after the measures are applied, which makes
them hard to tie to their cause. The first two come from a badly chosen
target, mode of /etc/shadow or a profile.d file not reloaded; the last
two from a search that is too broad, which walks network mounts or removes
a legitimately shared right.
| Symptom | Probable cause | Solution |
|---|---|---|
passwd fails for users | wrong group/mode on /etc/shadow | Debian: 640 root:shadow; RHEL: 0000 root:root |
| UMASK still 022 in a shell | profile.d not loaded or login not replayed | check /etc/profile.d/99-umask.sh, open a new session |
find very slow | walking network/mounts | keep -xdev, target local file systems |
A service breaks after chmod o-w | legitimately shared file | restore the needed right, isolate by group |
Put it into practice
The setgid bit on a directory complements the mask choice seen above: it forces new files to inherit the directory's group, and, combined with a 007 umask, makes group co-editing really work. This lab has you build a collaborative directory: creating the group, setting the bit with chmod g+s, and proof through the directory's mode and a file created by a member, without opening any world-writable rights.
Key points
- UMASK 027 in
/etc/login.defs(a line to add on Debian 13) and/etc/profile.d/(ANSSI R36, CIS 5.4.3.3). /etc/shadow:640 root:shadowon Debian,0000 root:rooton RHEL (ANSSI R50, CIS 7.1.x).- Hunt down world-writable files (
find -perm -0002) and set the sticky bit on/tmp(ANSSI R54, CIS 7.1.11). - Inventory the setuid/setgid binaries and compare them with a baseline (ANSSI R56/R57, CIS 7.1.13).
- Find ownerless files (
find \( -nouser -o -nogroup \)) (ANSSI R53, CIS 7.1.12). - Always parenthesise the
-oinfind, and state the CIS benchmark version (7.1.x recent, 6.1.x in v1.0.0).
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: sysctl: harden the Linux kernel and network stack
A new full quiz with random questions
Work again only on the questions you missed