Skip to content

Linux User and Group Command Reference

Mattscreative edited this page Feb 20, 2026 · 1 revision

Linux User and Group Command Reference

Welcome to the command reference! This page is your cheat sheet and deep dive into the actual commands you type in the terminal. Think of it as your "recipe book" for user and group management.

If the first page was the theory, this page is the practice. Bookmark this one!

We will cover every major command, break down its parts, and show you exactly what happens when you run it.


Table of Contents


How to Read This Guide

  • sudo : Almost every command here needs admin privileges. If a command fails, try sudo in front of it.
  • Brackets [ ] : These mean the option is optional.
  • Angle Brackets < > : These mean you need to replace this with your actual value.
    • Example: useradd <username> becomes useradd john

The Big Three Configuration Files

Before we start changing things, know that all these commands are just fancy editors for three text files. You can look at them to verify your work.

File Purpose Command to View
/etc/passwd List of all users (names, IDs, home folders) tail /etc/passwd or cat /etc/passwd
/etc/shadow Encrypted passwords (secret!) sudo tail /etc/shadow
/etc/group List of all groups and members tail /etc/group or cat /etc/group

User Management Commands

useradd - Create a new user

This creates a new user account. Without options, it creates a minimal user. We usually add flags to give them a home directory.

Basic Syntax:

sudo useradd [options] <username>

Common Options:

  • -m : Create a home directory (usually /home/username).
  • -c "Comment" : Add a full name or description.
  • -s /bin/bash : Set their default shell (program that runs when they open a terminal).
  • -G group1,group2 : Add user to extra groups immediately.

Examples:

Create a user named sarah with a home directory:

sudo useradd -m sarah
# Check her home folder was created
ls /home
# Output: sarah

Create a user named mark who is a developer, with a full name:

sudo useradd -m -c "Mark Smith" -G developers -s /bin/bash mark
# Now look at the end of the passwd file to see the result
tail -n 1 /etc/passwd
# Output: mark:x:1005:1005:Mark Smith:/home/mark:/bin/bash

usermod - Modify an existing user

This command changes the settings of a user that already exists.

Basic Syntax:

sudo usermod [options] <username>

Common Options:

  • -l <new_username> : Change the login name.
  • -d <new_home> : Change the home directory.
  • -m : Used with -d to move the contents of the old home to the new one.
  • -L : Lock the user account (prevents login).
  • -U : Unlock the user account.
  • -a -G <group> : The most common one! Adds user to a group. Always use -a with -G or you'll remove them from other groups.

Examples:

Lock Sarah's account (she lost her laptop):

sudo usermod -L sarah
# Now sarah cannot log in.

Change Mark's username to mark_smith:

sudo usermod -l mark_smith mark
# Check the change
id mark_smith
# Output: uid=1005(mark_smith)...

Add Sarah to the sudo group (so she can run admin commands):

sudo usermod -a -G sudo sarah
# Check her groups
groups sarah
# Output: sarah : sarah sudo

userdel - Delete a user

Removes a user from the system.

Basic Syntax:

sudo userdel [options] <username>

Common Options:

  • -r : Remove the user's home directory and mail spool.

Examples:

Delete Sarah, but keep her files (just in case):

sudo userdel sarah
# Sarah is gone, but /home/sarah still exists
ls /home
# Output: sarah  mark_smith

Delete Mark and everything he owned:

sudo userdel -r mark_smith
# Mark and his home folder are gone.
ls /home
# Output: sarah

passwd - Manage passwords

Sets or changes a user's password.

Basic Syntax:

# Change your own password
passwd

# Change another user's password (requires sudo)
sudo passwd <username>

Examples:

Give Sarah a password so she can log in (after unlocking her):

sudo passwd sarah
# System will ask: New password: 
# Retype new password:
# Output: passwd: password updated successfully

Lock and unlock with passwd (alternative to usermod):

sudo passwd -l sarah   # Lock
sudo passwd -u sarah   # Unlock

Group Management Commands

groupadd - Create a new group

Basic Syntax:

sudo groupadd <groupname>

Examples:

Create a group for the marketing team:

sudo groupadd marketing
# Check it exists
tail -n 1 /etc/group
# Output: marketing:x:1006:

groupmod - Modify a group

Usually just to rename a group.

Basic Syntax:

sudo groupmod [options] <old_groupname>

Common Options:

  • -n <new_name> : New name for the group.

Examples:

Rename marketing to digital_marketing:

sudo groupmod -n digital_marketing marketing
# Verify
tail -n 1 /etc/group
# Output: digital_marketing:x:1006:

groupdel - Delete a group

Basic Syntax:

sudo groupdel <groupname>

Important: You cannot delete a group if it is a user's primary group. You must delete the user first or change their primary group.

Examples:

Delete the digital_marketing group:

sudo groupdel digital_marketing
# Check it's gone
tail -n 1 /etc/group
# (Shows a different group, or nothing if it was the last one)

gpasswd - Manage group membership

This is the dedicated tool for adding and removing users from groups.

Basic Syntax:

# Add user to group
sudo gpasswd -a <username> <groupname>

# Remove user from group
sudo gpasswd -d <username> <groupname>

Examples:

Let's create a group and a user to test with:

sudo groupadd testers
sudo useradd -m diana

Add Diana to the testers group:

sudo gpasswd -a diana testers
# Output: Adding user diana to group testers
groups diana
# Output: diana : diana testers

Remove Diana from the testers group:

sudo gpasswd -d diana testers
# Output: Removing user diana from group testers
groups diana
# Output: diana : diana

Information and Utility Commands

id - Display user identity

Shows you the user ID (UID), group ID (GID), and all groups for a user.

Basic Syntax:

id [username]

Examples:

See your own identity:

id
# Output: uid=1000( yourname ) gid=1000( yourname ) groups=1000(yourname),4(adm),27(sudo)...

See Diana's identity:

id diana
# Output: uid=1006(diana) gid=1006(diana) groups=1006(diana)

groups - View group memberships

A simpler version of id that just lists the groups.

Basic Syntax:

groups [username]

Examples:

groups diana
# Output: diana : diana

who / w - Who is logged in

Shows you who is currently on the system.

Basic Syntax:

who
w

Examples:

who
# Output: diana    pts/0        2024-05-20 10:15 (192.168.1.100)
#         root     pts/1        2024-05-20 10:30 (192.168.1.101)

The w command shows more detail (what they are doing, system load).

last - Login history

Shows a log of all recent logins and reboots.

Basic Syntax:

last [username]

Examples:

See everyone who logged in:

last
# Output:
# diana    pts/0        192.168.1.100   Mon May 20 10:15   still logged in
# reboot   system boot  5.4.0-26-generic Mon May 20 10:00   still running
# ...

See just Diana's history:

last diana

Quick Command Matrix

Task Primary Command Example
Create user sudo useradd -m chris sudo useradd -m -G developers chris
Set password sudo passwd chris sudo passwd chris
Modify user sudo usermod -a -G docker chris sudo usermod -L chris (lock)
Delete user sudo userdel -r chris sudo userdel -r chris
Create group sudo groupadd devops sudo groupadd devops
Add to group sudo gpasswd -a chris devops sudo gpasswd -a chris devops
Remove from group sudo gpasswd -d chris devops sudo gpasswd -d chris devops
Delete group sudo groupdel devops sudo groupdel devops
Check user info id chris or groups chris id chris

Remember, the terminal is your friend. If you ever forget what a command does, use the built-in manual:

man useradd
man usermod
man groupadd

This will show you the official documentation for any command. Happy managing!

Clone this wiki locally