Creating a sudo user looks like a one-line operation: adduser ops && usermod -aG sudo ops. In practice the choices around that one line — which group, what shell, what default umask, how the sudoers file is structured, and how the user gets revoked when they leave — are what separate a fleet you can audit from one you cannot. This article documents the pattern we apply on every host.
How to verify
Before adding anything, inspect what is already in place.
getent group sudo wheel admin 2>/dev/null
ls -l /etc/sudoers.d/ 2>/dev/null
cat /etc/sudoers | grep -E '^(%|#includedir)'
id ops 2>/dev/null || echo "ops not present"
getent group tells you which sudo-granting group actually exists — Ubuntu and Debian use sudo, RHEL/CentOS/Fedora and openSUSE use wheel. The contents of /etc/sudoers.d/ are where you should put per-user rules; the monolithic /etc/sudoers should stay as the distro shipped it.
What’s happening
Sudo grants privilege escalation based on rules in /etc/sudoers and any file in /etc/sudoers.d/. The conventional pattern is: add the user to a group (sudo on Debian-family, wheel on RHEL-family) that the distro has already configured for blanket sudo. That works, but it gives the user full root via sudo — every command, with a password prompt by default. For an operator account that is acceptable; for a service account or a partial-privilege role, it is not.
The drop-in file approach (/etc/sudoers.d/10-ops) is more granular: you grant specific commands or NOPASSWD only where it matters, you can version-control the file, and you can revoke the user by removing the file without touching the monolithic config. This is the pattern we use for any privilege that is more nuanced than “full operator.”
A second consideration: PAM. On most distros, pam_wheel.so or pam_unix.so is what gates su to root. Adding the user to the sudo-granting group is necessary but not sufficient if PAM has been customized — always test from a fresh session.
The procedure
-
Identify the distro’s sudo-granting group. Debian/Ubuntu:
sudo. RHEL/Fedora/Rocky/Alma:wheel. Arch and others may vary; check/etc/sudoersfor the active%groupline.grep -E '^%(sudo|wheel|admin)' /etc/sudoers -
Create the user with a real home directory and bash as the login shell. On Debian-family,
adduseris interactive and sets sensible defaults. The--gecos ""skips the comment prompts.sudo adduser --gecos "" --shell /bin/bash opsOn RHEL-family,
useraddis the lower-level command and does not set up the home directory unless you ask.sudo useradd -m -s /bin/bash -c "Operator" ops sudo passwd ops # only if you intend to use a password; key-only login does not need one -
Add the user to the sudo-granting group.
# Debian/Ubuntu sudo usermod -aG sudo ops # RHEL/Fedora/Rocky/Alma sudo usermod -aG wheel opsThe
-ais critical — without it,usermod -Greplaces the user’s group memberships instead of appending. -
Set up the SSH directory with correct permissions. sshd refuses keys with loose permissions and gives almost no log feedback about why.
sudo install -d -m 0700 -o ops -g ops /home/ops/.ssh sudo install -m 0600 -o ops -g ops /dev/stdin /home/ops/.ssh/authorized_keys <<< "ssh-ed25519 AAAA... operator-key"See ssh-key-authentication for the deeper key-management discussion.
-
Verify sudo works from a fresh session. Do not test from the existing root session; PAM behaviour can differ.
ssh ops@host -- 'sudo -v && sudo whoami'Should prompt for the user’s password (or skip the prompt if you intend
NOPASSWD), then printroot. If it fails withops is not in the sudoers file, the group membership did not take effect — log out and back in to refresh group state. -
For granular grants, drop a file in
/etc/sudoers.d/. This is the pattern for service automation or limited roles. Always edit throughvisudo -f; a syntax error in any file under/etc/sudoers.d/breaks sudo for everyone.sudo visudo -f /etc/sudoers.d/20-ops-restartContents:
# Allow ops to restart application services without a password prompt ops ALL=(root) NOPASSWD: /bin/systemctl restart app.service, /bin/systemctl reload nginx Defaults:ops !requirettyFile mode must be
0440, ownerroot:root— visudo enforces this but only if you usedvisudo -f. A file with the wrong mode in/etc/sudoers.d/is silently ignored.sudo chmod 0440 /etc/sudoers.d/20-ops-restart sudo ls -l /etc/sudoers.d/20-ops-restart -
Set the default umask for the operator. Cloud images vary — some default to
022(group/world readable), some0077. For an operator account,0027is a reasonable middle ground: the user can read their own files, the group (which is the user’s primary group) can too, others cannot.Append to
/home/ops/.bashrc(or set in/etc/login.defs):echo 'umask 0027' | sudo tee -a /home/ops/.bashrc -
Document and version-control the change. The sudoers drop-in and the user creation belong in your Ansible / Terraform / Pulumi state, not a manual SSH session.
# Ansible task - name: Ensure ops operator ansible.builtin.user: name: ops shell: /bin/bash groups: sudo append: yes home: /home/ops create_home: yes state: present
Common pitfalls
- Forgetting the
-aflag onusermod -G: it silently removes the user from every group not listed, includingusers,docker, etc. - Editing
/etc/sudoersdirectly withnanoinstead ofvisudo— one syntax error and you have no working sudo until you boot single-user. - Setting a sudoers drop-in file with the wrong mode (
0644for example); sudo silently ignores it and the user gets nothing. - Granting
NOPASSWD: ALLto an operator account that is supposed to require MFA elsewhere — bypasses your audit story. - Adding the user to
sudobut not logging the session out and back in; group membership is evaluated at login.
In our managed engagements, every host has a small, named set of operator accounts plus a service account per automated process — defined once in code, applied uniformly across the fleet. If your servers have accumulated ad-hoc accounts and undocumented sudoers entries, an audit-and-consolidation pass is part of the Managed Operations onboarding.