👥
Users & groups cheat sheet
Account and ownership commands for Linux. Most need sudo. On Debian/Ubuntu the admin group is sudo; on RHEL-family systems it is wheel.
Click any command to copy it.
Who am I and who is here
| Command | What it does |
|---|---|
whoami | Show the current user. |
id | Show UID, GID and groups. |
groups user | List a user's groups. |
who | Who is logged in. |
w | Who is logged in and what they are doing. |
last | Recent logins. |
sudo -l | List what you are allowed to run with sudo. |
Create and modify users
| Command | What it does |
|---|---|
sudo adduser name | Create a user interactively (Debian/Ubuntu). |
sudo useradd -m -s /bin/bash name | Create a user with a home directory and shell. |
sudo passwd name | Set or change a password. |
sudo usermod -aG groupname name | Add a user to a group (the -a matters!). |
sudo usermod -L name | Lock an account. |
sudo usermod -U name | Unlock an account. |
sudo chage -l name | Show password ageing information. |
sudo userdel -r name | Destructive: Delete a user and their home directory. |
sudo chsh -s /bin/zsh name | Change a user's login shell. |
Groups
| Command | What it does |
|---|---|
sudo groupadd groupname | Create a group. |
sudo gpasswd -d name groupname | Remove a user from a group. |
getent group groupname | Show a group and its members. |
getent passwd name | Show a user's account entry. |
newgrp groupname | Switch your active group for this shell. |
Ownership
| Command | What it does |
|---|---|
sudo chown user:group file | Change owner and group. |
sudo chown -R user:group dir/ | Destructive: Change ownership recursively; a wrong path can break a system. |
sudo chgrp group file | Change only the group. |
chmod g+w file | Give the group write access. |
sudo visudo | Edit the sudoers file safely with syntax checking. |
su - name | Switch to another user with their environment. |
Frequently asked questions
Why must I use -aG with usermod?
-G alone replaces the user's supplementary groups with the list you give. -aG appends, which is almost always what you want.
How do I give a user sudo rights?
Add them to the admin group: sudo usermod -aG sudo name on Debian/Ubuntu, or -aG wheel on RHEL-family systems. They must log in again.