This content originally appeared on DEV Community and was authored by Oluchi Oraekwe
Introduction
A Bash script is a file containing a sequence of commands that are executed on a Bash shell. It allows performing a series of actions or automating tasks. This article will examine how to use a Bash script to create users dynamically by reading a CSV file. A CSV (Comma Separated Values) file contains data separated by commas or other delimiters.
In Linux systems, multiple users can access the same machine, necessitating the creation of various users with access to the machine. This article demonstrates a Bash script for creating users from a CSV file, assigning them to groups, and generating random passwords.
Objective
The main purpose of this article is to show how to create users in a Linux system using a simple bash script.
Bash Script Overview
We will follow a sequence of steps to create the Bash script. You can clone the full code from this GitHub repository.
Reading the CSV File
The first step is to read the CSV file. The Bash script will take the CSV file as an input argument. If no input or a file without the .csv
extension is provided, the script will throw an error and exit. Below is the code snippet for checking the file:
CSV_FILE="$1"
# Check if the file exists
if [[ ! -f "$CSV_FILE" ]]; then
echo "File not found!"
exit 1
fi
# Check if the file has a .csv extension
if [[ "$CSV_FILE" != *.csv ]]; then
echo "File must have a .csv extension"
exit 1
fi
Creating a Directory and File to Store Users
After reading the file, we need to create a directory and file to store the usernames and passwords of any new users created which will be accessible only to the file owner. To avoid creating a directory that already exists, we first check for its existence before creating a new one:
PASSWD_DIR="/var/secure"
PASSWD_FILE="user_passwords.csv"
if [ ! -d "$PASSWD_DIR" ]; then
mkdir -p "$PASSWD_DIR"
touch "$PASSWD_DIR/$PASSWD_FILE"
chmod 600 "$PASSWD_DIR/$PASSWD_FILE"
fi
Reading the Usernames and Group Names
After checking the CSV file and creating the file for storing the users, we will iterate through the rows to create users and assign them to groups. While creating the users, a random password is generated for each user. The code snippet below shows how users are created:
LOG_PATH="/var/secure/user_management.txt"
# Split the user and group by ";"
while IFS=';' read -r user groups; do
user=$(echo $user | tr -d ' ')
groups=$(echo $groups | tr -d ' ')
# Check if the user exists
if id "$user" &>/dev/null; then
echo "$(date "+%Y-%m-%d %H:%M:%S") user $user already exists." >> "$LOG_PATH"
else
# Generate random password
echo "$(date "+%Y-%m-%d %H:%M:%S") user with username $user already exist." >> $logPath
# Create user and assign password
useradd -m "$user"
echo "$user:$password" | chpasswd
# Store the created user and password
echo "$user,$password" >> "$PASSWD_DIR/$PASSWD_FILE"
echo "$(date "+%Y-%m-%d %H:%M:%S") user with username: $user cretaed by user $(whoami)" >> $logPath
fi
# Split the groups by comma and add user to each group
IFS=',' read -ra GROUP_ARRAY <<< "$groups"
for group in "${GROUP_ARRAY[@]}"; do
usermod -aG "$group" "$user"
echo "$(date "+%Y-%m-%d %H:%M:%S") user with username: $user added to group :$group by user $(whoami)" >> $logPath
done
done < "$CSV_FILE"
Important Notes
-
Separators: The CSV file uses
;
to separate usernames and groups, and,
to separate multiple groups. - Logging: All actions taken concerning user creation are logged to a file with timestamps.
Running the Script
To run the script, follow these steps:
- Ensure you are running on a Linux system with root privileges or use the
sudo
command. - Clone the repository and navigate to the directory.
-
Create a sample CSV file
users.csv
as shown below.
mary;developer,sys-admin paul;sys-admin peter;operations
-
Execute the script as shown add sudo if you are not a root user:
bash create_users.sh users.csv
After running the script, new users will be created, and their details will be stored in /var/secure/user_passwords.csv
. All actions will be logged in /var/secure/user_management.txt
.
Summary
The steps outlined in this article are not exhaustive other sources of information such as documentation can be used to complement your article.
To learn more about bash scripting join us at HNG Internship or subscribe and become a premium member by joining HNG Premium
This content originally appeared on DEV Community and was authored by Oluchi Oraekwe
Oluchi Oraekwe | Sciencx (2024-07-01T19:47:55+00:00) Creating Bash Scripts for User Management. Retrieved from https://www.scien.cx/2024/07/01/creating-bash-scripts-for-user-management/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.