Users & Groups

Creating Users

useradd is the low-level tool: add -m to create a home directory and -s to set the login shell. On Debian-based systems, adduser is the friendlier interactive front end. All user and group administration requires root.
$ useradd -m -s /bin/bash [name]
$ adduser [name]
$ passwd [name]
Plain useradd [name] creates the account without a home directory, which is rarely what you want for a real user.

Deleting & Renaming Users

-r also removes the user's home directory and mail spool.
$ userdel [name]
$ userdel -r [name]
$ deluser [name]
$ usermod -l [newUsername] [oldUsername]

Modifying Users

Change a user's shell, home directory, or comment field.
$ usermod -s /bin/zsh [name]
$ chsh -s /bin/zsh [name]
$ usermod -d /new/home -m [name]
Lock an account (disables password login) and unlock it again.
$ usermod -L [name]
$ usermod -U [name]
Force a password change at next login, or inspect password aging.
$ passwd -e [name]
$ chage -l [name]

Groups

Create, rename, and delete groups.
$ groupadd [name]
$ groupmod -n [newGroupname] [oldGroupname]
$ groupdel [name]
Add an existing user to a group. The -a in -aG is essential: without it, the user is removed from all other supplementary groups.
$ usermod -aG [groupName] [userName]
$ gpasswd -a [userName] [groupName]
$ adduser [userName] [groupName]
Remove a user from a group.
$ gpasswd -d [userName] [groupName]
$ deluser [userName] [groupName]
Group changes take effect at the next login. Use newgrp [groupName] to activate one in the current shell without logging out.

Granting sudo

Membership in the admin group grants sudo rights: the group is sudo on Debian/Ubuntu and wheel on Fedora, RHEL, and Arch.
$ usermod -aG sudo [user]
$ usermod -aG wheel [user]
Edit the sudoers file only with visudo, which checks the syntax before saving (a broken sudoers file can lock you out).

Listing Users & Groups

getent queries all account databases, including LDAP and other network sources; the /etc files only show local accounts.
$ getent passwd
$ getent group
$ cat /etc/passwd
$ cat /etc/group

Who Am I, Who Is Here

Show the current user, their IDs and groups, and the groups of any user.
$ id [user]
$ groups [user]
See who is logged in and what they are doing, and the login history.
$ who
$ w

Switching Users

su - starts a full login shell as another user (root if no name is given); sudo -i opens a root shell via sudo.
$ su - [user]
$ sudo -i
$ sudo -u [user] [command]