Users & Groups

Every account is a user that belongs to one or more groups, which together decide access.

Linux is multi-user. Each person (or service) has a user account with a name and a numeric ID. Users are collected into groups so permissions can be granted to many people at once.

Inspecting accounts

  • whoami — the user you are right now.
  • id — your user ID, group ID and all your groups.
  • groups — just the groups you belong to.

Managing accounts

Creating and changing accounts needs admin rights:

  • sudo useradd -m bob — create user bob with a home folder.
  • sudo passwd bob — set bob's password.
  • sudo usermod -aG sudo bob — add bob to the sudo group.

Example

Example · bash
id
# uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo)

# Create a new user with a home directory
sudo useradd -m bob
sudo passwd bob

# Let bob use sudo
sudo usermod -aG sudo bob

When to use it

  • A sysadmin creates a 'deploy' service account with 'useradd -r -s /usr/sbin/nologin deploy' to run deployments without shell access.
  • A team lead adds a developer to the 'docker' group with 'usermod -aG docker alice' so she can run containers without sudo.
  • A security audit script reads '/etc/group' to list all users with sudo privileges before a compliance review.

More examples

Create and manage users

Creates a user account with a home directory and shell, then confirms the /etc/passwd entry.

Example · bash
# Create a new user with a home directory
useradd -m -s /bin/bash alice

# Set the user's password
passwd alice

# Verify the user was created
grep alice /etc/passwd
# alice:x:1001:1001::/home/alice:/bin/bash

Add user to a group

Creates a group and adds a user to it with -aG, then uses id to confirm all group memberships.

Example · bash
# Create a group
groupadd developers

# Add alice to the group (preserve existing groups with -a)
usermod -aG developers alice

# Verify group membership
id alice
# uid=1001(alice) gid=1001(alice) groups=1001(alice),1002(developers)

Inspect users and groups

Shows several read-only commands to inspect user and group information without editing system files.

Example · bash
# List all groups for current user
groups
# alice adm sudo docker

# List all system groups
getent group | grep -v nologin | head -10

# Show who is logged in
who

Discussion

  • Be the first to comment on this lesson.