-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_user.sh
executable file
·51 lines (37 loc) · 1.33 KB
/
add_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
44
45
46
47
48
49
50
51
#!/bin/bash
### add user
echo "=====Welcome!"
echo "we need to get sudo permission first. Enter the password for admin below."
sudo ls
echo "=====Let's setup a new account and create a container now."
read -p "Enter your username: " USERNAME
if [[ -z "$USERNAME" ]]; then
echo "Please give me a username"
exit 1
fi
# create user
echo "Creating user..."
sudo useradd -m -s /var/scripts/login.sh -G lxd $USERNAME
sudo touch /home/$USERNAME/.hushlogin
# <<<<
printf "Allocating container for \e[96;1m$USERNAME\e[0m...\n"
# config the container
lxc init template ${USERNAME} -p default
# allocate ssh port
printf "Allocating ssh port... "
PORTFILE=/var/scripts/next-port
PORT=$(cat $PORTFILE)
echo $PORT | sudo tee /var/scripts/ports/$USERNAME
echo $(( $PORT+10 )) | sudo tee $PORTFILE
printf "\e[96;1m$PORT\e[0m\n"
lxc config device add ${USERNAME} sshproxy proxy listen=tcp:0.0.0.0:$PORT connect=tcp:127.0.0.1:22
# map uid
# lxc config device add $USERNAME door disk source=/home/$USERNAME path=/root/door
printf "uid $(id $USERNAME -u) 1000\ngid $(id $USERNAME -g) 1000" | lxc config set $USERNAME raw.idmap -
# password
echo "set password for $USERNAME now (host only)."
sudo passwd $USERNAME
echo "Login this host via \`ssh <username>@<host-ip>\` to manage your container."
# >>>>
echo "Done!"
read -p "Press any key to continue..." -n 1 -r