Linux_Server_Tools/Init-Server.sh

147 lines
4.6 KiB
Bash
Raw Normal View History

2025-02-28 08:19:52 +01:00
#!/bin/bash
2025-02-28 08:31:52 +01:00
# Define color codes
2025-02-28 08:33:08 +01:00
GREEN='\e[32m'
BLUE='\e[34m'
YELLOW='\e[33m'
NC='\e[0m' # No Color
2025-02-28 08:31:52 +01:00
# Function for consistent log messages
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_skip() { echo -e "${YELLOW}[SKIPPED]${NC} $1"; }
2025-02-28 08:49:21 +01:00
# Function to check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
2025-02-28 08:26:43 +01:00
2025-02-28 08:49:21 +01:00
# Function to handle user input with Y/n prompt
get_user_confirmation() {
local prompt="$1"
local input
log_info "$prompt (Y/n)"
read -p "> " input
[[ -z "$input" || "$input" =~ ^[Yy]$ ]]
}
2025-02-28 08:26:43 +01:00
2025-02-28 08:49:21 +01:00
# Function to setup SSH directory and keys
setup_ssh() {
# Create .ssh directory if it doesn't exist
if [ ! -d "$HOME/.ssh" ]; then
mkdir -m 700 "$HOME/.ssh"
log_success ".ssh-Verzeichnis wurde erstellt."
else
log_info ".ssh-Verzeichnis existiert bereits."
fi
2025-02-28 08:30:25 +01:00
2025-02-28 08:49:21 +01:00
# Ask for SSH public key
log_info "SSH-Public-Key eingeben (Leerlassen zum Überspringen)"
read -p "> " user_input
2025-02-28 08:36:01 +01:00
2025-02-28 08:49:21 +01:00
if [ ! -z "$user_input" ]; then
if grep -q "$user_input" "$HOME/.ssh/authorized_keys"; then
log_info "Der SSH-Public-Key ist bereits vorhanden."
else
echo "$user_input" >> "$HOME/.ssh/authorized_keys"
log_success "SSH-Public-Key wurde hinzugefügt."
fi
else
log_skip "SSH-Key Installation übersprungen."
fi
}
2025-02-28 08:36:01 +01:00
2025-02-28 08:49:21 +01:00
# Function to update system packages
update_system() {
if get_user_confirmation "System aktualisieren?"; then
log_info "System wird aktualisiert..."
apt update && apt upgrade -y
log_success "System wurde erfolgreich aktualisiert."
2025-02-28 08:36:01 +01:00
else
2025-02-28 08:49:21 +01:00
log_skip "System-Aktualisierung übersprungen."
2025-02-28 08:36:01 +01:00
fi
2025-02-28 08:49:21 +01:00
}
2025-02-28 08:44:17 +01:00
2025-02-28 08:49:21 +01:00
# Function to install Docker and Dockge
install_docker() {
if get_user_confirmation "Docker installieren?"; then
if command_exists docker; then
log_info "Docker ist bereits installiert."
else
log_info "Docker wird installiert..."
# Install required packages
apt install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update and install Docker
apt update
apt install -y docker-ce docker-ce-cli containerd.io
# Start and enable Docker
systemctl start docker
systemctl enable docker
log_success "Docker wurde erfolgreich installiert."
fi
2025-02-28 08:45:39 +01:00
2025-02-28 08:49:21 +01:00
# Install Dockge if Docker is available
if command_exists docker; then
if get_user_confirmation "Dockge (Docker Compose Manager) installieren?"; then
log_info "Dockge wird installiert..."
docker run -d --name=dockge -p 5001:5001 -v /var/run/docker.sock:/var/run/docker.sock louislam/dockge
log_success "Dockge wurde erfolgreich installiert und läuft auf Port 5001."
else
log_skip "Dockge-Installation übersprungen."
fi
fi
2025-02-28 08:45:39 +01:00
else
2025-02-28 08:49:21 +01:00
log_skip "Docker-Installation übersprungen."
2025-02-28 08:45:39 +01:00
fi
2025-02-28 08:49:21 +01:00
}
2025-02-28 08:45:39 +01:00
2025-02-28 08:49:21 +01:00
# Function to install Node.js
install_nodejs() {
if get_user_confirmation "Node.js installieren?"; then
if command_exists node; then
log_info "Node.js ist bereits installiert."
else
log_info "Node.js wird installiert..."
apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt install -y nodejs
log_success "Node.js wurde erfolgreich installiert."
fi
else
log_skip "Node.js-Installation übersprungen."
fi
}
2025-02-28 08:44:17 +01:00
2025-02-28 08:49:21 +01:00
# Function to setup UFW firewall
setup_ufw() {
log_info "UFW (Uncomplicated Firewall) wird installiert und konfiguriert..."
if command_exists ufw; then
log_info "UFW ist bereits installiert."
2025-02-28 08:44:17 +01:00
else
2025-02-28 08:49:21 +01:00
apt install -y ufw
ufw allow ssh
echo "y" | ufw enable
log_success "UFW wurde erfolgreich installiert und konfiguriert. SSH-Verbindungen sind erlaubt."
2025-02-28 08:44:17 +01:00
fi
2025-02-28 08:49:21 +01:00
}
2025-02-28 08:46:49 +01:00
2025-02-28 08:49:21 +01:00
# Main execution
main() {
setup_ssh
update_system
install_docker
install_nodejs
setup_ufw
}
2025-02-28 08:46:49 +01:00
2025-02-28 08:49:21 +01:00
# Run main function
main