-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure-ssh.sh
More file actions
47 lines (33 loc) · 1.17 KB
/
Copy pathsecure-ssh.sh
File metadata and controls
47 lines (33 loc) · 1.17 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
#!/bin/bash
set -e
echo "==== Secure Server Setup ===="
read -p "Enter new username: " USERNAME
read -p "Paste your SSH public key: " PUBKEY
echo "[+] Creating user..."
adduser --disabled-password --gecos "" $USERNAME
echo "[+] Adding user to sudo group..."
usermod -aG sudo $USERNAME
echo "[+] Creating SSH directory..."
mkdir -p /home/$USERNAME/.ssh
echo "[+] Adding public key..."
echo "$PUBKEY" > /home/$USERNAME/.ssh/authorized_keys
echo "[+] Setting permissions..."
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
echo "[+] Securing SSH config..."
SSHD_CONFIG="/etc/ssh/sshd_config"
sed -i 's/^#*PermitRootLogin.*/PermitRootLogin no/' $SSHD_CONFIG
sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' $SSHD_CONFIG
sed -i 's/^#*PubkeyAuthentication.*/PubkeyAuthentication yes/' $SSHD_CONFIG
echo "[+] Restarting SSH service..."
systemctl restart ssh || systemctl restart sshd
echo ""
echo "==== Setup Complete ===="
echo "New user: $USERNAME"
echo ""
echo "IMPORTANT:"
echo "Open a NEW terminal and test login before closing this session:"
echo ""
echo "ssh $USERNAME@SERVER_IP"
echo ""