-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateuser.sh
79 lines (58 loc) · 1.66 KB
/
createuser.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
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Create a user with ssh access for remote login.
# Author: Balli Asghar
# Check if the user is root
if [ $(id -u) -ne 0 ]; then
echo "You must be root to run this script."
exit 1
fi
echo "Enter User Information"
# ask for username
read -p "🙍 username: " username
# check if username is empty
if [ -z "$username" ]; then
echo "Username cannot be empty."
exit 1
fi
# check if user already exists
if id "$username" >/dev/null 2>&1; then
echo "User already exists."
exit 1
fi
# ask for password
password=`systemd-ask-password "Enter password:"`
# check if password is empty
if [ -z "$password" ]; then
echo "Password cannot be empty."
exit 1
fi
# Ask for public key
read -p "🔑 Enter public key (from you local machine): " publickey
# check if public key is empty
if [ -z "$publickey" ]; then
echo "Public key cannot be empty."
exit 1
fi
# validate public key
if ! echo "$publickey" | grep -q "ssh-rsa"; then
echo "Invalid public key"
exit 1
fi
# create user
useradd -m -s /bin/bash $username
# # set password
echo "$username:$password" | chpasswd
# # add user to sudo group
usermod -aG sudo $username
# # create .ssh directory
mkdir /home/$username/.ssh
# # change ownership of .ssh directory
chown $username:$username /home/$username/.ssh
# # add public key to authorized_keys
echo $publickey >> /home/$username/.ssh/authorized_keys
# # change ownership of authorized_keys
chown $username:$username /home/$username/.ssh/authorized_keys
echo -e "\e[32mUser created successfully.\e[0m"
# ip address of the server
ipaddress=`hostname -I | awk '{print $1}'`
echo -e "\e[33mLogin with: ssh $username@$ipaddress\e[0m"