Management
A common mistake. usermod -G grp without -a replaces all of a
user's supplementary groups — use usermod -aG, not -G. The template
for a new home directory is /etc/skel.
Linux accounts and groups; usermod -G without -a replaces all of a user's supplementary groups instead of adding one.
id alice
uid/gid/groups
sudo useradd -m -s /bin/bash bob
create
sudo usermod -aG sudo bob
add to a group (with -a, or it replaces all)
sudo passwd bob
set a password
sudo passwd -l bob
lock password login
sudo chage -M 90 bob
maximum password age — 90 days
getent group sudo
who's in the group
An account is a line in /etc/passwd, the password (hash) is in
/etc/shadow, groups are in /etc/group. A user has one primary
group and a list of supplementary ones.
The last field of an /etc/passwd line is the login shell: the program the
system starts when the user logs in. For people it's usually /bin/bash; for services it's
/usr/sbin/nologin, and nobody can log in as them. usermod -s changes it (see usermod).
A common mistake. usermod -G grp without -a replaces all of a
user's supplementary groups — use usermod -aG, not -G. The template
for a new home directory is /etc/skel.
System users (daemons) usually have uid < 1000, nologin, no password
(useradd -r).
Group membership applies on the next login. Log back in (or use
newgrp <group> for the current shell). Check with id.
-G without -a)Restore the needed ones: usermod -aG sudo,docker alice. You can verify
against /etc/group (getent group).
End its processes (pkill -u alice, see processes) or use userdel
-f.
The account/password is locked (passwd -l/chage -E in the past) or
the shell is nologin. Check chage -l, getent passwd (the last field
is the shell).
An SGID directory only assigns its group to NEW files; recursively
chgrp -R the old ones (see permissions, chmod-chown).
userdel -r deletes the home directory and mail spool irreversibly —
without a backup, the data cannot be recovered. Before deleting, check
whether any processes or cron jobs still belong to the user (pkill -u,
crontab -l -u): userdel without -f refuses to delete an account with
active processes, while -f deletes it anyway, even if processes keep
running under the now-nonexistent UID. After deletion, the freed UID can
be reassigned to a new user — files left on disk with that UID become
"theirs".
-G without -a trap.Found an inaccuracy? Report an error in the article