π
February 21, 2024
“I have a LOT of users to add, but I do not want to add them one by one. Can I use a script?”
Yes! This is what Linux specializes in. We can write a script that reads a list of usernames from a text file to create these users on a given system and assign them all a default password.
Here is one way to achieve this magic.
Username List
This is just a text file with one username per line. This could be a list of hundreds of users, but here is a short example for this demonstration.
usernames.txt
# This is a comment mislay dieseldan pugnaciouspete vitriolicvinny fracasfred gyrategypsy gamer234 moviedwnldrXtreme? little bobby smiles thisnameistoolongtobeausableusername tinytim spaceman Jo Joy SailorMoon
We will write our script to be somewhat resistant to erroneous usernames as shown above. One name is too long and another is too short. Another contains leading spaces, and we see a comment at the top. This will all be taken care of by our script, but you should fomat the usernames as best as possible.
The Script
Behold this beauty of cobbled commands:
addusers.sh
#!/bin/bash # Add new users to system based upon # usernames listed in a text file (usernames.txt). readonly PASSWORD=changemeNOW while read line do if [[ "$line" =~ ^[[:alnum:]]{3,32}$ ]] then username="${line,,}" # Convert to lowercase if [ $(grep -c "^$username:" /etc/passwd) -eq 0 ] then echo "Adding user: $username" sudo useradd -m "$username" echo "${username}:${PASSWORD}" | sudo chpasswd fi fi done < usernames.txt
How it Works
readonly PASSWORD=changemeNOW
All user accounts are assigned the same default password. It is assumed that the new users will be instructed to change their passwords.
if [[ "$line" =~ ^[[:alnum:]]{3,32}$ ]]
This is really the core of script, so alter this to meet your username formatting needs. The script iterates through each line in the file from top to bottom, but this condition filters the valid lines. Each username must be at least 3 characters long and be a maximum of 32 characters.
Only alphanumeric characters (upper or lower case) are allowed in usernames, so some of the names in the text file will be excluded. For example, the comment line contains the # and space characters, so it is ignored. Exploiting this “feature” allows us to add comments to usernames.txt.
The =~ operator compares an extended regular expression on the right with a string (stored in the $line variable) on the left.
{3,32}
This is the regex syntax that specifies the username length requirements. 3 is the shortest username allowed up to 32. Any username longer or shorter is discarded. Adjust these values to specify your own username length requirements.
username="${line,,}"
Convert the username to lowercase. Some names contain uppercase letters, and we need to convert them to all lowercase characters. Numbers are preserved.
if [ $(grep -c "^$username:" /etc/passwd) -eq 0 ]
Before we try to add a new user with the useradd command, first, check if the user already exists. This is accomplished by checking for the prospective username in /etc/passwd. By using command substitution, we can return a count. If the count is 0, then the new username does not exist. Let’s add the new user.
sudo useradd -m "$username"
Create a new user with the current username stored in the $username variable. Note: This was tested in Linux Mint. When a new user is added to the system with the -m option, it will not create the set of default subdirectories (Documents, Templates, Downloads, and the others) for each new user. These directories are created when the user logs into the system for himself via the GUI, not the command line.
These default user home directories may or may not be want you want. If you need to use a preset list of directories for each new user, then consider customizing /etc/skel. Just know that the absence of these default directories does not mean the script is not working. The user needs to log in at least one time via the GUI to make these subdirectories appear.
echo "${username}:${PASSWORD}" | sudo chpasswd
By default, a user is added to the system, but he cannot log in because there is no password set. For this particular policy, we want users to log into their accounts individually and have them to change their passwords to avoid extra work on our part. The solution used here is to set every new account to the same default password. This works because our users most certainly trust each other…maybe, and they would never try to tamper with each other’s account…we hope. chpasswd is a great program for bulk password changes. It is possible to assign each user his own password to be changed later. It is up to your needs. For now, all accounts have the same default password stored in the $PASSWORD constant. The point is to show how a script can be used to add many, many users to a Linux system from nothing more than simple text file of usernames.
Enjoy!