Last active
April 1, 2023 17:04
-
-
Save bluPhy/8c81c4ebddb258c4be5fe3306c932527 to your computer and use it in GitHub Desktop.
Adding user with password Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Purpose - Script to add a user to Linux system including passsword | |
# Original Author - Vivek Gite <www.cyberciti.biz> under GPL v2.0+ | |
# Updated by - Alejandro Leal @bluPhy | |
# ------------------------------------------------------------------ | |
# Am i Root user? | |
if [ $(id -u) -eq 0 ]; then | |
read -p "Enter username : " username | |
while true; do | |
read -p "Provide the password for default username (kali):" -s -r password | |
if [[ $password =~ ^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{8,}$ ]]; then | |
# Password meets all conditions | |
break | |
else | |
echo "Error: Password must be at least 8 characters long and contain at least one digit, one uppercase letter, and one lowercase letter." | |
fi | |
done | |
grep -E "^$username" /etc/passwd >/dev/null | |
if [ $? -eq 0 ]; then | |
echo "$username exists!" | |
exit 1 | |
else | |
password_salt=$(openssl rand -base64 12) | |
password_hash=$(openssl passwd -6 -salt "$password_salt" "$password") | |
useradd --create-home "$username" --home "/home/$username" --password "$password_hash" --shell "/bin/bash" | |
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" | |
fi | |
else | |
echo "Only root may add a user to the system." | |
exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment