DevOps Journey
Linux

Users and Groups

User and group management in Linux

Users and Groups

Users and groups are fundamental to Linux security and access control.

User Types

Root User:

  • UID 0
  • Has unrestricted access
  • Use sudo for administrative tasks

System Users:

  • UID < 1000 (typically)
  • Created for services (www-data, mysql, etc.)
  • Usually no login shell

Regular Users:

  • UID >= 1000
  • Standard user accounts
  • Have limited privileges

User Management

Creating Users

# Create a new user
useradd username

# Create with specific options
useradd -m -d /home/username -s /bin/bash -c "Full Name" username
# -m: create home directory
# -d: specify home directory
# -s: set login shell
# -c: set comment/full name

# Create with sudo privileges
useradd -G sudo username

# Create system user
useradd -r -s /bin/false serviceuser

Modifying Users

# Change password
passwd username

# Modify user properties
usermod -d /new/home/path username # change home directory
usermod -s /bin/zsh username # change shell
usermod -aG groupname username # add to group
usermod -l newname oldname # rename user

# Lock/unlock user account
usermod -L username # lock
usermod -U username # unlock

Deleting Users

# Delete user (keep home directory)
userdel username

# Delete user and home directory
userdel -r username

Group Management

Creating Groups

# Create a new group
groupadd groupname

# Create system group
groupadd -r servicegroupname

Managing Group Membership

# Add user to group
usermod -aG groupname username

# Add multiple users to group
for user in user1 user2 user3; do usermod -aG groupname $user; done

# Remove user from group
gpasswd -d username groupname

# Remove from all groups except primary
changing groups is tricky - use usermod

Modifying Groups

# Change group name
groupmod -n newname oldname

# Change group ID
groupmod -g 1001 groupname

Deleting Groups

# Delete group
groupdel groupname

Viewing User and Group Information

# List all users
cat /etc/passwd

# List all groups
cat /etc/group

# View current user
whoami

# View user ID and group membership
id username

# List all groups for a user
groups username

# View user details
finger username

Sudo Access

# Grant sudo access
usermod -aG sudo username

# Check sudo privileges
sudo -l

# Edit sudoers file safely
sudo visudo

Best Practices

  • Don't use root for daily tasks - use sudo
  • Create separate users for different services
  • Use strong passwords
  • Regularly audit user accounts and permissions
  • Remove inactive user accounts
  • Use groups to manage permissions efficiently
  • Limit sudo access to necessary users only

On this page