-
Notifications
You must be signed in to change notification settings - Fork 0
/
addusers
61 lines (50 loc) · 1.5 KB
/
addusers
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
#!/usr/bin/env bash
set -euo pipefail
[[ ${VERBOSE:-,,} =~ true|yes|on|1 ]] && set -x
# We aren't preserving /etc/passwd so hardcode the ids so they cannot change between runs.
# The usernames should match GitHub handles for fetching public SSH keys.
user_ids="
acostach:1000
alexgg:1001
floion:1002
jakogut:1003
klutchell:1004
majorz:1006
mtoman:1007
kb2ma:1008
rcooke-warwick:1009
jaomaloy:1010
shaunmulligan:1011
aethernet:1012
"
fetch_ssh_keys() {
local _username="${1}"
local _home="${2}"
(
cd "${_home}" || exit 1
mkdir -p .ssh
curl -fsSL "https://github.com/${_username}.keys" >.ssh/authorized_keys
chown -R "${_username}:${_username}" .ssh
chmod -R 700 .ssh
echo "Added $(wc -l <.ssh/authorized_keys) SSH keys for ${_username}"
)
}
# set the defaults for useradd/adduser
useradd -D --shell /bin/bash --base-dir /home
for user_id in ${user_ids}; do
# split the user_id into username and uid
name="${user_id%%:*}"
uid="${user_id##*:}"
echo "Creating user ${name}..."
# create a new user and home directory
useradd "${name}" --comment "${name}" --create-home --groups sudo,docker --uid "${uid}" || true
id "${name}"
# fetch the user's ssh keys from github
fetch_ssh_keys "${name}" "$(eval echo ~"${name}")" || true
done
if [ -n "${SET_HOSTNAME:-}" ]; then
# set the desired hostname
echo "${SET_HOSTNAME}" > /etc/hostname
hostname -F /etc/hostname
hostname
fi