0% found this document useful (0 votes)
4 views2 pages

Creer Utilisateurs

This bash script creates 35 users and 7 groups, and must be run as root. It checks for existing groups and users, creates them if they do not exist, and logs the actions taken. A default password is set for all users, and a super administrator is also created.

Uploaded by

rijag22480
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Creer Utilisateurs

This bash script creates 35 users and 7 groups, and must be run as root. It checks for existing groups and users, creates them if they do not exist, and logs the actions taken. A default password is set for all users, and a super administrator is also created.

Uploaded by

rijag22480
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/bin/bash

# SCRIPT: creer_utilisateurs.sh - Create 35 users and 7 groups


# Must be run as root
if [[ $EUID -ne 0 ]]; then
echo "Error: This script must be run as root." >&2
exit 1
fi

# Declare associative array for groups and their users


declare -A groupes_utilisateurs=(
["profs"]="alice bob claire david emilie"
["eleves"]="franck gina Henri isabelle jacques"
["admins"]="karim laura marc nora olivier"
["support"]="paul quentin rita sam theo"
["visiteurs"]="ursula victor wendy xavier yasmine"
["compta"]="zach lea mike nina omar"
["direction"]="pierre clara lucie julien sophie"
)

# Default password and log file


default_password="Password123!" # Consider generating random passwords
log_file="/var/log/utilisateurs_groupes.txt"

# Ensure log file is writable


touch "$log_file" 2>/dev/null || {
echo "Error: Cannot write to $log_file. Check permissions." >&2
exit 1
}

# Initialize log file with header


echo "=== User and Group Creation Log - $(date) ===" > "$log_file"

# Create groups and users


for groupe in "${!groupes_utilisateurs[@]}"; do
# Create group if it doesn't exist
if ! getent group "$groupe" >/dev/null 2>&1; then
groupadd "$groupe"
echo "Created group: $groupe" >> "$log_file"
else
echo "Group $groupe already exists" >> "$log_file"
fi

echo -e "\nGroupe: $groupe" >> "$log_file"

# Split user list into an array


read -ra utilisateurs <<< "${groupes_utilisateurs[$groupe]}"

for utilisateur in "${utilisateurs[@]}"; do


if ! id "$utilisateur" >/dev/null 2>&1; then
useradd -m -s /bin/bash -G "$groupe" "$utilisateur"
echo "$utilisateur:$default_password" | chpasswd
passwd -e "$utilisateur" # Force password change on first login
echo " - Created user: $utilisateur" >> "$log_file"
else
echo " - User $utilisateur already exists" >> "$log_file"
fi
done
done
# Create super administrator
super_admin="superadmin"
if ! id "$super_admin" >/dev/null 2>&1; then
useradd -m -s /bin/bash -G sudo "$super_admin"
echo "$super_admin:$default_password" | chpasswd
passwd -e "$super_admin"
echo "Created super administrator: $super_admin" >> "$log_file"
else
echo "Super administrator $super_admin already exists" >> "$log_file"
fi

echo "User and group creation completed. Log written to $log_file."

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy