In many scenarios, system administrators need to create multiple user accounts on a Linux system. This step-by-step guide is suitable for administrators looking to manage user accounts in an efficient and streamlined manner. We will share efficient ways to create users in bulk using shell script with examples.
Manually adding each user can be time-consuming and prone to errors. A more efficient and accurate approach is to create users in bulk using a shell script. By automating this process, administrators can quickly add multiple user accounts, while also reducing the likelihood of mistakes. This article explains how to create a shell script that enables you to create users in batch in Linux. Additionally, it highlights how to assign custom home directories, shells, and random passwords to each new user account. The script also ensures that users change their passwords upon their first login.
We will use this list usernames.txt
through out the tutorial
RaviKumar
AnjaliGupta
AmitPatel
SnehaVerma
VivekSingh
If you are not familiar with the process of creating users, I would strongly recommend following these guides before you jump to the shell scripts:
How to create user in Linux?
15 useradd command examples in Linux [Cheat Sheet]
15+ adduser command examples in Linux [Cheat Sheet]
Create users in bulk using shell script [Simple]
In this script, we've added comments for important sections:
- Check if the script is run as root: This section checks if the script is being run with root privileges. If not, it exits the script with an error message.
- Read the input file: This section sets the name of the input file containing the usernames.
- Check if the input file exists: This section checks if the input file exists. If not, it exits the script with an error message.
- Create the users: This section reads the input file line by line and creates the users if they don't exist already.
#!/bin/bash
# This script creates users from a given file
# Check if the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Read the input file
user_file="usernames.txt"
# Check if the input file exists
if [ ! -f "$user_file" ]; then
echo "Usernames file not found: $user_file"
exit 1
fi
# Create the users
while IFS= read -r username; do
# Check if the user already exists
if id "$username" >/dev/null 2>&1; then
echo "User $username already exists"
else
# Create the user with a properly formatted comment
useradd -m -c "Generated user, $username" "$username"
echo "User $username created"
fi
done <"$user_file"
echo "User creation process completed"
Shell script to create users along with shell and home directory
We have updated our previous script to assign a custom shell and home directory to each of the user:
#!/bin/bash
# This script creates users from a given file
# Check if the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Read the input file
user_file="usernames.txt"
# Check if the input file exists
if [ ! -f "$user_file" ]; then
echo "Usernames file not found: $user_file"
exit 1
fi
# Specify the custom shell
custom_shell="/bin/bash"
# Create the home base directory if it doesn't exist
if [ ! -d "$home_base" ]; then
mkdir "$home_base"
fi
# Create the users
while IFS= read -r username; do
# Check if the user already exists
if id "$username" >/dev/null 2>&1; then
echo "User $username already exists"
else
# Set the custom home directory for the user
custom_home="${home_base}/${username}"
# Create the user with a custom shell, home directory, and comment
useradd -m -d "/home/$username" -s "$custom_shell" -c "Generated user, $username" "$username"
echo "User $username created with custom shell and home directory"
fi
done <"$user_file"
echo "User creation process completed"
Output:
# ./create_user.sh
User RaviKumar created with custom shell and home directory
User AnjaliGupta created with custom shell and home directory
User AmitPatel created with custom shell and home directory
User SnehaVerma created with custom shell and home directory
User VivekSingh created with custom shell and home directory
User creation process completed
Shell script to create users and assign password [Advance]
Additionally this script performs the following functions generates a random password which will be used for all users. The create user function will:
- Reads the input file line by line and performs the following steps for each username:
- Checks if the user already exists.
- Sets the custom home directory for the user.
- Creates the user with a custom shell, home directory, and comment.
- Assigns the generated random password to the created user.
- Enforces the user to change their password at the first login.
#!/bin/bash
# This script creates users from a given file
# Check if the script is run as root
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Read the input file
user_file="usernames.txt"
# Check if the input file exists
if [ ! -f "$user_file" ]; then
echo "Usernames file not found: $user_file"
exit 1
fi
# Specify the custom shell and home directory base path
custom_shell="/bin/bash"
home_base="/home/custom"
# Create the home base directory if it doesn't exist
if [ ! -d "$home_base" ]; then
mkdir "$home_base"
fi
# Generate a random password and assign it to the user
random_password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
echo "Assigning password as $random_password"
# Create the users
while IFS= read -r username; do
# Check if the user already exists
if id "$username" >/dev/null 2>&1; then
echo "User $username already exists"
else
# Set the custom home directory for the user
custom_home="${home_base}/${username}"
# Create the user with a custom shell, home directory, and comment
useradd -m -d "$custom_home" -s "$custom_shell" -c "Generated user, $username" "$username"
echo "User $username created with custom shell and home directory"
echo "$username:$random_password" | chpasswd
echo "Assigned random password to user $username: $random_password"
# Force the user to change the password at the first login
chage -d 0 "$username"
echo "User $username is required to change the password at the first login"
fi
done <"$user_file"
echo "User creation process completed"
Output:
# ./create_user.sh
Assigning password as Oc8WSbCjcziF
User RaviKumar created with custom shell and home directory
Assigned random password to user RaviKumar: Oc8WSbCjcziF
User RaviKumar is required to change the password at the first login
User AnjaliGupta created with custom shell and home directory
Assigned random password to user AnjaliGupta: Oc8WSbCjcziF
User AnjaliGupta is required to change the password at the first login
User AmitPatel created with custom shell and home directory
Assigned random password to user AmitPatel: Oc8WSbCjcziF
User AmitPatel is required to change the password at the first login
User SnehaVerma created with custom shell and home directory
Assigned random password to user SnehaVerma: Oc8WSbCjcziF
User SnehaVerma is required to change the password at the first login
User VivekSingh created with custom shell and home directory
Assigned random password to user VivekSingh: Oc8WSbCjcziF
User VivekSingh is required to change the password at the first login
User creation process completed
Try to login as one of the user, as expected the user is asked to change the password:
# su - RaviKumar
You are required to change your password immediately (administrator enforced).
Changing password for RaviKumar.
Current password:
New password:
Retype new password:
RaviKumar@deepak-VirtualBox:~$
Summary
This article provided a detailed guide on creating a shell script to create users in bulk using shell script in Linux systems. The script automates the process of creating multiple users, making it easier and more efficient for system administrators. The script reads a list of usernames from an input file and then checks if the user already exists before proceeding with the creation process. Custom shell and home directory options can be assigned to each user, ensuring a tailored user experience. Additionally, the script generates and assigns random passwords to each user, enhancing security by reducing the chance of password reuse.
The shell script also includes a feature that forces users to change their passwords upon their first login. This helps to maintain security by ensuring that users have unique, confidential passwords. In summary, the article offers an effective solution to create users in batch in Linux systems, making it easier for administrators to manage user accounts. By following the steps outlined in this guide, you can quickly create multiple users in Linux with custom shells, home directories, and initial password settings. The script's flexibility and automation make it a valuable tool for managing user accounts in any Linux environment.
You can refer to man page of useradd to get the list of possible arguments which can be used to further enhance our shell script.