bash_bootstrap/bootstrap.sh

68 lines
2 KiB
Bash
Raw Normal View History

2025-04-06 11:21:13 +00:00
#!/bin/bash
set -euo pipefail
# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
2025-06-11 17:17:28 +00:00
echo "[✗] Dieses Skript muss als root ausgeführt werden." >&2
exit 1
fi
# Prompt for username input
read -p "Bitte geben Sie den Benutzernamen ein: " USERNAME
# Check if the username is not empty
if [ -z "$USERNAME" ]; then
echo "[✗] Kein Benutzername eingegeben." >&2
exit 1
fi
2025-06-11 17:12:16 +00:00
SSH_KEY_URL="https://skulldev.de/Skull-IT/trusted-ssh-keys/raw/branch/main/trusted-ssh-keys"
# Set SSH directory based on username
if [ "$USERNAME" = "root" ]; then
SSH_DIR="/root/.ssh"
else
SSH_DIR="/home/$USERNAME/.ssh"
fi
2025-04-06 11:21:13 +00:00
2025-06-11 17:17:28 +00:00
# Update package list and install necessary packages
2025-06-11 17:12:16 +00:00
echo "[+] Updating package list and installing essential packages..."
2025-06-11 17:17:28 +00:00
apt-get update
apt-get install -y wget openssh-server
2025-04-06 11:21:13 +00:00
2025-06-11 17:17:28 +00:00
# Setting up SSH directory and authorized keys
2025-04-06 11:21:13 +00:00
echo "[+] Setting up SSH directory..."
mkdir -p "$SSH_DIR"
2025-06-11 17:17:28 +00:00
wget -O "$SSH_DIR/authorized_keys" "$SSH_KEY_URL"
2025-04-06 11:21:13 +00:00
chown -R "$USERNAME:$USERNAME" "$SSH_DIR"
chmod 700 "$SSH_DIR"
chmod 600 "$SSH_DIR/authorized_keys"
# Configure SSH server to allow public key authentication
echo "[+] Configuring SSH server..."
SSH_CONFIG="/etc/ssh/sshd_config"
sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication yes/' "$SSH_CONFIG"
sed -i 's/^PubkeyAuthentication no/PubkeyAuthentication yes/' "$SSH_CONFIG"
sed -i 's/^#AuthorizedKeysFile/AuthorizedKeysFile/' "$SSH_CONFIG"
sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' "$SSH_CONFIG"
# Restart SSH service to apply changes
systemctl restart ssh
2025-06-11 17:17:28 +00:00
# Adding user to sudo group
2025-04-06 11:21:13 +00:00
echo "[+] Adding user to sudo group..."
2025-06-11 17:17:28 +00:00
apt-get install -y sudo
2025-04-06 11:21:13 +00:00
usermod -aG sudo "$USERNAME"
2025-06-11 17:17:28 +00:00
# Configuring passwordless sudo for the user
2025-04-06 11:21:13 +00:00
echo "[+] Configuring passwordless sudo for $USERNAME..."
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" | tee "/etc/sudoers.d/$USERNAME" > /dev/null
2025-04-06 11:21:13 +00:00
chmod 440 "/etc/sudoers.d/$USERNAME"
2025-06-11 17:17:28 +00:00
# Setting timezone
2025-04-06 11:21:13 +00:00
echo "[+] Setting timezone to Europe/Berlin..."
2025-06-11 17:17:28 +00:00
apt-get install -y tzdata
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
2025-06-11 17:17:28 +00:00
echo "[✓] Bootstrap complete."