You rarely need to edit account files by hand. A small set of commands does it safely. All of these changes need root, so they use sudo. Practise on a test machine.
Creating a user
$ sudo useradd -m -s /bin/bash sam
$ sudo passwd sam
New password:
Retype new password:
passwd: password updated successfully-m creates the home directory, filled from /etc/skel, and -s sets the login shell. Defaults for useradd differ between distributions, so stating these options is clearer.
Debian and Ubuntu also provide adduser, a friendlier script that prompts for details.
Groups and membership
$ sudo groupadd devs
$ sudo usermod -aG devs sam
$ id sam
uid=1001(sam) gid=1001(sam) groups=1001(sam),1002(devs)-G sets the supplementary groups and -a means append. Without -a, usermod -G devs sam replaces the whole list, which can remove someone's sudo access.
To remove one membership:
$ sudo gpasswd -d sam devs
Removing user sam from group devsLocking and changing
$ sudo usermod -L sam
$ sudo usermod -U sam
$ sudo usermod -s /usr/sbin/nologin sam-L locks the password and -U unlocks it. Locking the password may not stop SSH key logins. To disable the whole account, also expire it with sudo usermod -e 1 sam.
Removing
$ sudo userdel -r sam
$ sudo groupdel devs-r also removes the home directory and mail spool. Files that the user owned elsewhere remain, showing only a numeric UID. Find them before deleting:
$ sudo find / -xdev -user sam-xdev stays on one filesystem, so search others separately. Back up anything you may need. groupdel refuses to remove a group that is still some user's primary group.
Try it
On a test system, create a user, add it to a new group, check with id, lock it, then remove it and the group. After each step, run getent passwd or getent group to see the change. The accounts and access files lab gives you a place to practise.
Check your understanding
User Management Tools
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.