Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

File permissions and ownership: harden a Linux system

By updated

Improve this page

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:

Terminal window
umask
grep '^UMASK' /etc/login.defs
0022
UMASK 022

On 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):

Terminal window
if grep -q '^UMASK' /etc/login.defs; then
sudo sed -i 's/^UMASK.*/UMASK\t\t027/' /etc/login.defs
else
printf 'UMASK\t\t027\n' | sudo tee -a /etc/login.defs
fi
echo 'umask 027' | sudo tee /etc/profile.d/99-umask.sh

This 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:root

On 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.

FileDebian/UbuntuRHEL/AlmaLinux
/etc/passwd, /etc/group644 root:root644 root:root
/etc/shadow, /etc/gshadow640 root:shadow0000 root:root
/etc/ssh/sshd_config600 root:root600 root:root
Terminal window
sudo chown root:root /etc/passwd /etc/group
sudo chmod 644 /etc/passwd /etc/group
sudo chown root:shadow /etc/shadow /etc/gshadow
sudo chmod 640 /etc/shadow /etc/gshadow

These 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):

Terminal window
# Files writable by everyone
sudo find / -xdev -type f -perm -0002
# Directories writable by everyone WITHOUT a sticky bit
sudo find / -xdev -type d -perm -0002 ! -perm -1000

With 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:

Terminal window
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:

Terminal window
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_chkpwd

On 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:

Terminal window
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.

Terminal window
# UMASK
grep '^UMASK' /etc/login.defs # UMASK 027
bash -lc umask # 0027
# Permissions of sensitive files
stat -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 -0002
sudo find / -xdev -type d -perm -0002 ! -perm -1000
sudo 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 000 on 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.

SymptomProbable causeSolution
passwd fails for userswrong group/mode on /etc/shadowDebian: 640 root:shadow; RHEL: 0000 root:root
UMASK still 022 in a shellprofile.d not loaded or login not replayedcheck /etc/profile.d/99-umask.sh, open a new session
find very slowwalking network/mountskeep -xdev, target local file systems
A service breaks after chmod o-wlegitimately shared filerestore 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:shadow on Debian, 0000 root:root on 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 -o in find, 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

6 questions
6 min
70% to pass

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