User Management Intermediate

Automated User Creation

Script that automatically creates multiple user accounts by reading from a user list file. Creates home directory and assigns password for each user.

Published: January 20, 2024 Updated: November 15, 2024

Detailed Information

This script automatically creates multiple Linux user accounts by reading from a text file. It is an essential tool for system administrators and is particularly useful in new server setups, corporate systems, or educational environments.

What Does This Script Do?

Instead of manually creating each user one by one, this script allows you to create user accounts in bulk from a file. For each user, it:

  • Creates user account
  • Creates home directory (/home/username)
  • Sets password
  • Forces password change on first login
  • Assigns bash as default shell

Why Should You Use It?

Manual user creation is time-consuming and error-prone. With this script:

  • You can create dozens or hundreds of users in minutes
  • You minimize error risk
  • You ensure consistent user configuration
  • You can repeat the process (idempotent)

Security Features

The script follows security best practices:

  • Forces password change on first login
  • Skips existing users (prevents duplicate creation)
  • Root privilege check
  • Error handling and reporting

How to Use

Step-by-Step Usage Guide

1. Create User List File

First, create a file named users.txt and add user information:

nano users.txt

File format: username:password:fullname

john:SecurePass123:John Doe
jane:SecurePass456:Jane Smith
bob:SecurePass789:Bob Johnson
# Comment line - this line is skipped

2. Restrict File Permissions

For security, set file permissions so only root can read it:

chmod 600 users.txt

3. Create Script File

Save the script code to a file:

nano create_users.sh

Paste the script code and save.

4. Make Script Executable

chmod +x create_users.sh

5. Run Script as Root

sudo ./create_users.sh

File Format Details

Each line consists of three parts, separated by colons (:):

  • Username: The username to be used in the system (lowercase, numbers, underscore)
  • Password: User's initial password (use strong passwords)
  • Full name: User's real name (can contain spaces)

Advanced Usage

You can customize the script to assign different shells, different home directories, or additional groups. Edit the useradd command in the script to add these features.

Requirements

Requirements

  • Root or sudo privileges: Root privileges are required for user creation
  • Linux/Unix System: Script works on Linux and Unix-based systems
  • useradd command: Required for user creation
  • chpasswd command: Required for password assignment
  • chage command: Required for password policy settings
  • users.txt file: File containing user information

Installation Check

Check if required commands are installed:

which useradd chpasswd chage

These commands are usually installed by default on Linux systems. If missing:

# Ubuntu/Debian
sudo apt-get install passwd

# CentOS/RHEL
sudo yum install passwd

File Format Requirements

  • Each line represents one user
  • Fields are separated by colons (:)
  • Empty lines are skipped
  • Lines starting with # are treated as comments
  • Usernames can contain lowercase letters, numbers, and underscores

Use Cases

Use Cases

1. New Server Setup

When setting up a new server, you can quickly create accounts for all employees or users. Particularly useful in corporate servers.

2. Educational Environments

If you need to create dozens of student accounts for a class or course, this script is perfect for you. You can create new student accounts at the beginning of each semester.

3>Bulk User Management

You can use it to update existing users or add new users. Since the script skips existing users, you can run it safely.

4. Disaster Recovery

When a server crashes and you set up a new server, you can quickly recreate all user accounts.

5. Test Environments

If you need to create many test users in test environments, you can quickly create them with this script.

6. Temporary Projects

If you need to create user accounts for temporary projects, you can easily delete them when the project ends and recreate them when needed.

Examples

Usage Examples

Example 1: Basic Usage

Create a simple user list and run the script:

# users.txt file
john:Passw0rd123:John Doe
jane:Passw0rd456:Jane Smith
bob:Passw0rd789:Bob Johnson

# Run script
sudo ./create_users.sh

Example 2: With Comment Lines

Use comment lines in the file for organization:

# Management Team
john:Passw0rd123:John Doe - CEO
jane:Passw0rd456:Jane Smith - CTO

# Developer Team
bob:Passw0rd789:Bob Johnson - Developer
alice:Passw0rd012:Alice Brown - Developer

Example 3: Converting from CSV

Create user list from Excel or CSV file:

# CSV format: Name,Email,Department
# Convert with bash:
cat users.csv | awk -F, '{print tolower($1)":Passw0rd123:"$1}' > users.txt

Example 4: Advanced with Group Assignment

Edit the script to assign groups to users:

useradd -m -c "$fullname" -s /bin/bash -G developers "$username"

Code

#!/bin/bash

# Automated User Creation Script

if [ "$EUID" -ne 0 ]; then 
    echo "Error: This script must be run as root or with sudo"
    echo "Usage: sudo $0"
    exit 1
fi

USER_FILE="users.txt"

if [ ! -f "$USER_FILE" ]; then
    echo "Error: User file $USER_FILE not found!"
    echo ""
    echo "Please create a file named \"users.txt\" with the following format:"
    echo "username:password:Full Name"
    echo ""
    echo "Example:"
    echo "john:SecurePass123:John Doe"
    echo "jane:SecurePass456:Jane Smith"
    exit 1
fi

echo "======================================"
echo "   AUTOMATED USER CREATION"
echo "======================================"
echo "Reading users from: $USER_FILE"
echo ""

while IFS=: read -r username password fullname; do
    [[ -z "$username" || "$username" =~ ^#.*$ ]] && continue
    
    if id "$username" &>/dev/null; then
        echo "⚠️  User \"$username\" already exists. Skipping..."
        continue
    fi
    
    useradd -m -c "$fullname" -s /bin/bash "$username"
    
    if [ $? -eq 0 ]; then
        echo "✓ User \"$username\" created successfully"
        echo "$username:$password" | chpasswd
        
        if [ $? -eq 0 ]; then
            echo "  ✓ Password set for \"$username\""
        else
            echo "  ✗ Failed to set password for \"$username\""
        fi
        
        chage -d 0 "$username"
        echo "  ✓ Password change required on first login"
        echo ""
    else
        echo "✗ Failed to create user \"$username\""
        echo ""
    fi
done < "$USER_FILE"

echo "======================================"
echo "User creation process completed!"
echo ""
echo "Summary - Recent users:"
echo "----------------------"
getent passwd | grep -E "/home/" | tail -n 10

Usage

# Create user list (users.txt)
# Format: username:password:fullname
echo "john:Passw0rd123:John Doe" > users.txt
echo "jane:Passw0rd456:Jane Smith" >> users.txt

sudo chmod +x create_users.sh
sudo ./create_users.sh

Troubleshooting

Troubleshooting

Problem: "Permission denied" Error

Solution: Make sure you run the script as root or with sudo:

sudo ./create_users.sh

Problem: "useradd: command not found"

Solution: The useradd command is usually installed by default. If missing:

# Ubuntu/Debian
sudo apt-get install passwd

# CentOS/RHEL
sudo yum install passwd

Problem: User Created But Password Not Working

Solution: Check password format. Special characters may cause issues. Do not use quotes in passwords.

Problem: "users.txt: No such file or directory"

Solution: Make sure users.txt file is in the same directory as the script. Or update the USER_FILE variable in the script with the full path.

Problem: Invalid Username

Solution: Usernames can only contain lowercase letters, numbers, and underscores. Do not use uppercase letters, spaces, or special characters.

Tags

user creation bulk operation user management useradd