-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsecure-ssh.sh
More file actions
executable file
·106 lines (74 loc) · 1.85 KB
/
secure-ssh.sh
File metadata and controls
executable file
·106 lines (74 loc) · 1.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
100
101
102
103
104
105
106
#!/bin/bash
###
if ! which expect &> /dev/null;
then
echo "Unable to find command 'expect', run ./install-essentials.sh first"
exit
fi;
###
echo -n "Username: "
read USERNAME
if [ -z "$USERNAME" ]
then
echo "Username must not be empty"
exit
fi
###
echo -n "Shell Password: "
read -s PASSWORD
echo ""
echo -n "Retype Shell Password: "
read -s PASSWORD2
echo ""
if [ "$PASSWORD" != "$PASSWORD2" ]
then
echo "Shell Passwords do not match"
exit
fi
###
echo -n "SSH Public Key, e.g. \"ssh-rsa [PUBLICKEY]\" (end input with ESC):"
read -d `echo -e "\e"` PUBLICKEY
echo ""
if [ -z "$PUBLICKEY" ]
then
echo "Public Key must not be empty"
exit
fi
###
echo -n "SSH Port [default=22]: "
read PORT
if [ -z "$PORT" ]
then
PORT='22'
fi
###
useradd -s /bin/bash -m $USERNAME
# add user to sudo group (append)
usermod -a -G sudo $USERNAME
# add user password
expect << EOF
spawn passwd $USERNAME
expect "Enter new UNIX password:"
send "${PASSWORD}\r"
expect "Retype new UNIX password:"
send "${PASSWORD}\r"
expect eof;
EOF
###
mkdir /home/$USERNAME/.ssh
touch /home/$USERNAME/.ssh/authorized_keys
# remove newlines from public key
echo "$PUBLICKEY" | sed ':a;N;$!ba;s/\n//g' >> /home/$USERNAME/.ssh/authorized_keys
###
# backup original config file
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -e "s/#\?Port .*/Port $PORT/g" -i /etc/ssh/sshd_config
sed -e "s/#\?RSAAuthentication .*/RSAAuthentication yes/g" -i /etc/ssh/sshd_config
sed -e "s/#\?PubkeyAuthentication .*/PubkeyAuthentication yes/g" -i /etc/ssh/sshd_config
sed -e "s/#\?PermitRootLogin .*/PermitRootLogin no/g" -i /etc/ssh/sshd_config
sed -e "s/#\?PasswordAuthentication .*/PasswordAuthentication no/g" -i /etc/ssh/sshd_config
# restart ssh
service ssh restart
echo ""
echo "Before disconnecting, open another terminal window and test an ssh/private-key connection"
echo ""