DevOps Notes

Home / DevOps Intro & Linux / Lesson 2

Users & Groups Management

Lesson 2 Stage 1 — Day 2

User & Group Configuration Files

Users and Groups Diagram

Linux stores all user and group data in four key files under /etc. Understanding these files is essential for managing access and security on any system.

User Files

/etc/passwd — Contains user account information. Each line represents one user with fields separated by colons:

username:x:UID:GID:comment:home_directory:shell
# Example:
gamal:x:1001:1001:Gamal Mohamed:/home/gamal:/bin/bash

The fields include: Name, password placeholder (x), UID (User ID), GID (Group ID), comment, Home directory, and Shell. This file is readable by all users.

/etc/shadow — Contains the actual hashed passwords. Only readable by root for security. Passwords are never stored in plain text.

Group Files

/etc/group — Contains group names and their members. Each line:

groupname:x:GID:member1,member2
# Example:
devops:x:1005:ali,gamal

/etc/gshadow — Contains group hashed passwords. Only readable by root.

User Management Commands

Checking User Info

whoami              # Show the currently logged-in username
id username         # Show UID, GID, and all groups for a user
su - username       # Switch to another user (opens their shell with their environment)

su - (with the dash) loads the target user's full environment — their home directory, PATH, etc. Without the dash, you switch user but keep your current environment.

Creating Users

There are two commands to create users:

sudo adduser username    # Interactive — prompts for password, full name, etc.
                         # Creates home directory automatically
                         # Recommended for manual user creation

sudo useradd username    # Non-interactive — just creates the user entry
                         # Does NOT create home dir by default
                         # Use -m flag to create home: sudo useradd -m username
                         # Better for scripting/automation

Modifying Users

sudo usermod username -aG sudo    # Add user to the sudo group (grant admin privileges)
                                  # -a = append (don't remove from other groups)
                                  # -G = supplementary group

Important: Always use -aG together. Using -G alone will replace all the user's groups with only the one specified — you'd lose their existing group memberships.

Setting Passwords

sudo passwd username     # Set or change a user's password
                         # Prompts for the new password twice

Deleting Users

sudo userdel username       # Delete the user account only
                            # Home directory and files are kept

sudo userdel -r username    # Delete user AND their home directory + mail spool
                            # Use when you want a complete cleanup

Group Management Commands

Creating & Inspecting Groups

sudo groupadd devops          # Create a new group called "devops"
grep devops /etc/group        # Check if the group exists and see its members

Managing Group Members — gpasswd

gpasswd is the main tool for managing group membership:

sudo gpasswd devops -a username              # Add a single user to the group
sudo gpasswd devops -M user1,user2,user3     # Set the full member list (replaces existing!)
sudo gpasswd devops -d username              # Remove a user from the group

Warning about -M: This flag replaces the entire member list. If the group had 5 members and you run -M user1,user2, the other 3 are removed. Use -a to safely add one user at a time.

Modifying & Deleting Groups

sudo groupmod -n newname oldname    # Rename a group
sudo groupdel devops                # Delete a group
                                    # The group must NOT be the primary group of any user

✓ Key Takeaways

  • /etc/passwd stores user info (readable by all), /etc/shadow stores hashed passwords (root only)
  • /etc/group stores groups & members, /etc/gshadow stores group passwords
  • adduser is interactive (recommended for manual use), useradd is non-interactive (better for scripts)
  • Always use -aG with usermod — never -G alone, or you'll wipe existing group memberships
  • userdel -r removes the user AND their home directory; without -r the files stay
  • gpasswd -a adds one user safely; gpasswd -M replaces the entire member list
  • su - username switches user with full environment; su username keeps your environment
  • Grant sudo access by adding a user to the sudo group: usermod -aG sudo username