-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-host-user.sh
executable file
·41 lines (34 loc) · 996 Bytes
/
create-host-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
#!/bin/bash
set -e
function unused-gid() {
i=0
while true; do
if ! grep -q ":$i:" /etc/group; then
echo "$i"
exit 1
fi
((i++))
done
}
function move-existing-group-id() {
local group_id=$1
local group_name=$2
local found_group_name=$(getent group ${group_id} | cut -d ':' -f1)
if [ -n "${found_group_name}" ] ; then
local new_gid=$(unused-gid)
groupmod -g ${new_gid} ${found_group_name}
fi
}
# Add the user
useradd -l -u ${HOST_UID} -G root -s /bin/bash -M -N -d ${CLI_HOME} ${HOST_USER}
# Add the primary group from the host
move-existing-group-id ${HOST_GID} ${HOST_GROUP_NAME}
groupadd -g ${HOST_GID} ${HOST_GROUP_NAME}
usermod -g ${HOST_GROUP_NAME} ${HOST_USER}
# Optionally add the user to the docker group if on Linux
if [ -n "${HOST_DOCKER_GID}" ]; then
move-existing-group-id ${HOST_DOCKER_GID} docker
groupadd -g ${HOST_DOCKER_GID} docker
usermod -a -G docker ${HOST_USER}
fi
echo 'ALL ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers