Files
server-scripts/sshupdate.sh

145 lines
3.4 KiB
Bash
Raw Normal View History

2026-05-31 13:09:54 -05:00
#!/usr/bin/env bash
set -e
2026-05-31 14:19:47 -05:00
# ===============================
# CONFIG VALUES (EDIT HERE)
# ===============================
SSH_PORT="48291"
2026-05-31 13:09:54 -05:00
echo "==============================="
2026-05-31 14:19:47 -05:00
echo " Server Prep Script"
2026-05-31 13:09:54 -05:00
echo "==============================="
# -------------------------------
2026-05-31 14:19:47 -05:00
# Prompt for hostname
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
read -p "Enter new hostname (FQDN recommended): " NEW_HOSTNAME
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
if [ -z "$NEW_HOSTNAME" ]; then
echo "Hostname cannot be empty"
exit 1
fi
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
hostnamectl set-hostname "$NEW_HOSTNAME"
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
# -------------------------------
# Timezone
# -------------------------------
read -p "Enter timezone [default: America/Chicago]: " NEW_TZ
NEW_TZ=${NEW_TZ:-America/Chicago}
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
timedatectl set-timezone "$NEW_TZ"
timedatectl set-ntp true
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
# System update
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
apt update && apt upgrade -y
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
# Base packages
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
apt install -y \
curl \
ca-certificates \
gnupg \
lsb-release \
apt-transport-https \
software-properties-common \
fail2ban \
net-tools \
unzip \
jq
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
# -------------------------------
# SSH CONFIG (MODERN - socket)
# -------------------------------
echo "Configuring SSH socket on port ${SSH_PORT}..."
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
mkdir -p /etc/systemd/system/ssh.socket.d
cat > /etc/systemd/system/ssh.socket.d/override.conf <<EOF
[Socket]
ListenStream=
ListenStream=${SSH_PORT}
EOF
# Keep sshd_config aligned
sed -i "s/^#*Port .*/Port ${SSH_PORT}/" /etc/ssh/sshd_config
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
# SSH auth settings
sed -i "s/^#*PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config
if ! grep -q "^KbdInteractiveAuthentication" /etc/ssh/sshd_config; then
echo "KbdInteractiveAuthentication yes" >> /etc/ssh/sshd_config
2026-05-31 13:09:54 -05:00
else
2026-05-31 14:19:47 -05:00
sed -i "s/^KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/" /etc/ssh/sshd_config
2026-05-31 13:09:54 -05:00
fi
2026-05-31 14:19:47 -05:00
if ! grep -q "^ChallengeResponseAuthentication" /etc/ssh/sshd_config; then
echo "ChallengeResponseAuthentication yes" >> /etc/ssh/sshd_config
2026-05-31 13:09:54 -05:00
else
2026-05-31 14:19:47 -05:00
sed -i "s/^ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/" /etc/ssh/sshd_config
2026-05-31 13:09:54 -05:00
fi
2026-05-31 14:19:47 -05:00
systemctl daemon-reexec
systemctl daemon-reload
systemctl restart ssh.socket
2026-05-31 13:09:54 -05:00
systemctl restart ssh
2026-05-31 14:19:47 -05:00
# -------------------------------
# SSH KEY INSTALL
# -------------------------------
echo "Installing SSH key..."
SSH_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDosb5jR9eu4Avc0HmMzR8HQDYOGRSxwRYgprpDuggDG eddsa-key-20260531"
mkdir -p ~/.ssh
chmod 700 ~/.ssh
grep -qxF "$SSH_KEY" ~/.ssh/authorized_keys 2>/dev/null || echo "$SSH_KEY" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo "✅ SSH key added"
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
# Firewall
2026-05-31 13:09:54 -05:00
# -------------------------------
2026-05-31 14:19:47 -05:00
read -p "Disable UFW? (Y/n): " DISABLE_UFW
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
if [[ ! "$DISABLE_UFW" =~ ^[Nn]$ ]]; then
systemctl stop ufw || true
systemctl disable ufw || true
else
ufw allow ${SSH_PORT}/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable
2026-05-31 13:09:54 -05:00
fi
2026-05-31 14:19:47 -05:00
# -------------------------------
# Fail2Ban
# -------------------------------
systemctl enable fail2ban
systemctl start fail2ban
2026-05-31 13:09:54 -05:00
2026-05-31 14:19:47 -05:00
# -------------------------------
# Finish
# -------------------------------
IP_ADDR=$(hostname -I | awk '{print $1}')
2026-05-31 13:09:54 -05:00
echo ""
echo "==============================="
2026-05-31 14:19:47 -05:00
echo " ✅ SERVER READY"
2026-05-31 13:09:54 -05:00
echo "==============================="
2026-05-31 14:19:47 -05:00
echo "Hostname: $NEW_HOSTNAME"
echo "Timezone: $NEW_TZ"
echo "SSH Port: $SSH_PORT"
echo "IP: $IP_ADDR"
echo ""
echo "⚠️ TEST SSH NOW:"
echo "ssh -p $SSH_PORT root@$IP_ADDR"
``