-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcreate-system-users.sh
executable file
·69 lines (53 loc) · 1.48 KB
/
create-system-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
63
64
65
66
67
68
69
#!/bin/bash
DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
# Configure sudoers to require no password for these users
cp "$DIR/shared-config/20-sudo-nopassword" /etc/sudoers.d
# Find users file
HOSTNAME=$( hostname )
if [ -f "$DIR/$HOSTNAME/users" ]; then
USERS_FILE="$DIR/$HOSTNAME/users"
else
HOSTNAME=$( hostname -s )
if [ -f "$DIR/$HOSTNAME/users" ]; then
USERS_FILE="$DIR/$HOSTNAME/users"
fi
fi
if [ "$USERS_FILE" = "" ]; then
echo "Can't find users file"
exit -1
fi
echo Using $USERS_FILE to create users.
while IFS= read -r line; do
if [ "${line:0:1}" = "#" ]; then
continue
fi
echo
USER=$( echo $line | cut -d ":" -f 1 )
NAME=$( echo $line | cut -d ":" -f 2 )
GHUB=$( echo $line | cut -d ":" -f 3 )
if id "$USER" >/dev/null 2>&1; then
echo "$USER: found, adding to sudo group"
usermod $USER -a -G sudo
else
echo "$USER: not found, creating"
useradd $USER -c "$NAME" -G sudo -m -s /bin/bash
fi
echo -n "- Downloading SSH keys from GitHub: "
TMP_KEY_FILE="/tmp/tmp-$USER-key"
wget -q -O $TMP_KEY_FILE "https://github.com/$GHUB.keys"
if [ ! -f $TMP_KEY_FILE ]; then
echo "FAIL"
continue
else
echo "OK"
# Copy ssh keys and set permissions
mkdir -p /home/$USER/.ssh
chown $USER:$USER /home/$USER/.ssh
chmod 0700 /home/$USER/.ssh
cp $TMP_KEY_FILE /home/$USER/.ssh/authorized_keys
chown $USER:$USER /home/$USER/.ssh/authorized_keys
chmod 0600 /home/$USER/.ssh/authorized_keys
# Remove tmp key
rm $TMP_KEY_FILE
fi
done < $USERS_FILE