LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

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]
copy
$ adduser [name]
copy
$ passwd [name]
copy
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]
copy
$ userdel -r [name]
copy
$ deluser [name]
copy
$ usermod -l [newUsername] [oldUsername]
copy

Modifying Users

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

Groups

Create, rename, and delete groups.
$ groupadd [name]
copy
$ groupmod -n [newGroupname] [oldGroupname]
copy
$ groupdel [name]
copy
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]
copy
$ gpasswd -a [userName] [groupName]
copy
$ adduser [userName] [groupName]
copy
Remove a user from a group.
$ gpasswd -d [userName] [groupName]
copy
$ deluser [userName] [groupName]
copy
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]
copy
$ usermod -aG wheel [user]
copy
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
copy
$ getent group
copy
$ cat /etc/passwd
copy
$ cat /etc/group
copy

Who Am I, Who Is Here

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

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]
copy
$ sudo -i
copy
$ sudo -u [user] [command]
copy
Copied to clipboard
Kai