-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadd_local_users.sh
62 lines (51 loc) · 1.19 KB
/
add_local_users.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# Check if I am root
if [[ "${UID}" -ne 0 ]]
then
echo 'You are not root'
exit 1
fi
# Check inputs
NUMBERS_OF_USERS="${#}"
if [[ "${NUMBERS_OF_USERS}" -lt 1 ]]
then
echo 'Please input username(s)'
exit 2
fi
# Create new local users
for USERNAME in "${@}"
do
useradd -c "${USERNAME}" -m ${USERNAME}
## Check to see if the useradd command succeeded
if [[ "${?}" -ne 0 ]]
then
echo "The useradd command did not work successfully."
exit 3
fi
done
# Set up passwords
S="!@#$%^&*()_-+="
for USERNAME in "${@}"
do
## Generate passwords for each user
SPECIAL_CHAR=$(echo "${S}" | fold -b1 | shuf | head -n1)
PASSWORD_TEMP=$(date +%s%N${RANDOM}${RANDOM} | sha256sum | head -c12)
PASSWORD=${PASSWORD_TEMP}${SPECIAL_CHAR}
## Set the password for the user
echo ${PASSWORD} | passwd --stdin ${USERNAME}
## Check to see if the passwd command succeeded
if [[ "${?}" -ne 0 ]]
then
echo "The passwd command did not work successfully."
exit 4
fi
## Force password change on first login
passwd -e ${USERNAME}
echo "${USERNAME}:${PASSWORD}"
echo
done
# To see if users are all created
# less /etc/passwd
# To delete a user
# userdel USERNAME
exit 0