Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

13

Users, Groups & sudo

  • Create service user
  • Add to group without wiping (-aG)
  • sudo -l / visudo safety
idgetentuseraddusermoduserdelgroupaddpasswdchagesudosession helpers

Must-know cold

  • id · id USER · getent passwd USER · getent group GROUP
  • sudo useradd -m -s /bin/bash USER · sudo passwd USER
  • sudo usermod -aG GROUP USER (-a when appending; bare -G replaces the list)
  • sudo visudo / visudo -f for sudoers · sudo -l
  • Group changes apply to new sessions (id USER updates immediately; id in this shell does not)

Key files

Definition: Core identity files: passwd, shadow, group, sudoers.

FileRole
/etc/passwdUser account metadata (hash field is just x)
/etc/shadowPassword hashes (root + group shadow)
/etc/groupGroups (also /etc/gshadow)
/etc/sudoers + /etc/sudoers.d/sudo policy. Drop-in names must not contain a . (deploy OK, deploy.conf ignored)

Commands

id

Definition: Show user identity and group memberships for access debugging.

OptionArgumentMeaningExample
(none)—Current user identityid
(none)USERThat user’s identityid jenkins
-u / -un—UID / usernameid -un
-g / -gn—Primary GID / nameid -gn
-G / -Gn—All groups IDs / namesid -Gn deploy

Flag combos

ComboMeaningExample
id USER / id -Gn USERDatabase membership (updates immediately)
id / id -GnThis session’s groups (stale until re-login)

getent

Definition: Query system databases (passwd, group, hosts) via Name Service Switch.

OptionArgumentMeaningExample
passwdUSERResolve user via nss (local/LDAP)getent passwd jenkins
groupGROUPResolve groupgetent group docker
shadowUSERShadow line (if permitted)sudo getent shadow USER
hostsNAMEHost resolutiongetent hosts api

Flag combos

ComboMeaningExample
getent passwd USERPrefer over grepping /etc/passwdNetwork accounts

useradd

Definition: Create a new user account (low-level; non-interactive).

OptionArgumentMeaningExample
(none)USERCreate user (defaults vary by distro)sudo useradd app
-mUSERCreate home directorysudo useradd -m app
-dHOME USERHome pathsudo useradd -m -d /opt/app app
-sSHELL USERLogin shellsudo useradd -s /bin/bash app
-s/usr/sbin/nologin USERNo login shell (/sbin/nologin on some distros). sudo -u and SSH forced commands still worksudo useradd -r -s /usr/sbin/nologin svc
-gGROUP USERPrimary groupsudo useradd -g app app
-GG1,G2 USERSupplementary groups at createsudo useradd -G docker app
-uUID USERSpecific UIDsudo useradd -u 1100 app
-rUSERSystem account (low UID)sudo useradd -r svc
-cCOMMENT USERGECOS/comment fieldsudo useradd -c 'CI agent' agent
-eYYYY-MM-DD USERAccount expiry datesudo useradd -e 2026-12-31 c

Flag combos

ComboMeaningExample
useradd -m -s /bin/bash -c 'Dev' devuserHuman account baseline
useradd -r -s /usr/sbin/nologin -d /nonexistent svcService user

Debian often prefers adduser (friendlier, interactive); useradd is the portable low-level tool.

usermod

Definition: Modify an existing user (groups, shell, lock, home, rename).

OptionArgumentMeaningExample
-aGGROUP USERAppend supplementary group(s)sudo usermod -aG docker u
-GG1,G2 USERReplace supplementary list (primary -g is unchanged)Dangerous without -a
-lNEW OLDRename login only (home/mail/group stay)sudo usermod -l new old
-dHOME USERHome pathsudo usermod -d /new/home u
-m—With -d: move home contentssudo usermod -d /new -m u
-sSHELL USERChange shellsudo usermod -s /bin/bash u
-LUSERLock password (! prefix). SSH keys still worksudo usermod -L u
-UUSERUnlock passwordsudo usermod -U u
-eDATE USERExpirysudo usermod -e 2026-01-01 u
-gGROUP USERPrimary groupsudo usermod -g staff u
-cCOMMENT USERCommentsudo usermod -c 'x' u

Flag combos

ComboMeaningExample
usermod -aG GROUP USERSafe add groupAlways -a with -G for add
usermod -L USERLock without deleteOffboarding temp

userdel

Definition: Delete a user account (optionally remove home).

OptionArgumentMeaningExample
(none)USERDelete account; keep home by defaultsudo userdel u
-rUSERRemove home + mail spoolsudo userdel -r u
-fUSERForce even if logged in (dangerous)Avoid casually

Flag combos

ComboMeaningExample
Stop processes firstpkill -u USER then userdel -rClean removal

groupadd

Definition: Create/delete groups and manage membership (groupdel, gpasswd).

CommandArgumentMeaningExample
groupaddGROUPCreate groupsudo groupadd deploy
groupadd -gGID GROUPSpecific GIDsudo groupadd -g 2001 deploy
groupdelGROUPDelete group (fails if it is someone’s primary group)sudo groupdel deploy
gpasswd -aUSER GROUPAdd user to groupsudo gpasswd -a u deploy
gpasswd -dUSER GROUPRemove user from groupsudo gpasswd -d u deploy
gpasswd -AUSER GROUPGroup adminsRare

Flag combos

ComboMeaningExample
groupadd deploy && usermod -aG deploy uNew shared group

passwd

Definition: Set or lock a user’s login password.

OptionArgumentMeaningExample
(none)—Change your passwordpasswd
(none)USERSet USER password (root)sudo passwd jenkins
-lUSERLock password (not the account; SSH keys still work)sudo passwd -l u
-uUSERUnlock password (fails if there was never a hash)sudo passwd -u u
-dUSERDelete password (empty; careful)Avoid
-eUSERExpire password; force change next loginsudo passwd -e u
-SUSERStatus of passwordsudo passwd -S u
-nDAYS USERMin days between changessudo passwd -n 1 u
-xDAYS USERMax password agesudo passwd -x 90 u

Flag combos

ComboMeaningExample
passwd -e USERForce reset on next loginContractor onboarding

chage

Definition: View or change password aging and account expiry.

OptionArgumentMeaningExample
-lUSERList ageing infosudo chage -l u
-EDATE USERAccount expiresudo chage -E 2026-12-31 u
-MDAYS USERMax password agesudo chage -M 90 u
-mDAYS USERMin password agesudo chage -m 1 u
-WDAYS USERWarn before expirysudo chage -W 7 u
-IDAYS USERInactive after password expires (not account -E)sudo chage -I 14 u
-dDATE USERLast password change. 0 ≈ force change next login (passwd -e)sudo chage -d 0 u

Flag combos

ComboMeaningExample
chage -l USERAudit expiry

sudo

Definition: Run commands as another user (usually root); edit sudoers safely with visudo.

Option / commandArgumentMeaningExample
sudoCMDRun CMD as root (default)sudo systemctl status nginx
sudo -uUSER CMDRun as another usersudo -u jenkins whoami
sudo -i—Login shell as target (usually root): HOME/cwd/profile like a real loginsudo -i
sudo -s—Shell as target (env_reset still applies; not a login)sudo -s
sudo -l—List your allowed sudo rulessudo -l
sudo -k—Invalidate cached credentialssudo -k
sudo -v—Extend/validate timestampsudo -v
sudo -nCMDNon-interactive; fail if password neededsudo -n true
sudo -ECMDPreserve environment (careful)sudo -E cmd
sudo -gGROUP CMDSet groupRare
visudo—Edit sudoers with syntax checksudo visudo
visudo -fFILEEdit drop-in under sudoers.dsudo visudo -f /etc/sudoers.d/deploy
visudo -c—Check syntax onlysudo visudo -c

Flag combos

ComboMeaningExample
sudo -u app -H bash -lc 'cmd'Simulate service userWorks-manual, fails-systemd RCA
Never chmod 777 instead of sudo designLeast privilege

sudoers idea (narrow): deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart myapp — avoid NOPASSWD: ALL.

session helpers

Definition: who / w / last helpers for who is logged in and recent logins.

CommandArgumentMeaningExample
whoami—Effective usernamewhoami
who—Logged-in userswho
w—Who + load + what runningw
last—Login historylast -n 20
lastlog—Last login per userlastlog | head
groups—Groups of current/usergroups · groups u

Common recipes

GoalCommand
Create human usersudo useradd -m -s /bin/bash devuser && sudo passwd devuser
Service usersudo useradd -r -s /usr/sbin/nologin -d /nonexistent svc
Add to docker groupsudo usermod -aG docker $USER then re-login
Verify groupsid -Gn USER
Lock passwordsudo usermod -L USER or passwd -l
Disable accountsudo usermod -e 1 USER (expire 1970-01-02) + lock / nologin
Delete with homesudo userdel -r USER
What can I sudo?sudo -l
Edit sudo safelysudo visudo
Run as app usersudo -u app -H command

Pitfalls

  • usermod -G without -a replaces the supplementary list (primary group stays). For add, always -aG.
  • id USER shows the database immediately; this shell still has the old groups until re-login (newgrp / sg / su - as a shortcut).
  • passwd -l / usermod -L lock the password, not SSH keys. To disable the account use expiry (usermod -e 1) and/or nologin.
  • nologin / false blocks an interactive login shell. sudo -u svc cmd and SSH command= / keys can still run.
  • useradd defaults differ by distro (Debian: no home and often sh unless you pass -m -s). Prefer adduser only on Debian/Ubuntu.
  • usermod -l does not rename the home directory or the user-private group.
  • groupdel refuses if the group is anyone’s primary group.
  • userdel -r is irreversible for home/mail — stop processes first (pkill -u).
  • Broken sudoers can lock out sudo users (root console still works). Only visudo / visudo -f. Files in sudoers.d with a dot in the name are ignored.
  • NOPASSWD: ALL is a major security smell on shared hosts.
  • Docker group ≈ root (docker.sock) — say so in interviews.

For more details, try man <command> in your terminal.

Previous12 Networking BasicsNext14 Package Management