forked from thewhiterabbit/LEMP_MODX_Install_Bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_without_password.sh
123 lines (110 loc) · 2.91 KB
/
secure_without_password.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
# Author: Aaron K. Nall
set -e
# Get the username of the actual user
UN="$(who am i | awk '{print $1}')"
# Check for options
while [ -n "$1" ]; do # while loop starts
case "$1" in
-u)
USERNAME="$2"
shift
;;
-k)
KEY="$2"
shift
;;
-f)
FILE="true"
shift
;;
-fn)
FILENAME="$2"
shift
;;
-s)
SS="$2"
shift
;;
*)
echo "Option $1 is invalid."
echo "Exiting"
exit 1
;;
esac
shift
done
# Set the home directory in a variable
if [[ "$UN" != "root" ]] || [[ "$USERNAME" != "" ]]; then
if [[ "$USERNAME" != "" ]]; then
HD="/home/$USERNAME"
else
HD="/home/$UN"
fi
else
HD="/root"
fi
secure_user(){
sudo mkdir -p $HD/.ssh
if [[ "$FILE" = "" ]] && [[ "$FILENAME" = "" ]]; then
while [[ "$KEY" = "" ]]
do
if [[ "$USERNAME" = "" ]]; then
echo -n "Paste the SSH Key for $UN:"
else
echo -n "Paste the SSH Key for $USERNAME:"
fi
read -s KEY
done
sudo echo "$KEY" >| $HD/.ssh/authorized_keys
echo ""
echo "Private key was added to $HD/.ssh/authorized_keys"
echo ""
else
if [[ "$FILENAME" != "" ]]; then
sudo mv $HD/LEMP_MODX_Install_Bash/$FILENAME $HD/.ssh/authorized_keys
echo "$HD/LEMP_MODX_Install_Bash/$FILENAME was copied to $HD/.ssh/authorized_keys"
echo ""
else
sudo mv $HD/LEMP_MODX_Install_Bash/authorized_keys $HD/.ssh/authorized_keys
echo "$HD/LEMP_MODX_Install_Bash/authorized_keys was copied to $HD/.ssh/authorized_keys"
echo ""
fi
fi
}
secure_ssh(){
CF="/etc/ssh/sshd_config"
if test -f "$CF"; then
sudo rm $CF
fi
sudo cp $HD/LEMP_MODX_Install_Bash/sshd_conf /etc/ssh/sshd_config
echo ""
echo "/etc/ssh/sshd_config was changed to reflect $HD/LEMP_MODX_Install_Bash/sshd_conf"
echo ""
sleep 1
}
if [[ "$UN" != "root" ]]; then
if [[ ! $(sudo echo 0) ]]; then
echo "Permission Denied!"
echo ""
exit;
fi
if [[ "$SS" != "no" ]]; then
secure_ssh
else
echo ""
fi
secure_user
sudo service ssh reload
else
if [[ "$SS" != "no" ]]; then
secure_ssh
else
echo ""
fi
secure_user
service ssh reload
fi
echo "Your changes have been applied successfully."
echo "The ssh service has been reloaded."
echo ""