Skip to main content

Chapter 5.1 - User Management

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand the Linux user system
  • Create and delete users
  • Manage groups
  • Understand configuration files (/etc/passwd, /etc/group)

Introduction​

Linux is a multi-user system. Each user has their own account with specific permissions and resources.


Types of Users​

Root (Superuser)​

Characteristics:

  • UID: 0 (always)
  • Privileges: Full system access
  • Danger: Can modify/delete everything
  • Usage: System administration only

Normal Users​

Characteristics:

  • UID: Generally ≥ 1000
  • Privileges: Limited to their files
  • Home: /home/username
  • Usage: Regular users

System Users​

Characteristics:

  • UID: 1-999 (depending on distribution)
  • Privileges: Limited, for services
  • Home: Often /var/lib/service or /
  • Usage: System services (www-data, mysql, etc.)

Configuration Files​

/etc/passwd​

Contains user information:

# Format: username:password:UID:GID:comment:home:shell
root:x:0:0:root:/root:/bin/bash
user1:x:1000:1000:User One:/home/user1:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

Fields:

  1. Username: login name
  2. Password: x (in /etc/shadow)
  3. UID: User ID
  4. GID: Primary Group ID
  5. Comment: Full name, description
  6. Home: Home directory
  7. Shell: Default shell

/etc/shadow​

Contains encrypted passwords:

# Format: username:password:last_change:min:max:warn:expire:disable
user1:$6$salt$hash:19000:0:99999:7:::

Security:

  • Readable only by root
  • Encrypted passwords
  • Password policies

/etc/group​

Contains group information:

# Format: groupname:password:GID:members
root:x:0:
users:x:100:user1,user2
developers:x:1001:user1,user3

Create a User​

useradd​

# Simple user
sudo useradd username

# With options
sudo useradd -m -s /bin/bash -c "Full Name" username
# -m: Create home directory
# -s: Shell
# -c: Comment/description

# With primary group
sudo useradd -g developers username

# With additional groups
sudo useradd -G sudo,developers username

# Specific UID
sudo useradd -u 2000 username

adduser (Ubuntu/Debian)​

More user-friendly interface:

sudo adduser username
# Asks interactive questions

Modify a User​

usermod​

# Change shell
sudo usermod -s /bin/zsh username

# Change home
sudo usermod -d /new/home username
sudo usermod -m -d /new/home username # Move files

# Add to a group
sudo usermod -aG sudo username

# Change UID
sudo usermod -u 2001 username

# Lock account
sudo usermod -L username

# Unlock
sudo usermod -U username

Delete a User​

userdel​

# Delete user
sudo userdel username

# Delete with home directory
sudo userdel -r username

# Force (if active processes)
sudo userdel -f -r username

Group Management​

Create a Group​

# Create group
sudo groupadd developers

# With specific GID
sudo groupadd -g 2000 developers

Modify a Group​

# Change GID
sudo groupmod -g 2001 developers

# Rename
sudo groupmod -n newname oldname

Delete a Group​

sudo groupdel developers

Add/Remove from a Group​

# Add user to group
sudo usermod -aG developers username

# See user's groups
groups username
id username

Useful Commands​

# Who am I?
whoami
id

# Change user
su username
su - username # With full environment

# Temporarily change
sudo -u username command

# See connected users
who
w
users

# See login history
last
lastlog

Summary​

In this chapter, you learned:

Types: Root (UID 0), Users (UID ≥1000), System (1-999)
Files: /etc/passwd, /etc/shadow, /etc/group
Creation: useradd, adduser
Modification: usermod
Deletion: userdel
Groups: groupadd, groupmod, groupdel
Commands: whoami, id, su, sudo


Next Steps​

Chapter 5.2: Group Management
Chapter 5.3: Advanced Permissions


Chapter created on: December 2024