Add Linux/defaultSetup/linux_fresh.sh

This commit is contained in:
2025-05-07 18:37:29 +00:00
parent d3cc65f3ea
commit 7f22a21552

View File

@@ -0,0 +1,122 @@
#!/bin/sh
set -e
USERNAME="ansible"
SSH_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICBpQgsPoNARIuzw+YTuADA8lk7S3y9wqvPxtDAsdYbw ansible@deploy"
# Farben
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# OS-Erkennung
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID
else
printf "${RED}✗ Kann Betriebssystem nicht erkennen.${NC}\n"
exit 1
fi
# Alpine Setup
setup_alpine() {
printf "${BLUE}→ Alpine Linux erkannt führe Setup aus${NC}\n"
# Repositories setzen
cat << 'EOF' > /etc/apk/repositories
https://dl-cdn.alpinelinux.org/alpine/latest-stable/main
https://dl-cdn.alpinelinux.org/alpine/latest-stable/community
EOF
apk update
apk add --no-cache curl bash sudo python3 openssh-server
# Benutzer anlegen
create_user_alpine
}
# Debian/Ubuntu Setup
setup_debian() {
printf "${BLUE}→ Debian/Ubuntu erkannt führe Setup aus${NC}\n"
apt-get update && apt-get upgrade -y
create_user_debian
}
# RHEL/CentOS Setup
setup_rhel() {
printf "${BLUE}→ RHEL/AlmaLinux erkannt führe Setup aus${NC}\n"
yum update -y || dnf update -y
create_user_rhel
}
# Benutzer anlegen
create_user_rhel() {
if id "$USERNAME" >/dev/null 2>&1; then
printf "${YELLOW}↷ Benutzer $USERNAME existiert bereits überspringe${NC}\n"
else
sudo useradd -m -s /bin/bash "$USERNAME"
fi
common_user_setup
}
create_user_debian() {
if id "$USERNAME" >/dev/null 2>&1; then
printf "${YELLOW}↷ Benutzer $USERNAME existiert bereits überspringe${NC}\n"
else
sudo adduser --disabled-password --gecos "" "$USERNAME"
fi
common_user_setup
}
create_user_alpine() {
if id "$USERNAME" >/dev/null 2>&1; then
printf "${YELLOW}↷ Benutzer $USERNAME existiert bereits überspringe${NC}\n"
else
sudo adduser -D -s /bin/sh "$USERNAME"
fi
common_user_setup
}
# Gemeinsames Setup für alle Systeme
common_user_setup() {
printf "${YELLOW}→ Überprüfe ob Benutzer $USERNAME in /etc/shadow gesperrt ist${NC}\n"
if sudo grep -E "^$USERNAME:(!+|\*+):" /etc/shadow >/dev/null; then
printf "${YELLOW}→ Benutzer ist gesperrt, entsperre für SSH-Key-Login${NC}\n"
sudo sed -i -r "s/^($USERNAME:)(!+|\*+)(:.*)/\1\3/" /etc/shadow
else
printf "${GREEN}→ Benutzer ist nicht gesperrt${NC}\n"
fi
# SSH-Verzeichnis und Key setzen
sudo mkdir -p /home/$USERNAME/.ssh
echo "$SSH_KEY" | sudo tee /home/$USERNAME/.ssh/authorized_keys > /dev/null
sudo chmod 700 /home/$USERNAME/.ssh
sudo chmod 600 /home/$USERNAME/.ssh/authorized_keys
sudo chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
# Sudo ohne Passwort
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$USERNAME > /dev/null
sudo chmod 440 /etc/sudoers.d/$USERNAME
printf "${GREEN}✓ Benutzer $USERNAME wurde erfolgreich eingerichtet.${NC}\n"
}
# Hauptlogik
case "$OS_ID" in
alpine)
setup_alpine
;;
debian|ubuntu)
setup_debian
;;
almalinux|centos|rhel|rocky|fedora)
setup_rhel
;;
*)
printf "${RED}✗ Nicht unterstütztes Betriebssystem: $OS_ID${NC}\n"
exit 1
;;
esac