-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadd_local_user.sh
43 lines (36 loc) · 1.05 KB
/
add_local_user.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
#!/bin/bash
## Check if I am root
if [[ "${UID}" -ne 0 ]]
then
echo 'You are not root'
exit 1
fi
## Input
read -p 'Enter the username to create: ' USERNAME
read -p 'Enter the name of the person or application that will be using this acount: ' FULLNAME
read -p 'Enter the password to use for the account: ' PASSWORD
## Create the user.
## Notice that we quote ${FULLNAME} here is because this variable may include blanks
useradd -c "${FULLNAME}" -m ${USERNAME}
## Check to see if the useradd command succeeded
if [[ "${?}" -ne 0 ]]
then
echo "The useradd command did not work successfully."
exit 2
fi
## 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 3
fi
## Force password change on first login
passwd -e ${USERNAME}
## Display the username, password, and the host where the user was created
echo
echo 'username: ' ${USERNAME}
echo 'password: ' ${PASSWORD}
echo 'hostname: ' ${HOSTNAME}
exit 0