guest@linuxthings: ~/guides/users.md

guest@linuxthings:~/guides$ man users

Users & Groups

Commands to identify users and manage accounts.

whoami                     # Current username
id                         # User ID and group IDs
id smith                   # Another user's IDs and groups
groups                     # Groups you belong to
who                        # Who is logged in
w                          # Who is logged in and what they are doing
last                       # Login history
last -n 10                 # The 10 most recent logins
sudo lastb                 # Failed login attempts

Becoming Another User

sudo command               # Run one command as root
sudo -u www-data command   # Run one command as another user
sudo -i                    # Root login shell (root's environment)
sudo -s                    # Root shell, keeping your environment
sudo -l                    # What am I allowed to run with sudo?
su - username              # Switch user (needs that user's password)
exit                       # Leave the root or switched shell

Prefer sudo over su: it uses your own password, can be limited to specific commands, and logs what each user ran. Edit sudo rules only with sudo visudo, which checks the syntax before saving so a typo cannot lock you out.

Account Management (requires sudo)

sudo useradd -m -s /bin/bash name  # Create user with home dir & shell
sudo useradd -m -G sudo,docker name  # ...and add extra groups at creation
sudo adduser name          # Interactive alternative on Debian/Ubuntu
sudo passwd name           # Set password
sudo usermod -aG group user # Add user to group
sudo usermod -s /bin/zsh user  # Change login shell
sudo usermod -L user       # Lock an account (disable password login)
sudo usermod -U user       # Unlock it
sudo userdel name          # Delete user, keep home dir
sudo userdel -r name       # Delete user and home dir
chsh -s /bin/zsh           # Change your own login shell
Always use -a with usermod -G. Without it, the user is removed from every group you didn't list.

Group changes apply at the next login. To use a new group straight away, run newgrp groupname to start a shell with that group.

Groups

sudo groupadd groupname    # Create a group
sudo groupmod -n new old   # Rename a group
sudo groupdel groupname    # Delete a group
sudo gpasswd -d user group # Remove a user from a group
getent group sudo          # Who is in the sudo group

Password Ageing (chage)

sudo chage -l name         # Show password and account expiry details
sudo chage -M 90 name      # Password must change every 90 days
sudo chage -E 2026-12-31 name  # Account expires on a date
sudo chage -d 0 name       # Force a password change at next login

Account Files

File

Contents

/etc/passwd

One line per account: name, UID, GID, comment, home, shell

/etc/shadow

Password hashes and ageing data (root-readable only)

/etc/group

Groups and their members

/etc/sudoers, /etc/sudoers.d/

Who may use sudo (edit with visudo)

/etc/skel/

Template files copied into new home directories

getent passwd smith        # Look up one account
cut -d: -f1 /etc/passwd    # List all account names

guest@linuxthings:~/guides$