-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateUser.sh
More file actions
executable file
·99 lines (85 loc) · 2.85 KB
/
Copy pathcreateUser.sh
File metadata and controls
executable file
·99 lines (85 loc) · 2.85 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# Created 20160504 by eeemil
# MIT License, no warranty, if this blows stuff up: blame yourself
# He he he
#
# WARNING: TODO, ADD MORE HOSTS
#
if [[ ! `whoami` == "root" ]]; then
echo "Lol, you are not root!"
echo "You have to be root"
exit 1
fi
echo -n "Enter username: "
read USERNAME
USERNAME=$(echo "$USERNAME" | tr '[:upper:]' '[:lower:]')
if [[ "$USERNAME" =~ [^a-z0-9] ]]; then
echo "Invalid username"
exit 2
fi
# Already exists?
getent passwd "$USERNAME" >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "User already exists!"
exit 2
fi
echo -n "Enter full name: "
read FULLNAME
if [[ "$FULLNAME" =~ [^\ a-zA-Z] ]]; then
echo "Invalid name"
exit 2
fi
PASSWORD=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-12};echo`
HOSTS="osthyvel diskborste grytvante stekspade yxa elvisp paltslev pastaslev"
HOSTS=$(echo $HOSTS | sed "s/\<`hostname`\>//g")
echo $HOSTS
echo "Creating user on local machine..."
# Creating user...
useradd -m "$USERNAME" >/dev/null
chfn -f \""$FULLNAME"\" "$USERNAME" >/dev/null
# Zsh: default shell
chsh -s "/usr/bin/zsh" "$USERNAME" >/dev/null
passwd "$USERNAME" >/dev/null 2>&1 <<EOF
$PASSWORD
$PASSWORD
EOF
# Add to sudoers
usermod -aG sudo "$USERNAME" >/dev/null
# Populate home with neat stuff
echo "Configuring, setting up ssh key..."
## SSH key (will enable roaming across computorz in project room)
sudo -u "$USERNAME" ssh-keygen -N "" -t rsa -b 4096 -f "/home/$USERNAME/.ssh/id_rsa" -C "automatically generated by eeemil's script createUser.sh" >/dev/null
sudo -u "$USERNAME" cp "/home/$USERNAME/.ssh/id_rsa.pub" "/home/$USERNAME/.ssh/authorized_keys" >/dev/null
## ZSH-config
sudo -u "$USERNAME" cp "/etc/zsh/newuser.zshrc.recommended" "/home/$USERNAME/.zshrc" >/dev/null
## Todo: a git config, perhaps?
echo -e "Done, cloning user to other hosts\n"
for HOSTNAME in ${HOSTS} ; do
echo "Adding $USERNAME on $HOSTNAME"
ssh "$HOSTNAME" getent passwd "$USERNAME" >/dev/null 2>&1
ret=$?
if [[ "$ret" -eq 0 ]]; then
echo "User already exists! Skipping..."
continue
elif [[ "$ret" -eq 255 ]]; then
echo "$HOSTNAME not reachable, skipping..."
continue
fi
ssh ${HOSTNAME} useradd -m "$USERNAME" >/dev/null
ssh ${HOSTNAME} chfn -f \""$FULLNAME"\" "$USERNAME" >/dev/null
ssh ${HOSTNAME} chsh -s "/usr/bin/zsh" "$USERNAME" >/dev/null
ssh ${HOSTNAME} passwd "$USERNAME" >/dev/null 2>&1 <<EOF
$PASSWORD
$PASSWORD
EOF
ssh ${HOSTNAME} usermod -aG sudo "$USERNAME"
echo "Copying files, setting up ssh keys..."
rsync -rp "/home/$USERNAME" "${HOSTNAME}:/home/" >/dev/null
ssh ${HOSTNAME} chown -R "$USERNAME:$USERNAME" "/home/$USERNAME" >/dev/null
echo -e "$USERNAME added on $HOSTNAME!\n---------"
done
echo -e "\n\n========SUMMARY========="
echo -e "Username: $USERNAME"
echo -e "Password: $PASSWORD"
echo -e "User created on the following hosts:"
echo -e `hostname` "$HOSTS"