System Administration · #10 of 19

User Management

Users, Groups & sudo

Users in Linux

Every process runs as a user. Every file is owned by a user.

Key Concepts


User Information

Who Am I?

whoami                      # Current username
id                          # UID, GID, and groups
id alice                    # Info about another user

User Database

cat /etc/passwd             # All user accounts
# format: username:x:UID:GID:comment:home:shell

getent passwd alice         # Specific user info

Example line:

alice:x:1000:1000:Alice Smith:/home/alice:/bin/bash
└──┬─┘ │ └─┬─┘└─┬─┘└────┬────┘└────┬────┘└───┬───┘
 name  │  UID  GID    comment    home      shell
       └── password in /etc/shadow

Groups

Groups allow shared permissions across multiple users.

groups                      # Your groups
groups alice                # Another user's groups
cat /etc/group              # All groups

# Add user to group (requires sudo)
sudo usermod -aG docker alice    # Add alice to docker group

Important groups:


Managing Users

Creating Users

sudo useradd -m bob         # Create user with home directory
sudo useradd -m -s /bin/bash bob  # Also set shell to bash
sudo passwd bob             # Set password

Modifying Users

sudo usermod -aG sudo bob   # Add bob to sudo group
sudo usermod -l newname oldname   # Rename user
sudo usermod -d /home/newdir bob  # Change home directory

Deleting Users

sudo userdel bob            # Delete user (keep home)
sudo userdel -r bob         # Delete user AND home directory

The sudo Command

sudo = “superuser do” — Run one command as root.

sudo apt update             # Run apt update as root
sudo -i                     # Start a root shell
sudo -u alice command       # Run as user alice

sudoers File

Who can use sudo is defined in /etc/sudoers:

sudo visudo                 # Safe way to edit sudoers

Never edit /etc/sudoers directly!


Switching Users

su — Switch User

su alice                    # Switch to alice (need her password)
su -                        # Switch to root (need root password)
su - alice                  # Switch to alice with fresh environment

sudo su

sudo su                     # Become root (using YOUR password)
sudo su - alice             # Become alice (no password needed)

Password Management

passwd                      # Change your password
sudo passwd bob             # Change bob's password (as root)
sudo passwd -l bob          # Lock account
sudo passwd -u bob          # Unlock account

Password hashes are stored in /etc/shadow (readable only by root).


Try It!

Practice user commands in the terminal:

Exercises:

  1. Check your user info: whoami and id
  2. List all groups you belong to: groups
  3. View the passwd file: cat /etc/passwd
  4. Switch to root: su root (password in demo)
  5. Check you’re root: whoami
  6. Exit root: exit

Try it in the terminal

A sandboxed shell with this lesson’s commands. Type help to see what’s available.

bash — try it