-
Notifications
You must be signed in to change notification settings - Fork 0
/
onboard.sh
65 lines (53 loc) · 2.14 KB
/
onboard.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
63
64
65
# This script will read a CSV file that contains 20 new Linux users.
# This script will create each user on the server and add them to an already existing group called 'Developers'.
# This script will first check for the existence of the user on the system, before it will attempt to create the user.
# The user that is being created also must also have a default home folder.
# Each user should have a .ssh folder within its HOME folder. If it does not exist, then it will be created.
# For each user’s SSH configuration, We will create an authorized_keys file and add the below public key.
#!/bin/bash
userfile=$(less names.csv)
PASSWORD=password
# To ensure the user running this script has sudo/superuser privilege
if [ $(id -u) -eq 0 ]; then
# Reading the CSV file
for user in $userfile;
do
echo $user
if id "$user" &>/dev/null
then
echo "This User Exists"
else
# This will create a new user
useradd -m -d /home/$user -s /bin/bash -g developers $user
echo "New User Created"
echo
# This will create a ssh folder in the user home folder
su - -c "mkdir ~/.ssh" $user
echo ".ssh directory created for new user"
echo
# We need to set the user permission for the ssh dir
su - -c "chmod 700 ~/.ssh" $user
echo "user permission for .ssh directory set"
echo
# This will create an authorized-key file
su - -c "touch ~/.ssh/authorized_keys" $user
echo "Authorized Key File Created"
echo
# We need to set permission for the key file
su - -c "chmod 600 ~/.ssh/authorized_keys" $user
echo "user permission for the Authorized Key File set"
echo
# We need to create and set public key for users in the server
cp -R "/home/ubuntu/Shell/id_rsa.pub" "/home/$user/.ssh/authorized_keys"
echo "Copied the Public Key to New User Account on the server"
echo
echo
echo "USER CREATED"
# Generate a password.
sudo echo -e "$PASSWORD\n$PASSWORD" | sudo passwd "$user"
sudo passwd -x 5 $user
fi
done
else
echo "Only An Admin Can Onboard A User"
fi