Sub topic 14
User accounts, shells, and session environment
This chapter covers the account and session recommendations in a CIS Benchmark. It assumes you can read /etc/passwd and /etc/shadow, you know what a login shell is, and you have root or sudo on a test machine. Password quality, lockout, and sudo are in the PAM chapter. This chapter covers who may log in, with what shell, and what environment they get.
Section numbers differ between benchmark versions and distributions, so this chapter names each check by what it does.
What the benchmark checks
The checks fall into three groups:
- Account integrity: only root has UID 0, root's primary group is GID 0, and no account has an empty password.
- Account use: system accounts cannot log in interactively, and accounts that sit unused get locked.
- Session environment: a restrictive default umask, an idle timeout for interactive shells, and a trustworthy
PATHfor root.
Audit account integrity
Run these read-only checks first. Each should print nothing, except the UID 0 check, which should print only root.
# Accounts with UID 0 (expected: root only)
awk -F: '($3 == 0) { print $1 }' /etc/passwd
# Root's primary group (expected: 0)
awk -F: '$1 == "root" { print $4 }' /etc/passwd
# Accounts with an empty password field
sudo awk -F: '($2 == "") { print $1 }' /etc/shadowA second UID 0 account is a hidden root login, and so is an account with an empty password field. Remove or lock either one. If root's GID is not 0, repair it with sudo usermod -g 0 root.
Keep system accounts out of interactive use
Accounts such as daemon, postgres, or nginx exist so services can own files and run processes. They should not accept a login shell. List any system account that has one:
UID_MIN=$(awk '/^\s*UID_MIN/ { print $2 }' /etc/login.defs)
awk -F: -v min="$UID_MIN" '
($3 < min && $1 != "root" && $1 != "sync" && $1 != "shutdown" && $1 != "halt" &&
$7 != "/usr/sbin/nologin" && $7 != "/sbin/nologin" && $7 != "/bin/false") {
print $1, $3, $7
}' /etc/passwdThe script reads UID_MIN from /etc/login.defs rather than hard-coding 1000, because some distributions use 500. It skips root and the three shutdown-style accounts that the benchmark exempts.
Fix each result with sudo usermod -s /usr/sbin/nologin <account>. On Red Hat family systems the path is /sbin/nologin. Then confirm the service still starts, because a few services use su to the account and need a real shell. For those, use systemd options or runuser -s instead of restoring the shell.
The benchmark also says nologin must not be listed in /etc/shells. Some tools treat a shell listed there as valid, which weakens the control. Check with grep nologin /etc/shells.
Lock unused accounts
Accounts that sit idle after their password expires are a common foothold. Set the default inactivity period for new accounts and apply it to existing ones:
# Default for accounts created from now on (benchmark: 30 days or less)
sudo useradd -D -f 30
useradd -D | grep INACTIVE
# Apply to an existing account, then review it
sudo chage --inactive 30 alice
sudo chage -l aliceEach user must be done individually, because useradd -D affects only future accounts.
Set a restrictive default umask
The umask removes permission bits from newly created files. The benchmark wants 027 or stricter. That gives owner full access, group read-only, and others nothing.
# /etc/profile.d/60-cis-umask.sh
umask 027Make the file readable with sudo chmod 644 /etc/profile.d/60-cis-umask.sh. Also check UMASK in /etc/login.defs, and on Red Hat systems check /etc/bashrc. Several files can set a umask, and the last one to run wins. Scanners flag any file that sets a looser value.
Naming the file 60-cis-... puts it in a predictable order. A user's own profile runs last, so a user can still loosen their umask there. The readonly trick in the next section does not work for umask, which is why the scanner also checks home directory files.
Add an idle timeout
An unattended root shell is an easy target. Setting TMOUT makes bash exit after that many seconds with no input.
# /etc/profile.d/60-cis-tmout.sh
readonly TMOUT=900
export TMOUTThe readonly keyword stops a user from running TMOUT=0 to disable it. The timeout applies only to an interactive bash prompt. It does not end a long-running command, and it does not replace the ClientAliveInterval settings in the SSH chapter, which handle dead network connections.
Test it by opening a new login shell and running echo $TMOUT. Then try TMOUT=0 and confirm bash reports it as read-only.
Check root's PATH
If root's PATH contains an empty entry, a ., or a directory that other users can write to, a user can plant a program named like a common command and wait for root to run it. This script checks for those cases:
#!/usr/bin/env bash
# /usr/local/sbin/check-root-path.sh
echo "$PATH" | tr ':' '\n' | while IFS= read -r dir; do
if [ -z "$dir" ] || [ "$dir" = "." ]; then
echo "BAD: empty or current-directory entry"
elif [ ! -d "$dir" ]; then
echo "WARN: $dir does not exist"
elif [ -n "$(find "$dir" -maxdepth 0 -perm /022)" ]; then
echo "BAD: $dir is group- or world-writable"
fi
doneRun it as root's login environment with sudo -i bash /usr/local/sbin/check-root-path.sh. Be aware that secure_path in sudoers can override the PATH that sudo passes, so this approximates what an actual root login sees.
Common mistakes
- Disabling a shell before checking the service. Some daemons need a real shell for helper scripts. Test after each change.
- Changing the template but not existing users.
useradd -Dand/etc/skelaffect only new accounts. Loop over existing accounts for inactivity and home directory checks. - Putting settings in
/etc/profiledirectly. Package updates can overwrite it. Use/etc/profile.d/files instead. - Forgetting non-bash shells.
TMOUTand the profile scripts apply to bash and compatible shells. If you permit another shell, check its own startup files.
Exercise
A scanner reports that postgres has the login shell /bin/bash. You change it to /usr/sbin/nologin, and the next day a maintenance script that runs su - postgres -c "pg_dump ..." fails. What are two ways to resolve this while keeping the benchmark check passing?
Show answer
Run the job without needing the account's login shell. Use sudo -u postgres pg_dump ..., or runuser -u postgres -- pg_dump ..., or run the job from a systemd service or timer with User=postgres. Each of these runs the command as that account without a login shell, so the shell stays nologin and the check passes. If the job cannot be changed, record an exception for that account that names the owner, the reason, and a review date.