Automating User and Group Provisioning
Create, modify, and audit accounts in bulk using useradd, chage, and sudoers fragment management.
Why Automate User Provisioning?
Managing users one at a time with useradd works fine for a handful of accounts, but enterprise environments routinely onboard dozens or hundreds of users simultaneously. Manual commands become error-prone, inconsistent, and unauditable.
Bash scripting lets you:
- Create users with standardised settings (shell, home directory, password policy) every time
- Read a CSV or text file of new hires and provision them in a single run
- Log every action so you have an audit trail for compliance
- Integrate with configuration management pipelines (Ansible, Chef, Jenkins)
This lesson walks through building a production-grade user provisioning script from scratch, covering useradd, chage, usermod, group management, sudoers drop-ins, and post-run auditing.
Reading a Bulk User List
The canonical input format for bulk provisioning is a delimited text file — one record per line. A typical CSV might look like:
username,full_name,group,shell
alice,Alice Smith,developers,/bin/bash
bob,Bob Jones,ops,/bin/zshUse IFS and read inside a while loop to parse it safely. Skipping the header line with tail -n +2 keeps the logic clean.
Key defensive practices:
- Strip leading/trailing whitespace from each field
- Skip blank lines and comment lines starting with
# - Validate that mandatory fields are non-empty before calling any system commands
#!/usr/bin/env bash
# parse_users.sh — safely read a CSV of users
set -euo pipefail
USER_FILE="${1:-users.csv}"
[[ -f "$USER_FILE" ]] || { echo "ERROR: $USER_FILE not found"; exit 1; }
tail -n +2 "$USER_FILE" | while IFS=',' read -r username full_name group shell; do
# trim whitespace
username="${username// /}"
[[ -z "$username" || "$username" == \#* ]] && continue
echo "Parsed -> user=$username group=$group shell=$shell"
doneAll lessons in this course
- Automating User and Group Provisioning
- Controlling systemd Services and Writing Unit Files
- Disk, Filesystem, and Mount Automation
- Building System Health Check and Alert Scripts