-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.sh
More file actions
73 lines (47 loc) · 2.16 KB
/
ssh.sh
File metadata and controls
73 lines (47 loc) · 2.16 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
#!/bin/sh
coded_by='
In the name of Allah, the most Gracious, the most Merciful.
▓▓▓▓▓▓▓▓▓▓
░▓ Author ▓ Abdullah <https://abdullah.today>
░▓▓▓▓▓▓▓▓▓▓ YouTube <https://YouTube.com/AbdullahToday>
░░░░░░░░░░
░█▀▀░█▀▀░█░█░░░█▀▀░█▀▀░█▀▀░█░█░█▀▄░█▀▀░█▀▄
░▀▀█░▀▀█░█▀█░░░▀▀█░█▀▀░█░░░█░█░█▀▄░█▀▀░█░█
░▀▀▀░▀▀▀░▀░▀░░░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀▀░
'
# Make a server ready for ssh
echo "$coded_by"
sleep 2
server_config_file="/etc/ssh/sshd_config"
super_key="/etc/super_key"
# Exit if not run as root
if [ "$(id -u)" -ne 0 ]; then
echo 'Error: This script must be run as root or with sudo! Bye' >&2
exit 1
fi
# Ask user to enter the public SSH key.
read -p "Enter public SSH key: " public_key
# Check if user entered the key or not.
if [ -z "$public_key" ]; then
echo "You didn't provide SSH Public key."
exit 1
fi
# Check if required directories exist on server with correct permissions, create them if not, copy ID and change file permissions.
if [ ! -d ~/.ssh ]; then
mkdir -m 700 ~/.ssh >/dev/null 2>&1
echo "$public_key" >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys >/dev/null 2>&1
fi
# Enable Public key authentication.
sed -i '/PubKeyAuthentication/c\PubKeyAuthentication yes' "$server_config_file"
# Disable password authentication
sed -i '/PasswordAuthentication/c\PasswordAuthentication no' "$server_config_file"
sed -i '/UsePAM/c\UsePAM no' $server_config_file
# Disable root login with clear password. Only allow root login with Public Key authentication
sed -i '/PermitRootLogin/c\PermitRootLogin without-password' "$server_config_file"
# Enable super key. So you can login as any user.
sed -i '/AuthorizedKeysFile/c\AuthorizedKeysFile %h/.ssh/authorized_keys /etc/super_key' "$server_config_file"
# Add this key as super key
echo "$public_key" >> "$super_key"
# And finally restart SSH service
systemctl restart sshd