Files
operating-automation/docs/07-Basis-System.md
Ansible Servercow da81549161 add docs
2026-06-11 18:04:41 +02:00

16 KiB

Basis-System

Übersicht

Diese Kategorie umfasst Playbooks und Rollen für grundlegende Systemkonfiguration und Infrastruktur-Services wie ClamAV-Antivirus, Zeitsynchronisation (Chrony) und allgemeine System-Setup-Aufgaben.

Anwendungsfälle:

  • Installation von ClamAV-Antivirus-Server
  • NTP-Zeitserver-Konfiguration (Chrony)
  • Docker-Installation und -Konfiguration
  • Basis-Tools-Installation
  • System-Härtung
  • MOTD-Konfiguration
  • Admin-Benutzer-Erstellung

Playbooks

deploy-clamav-server.yml

Bereitstellung eines dedizierten ClamAV-Antivirus-Servers.

Datei: playbooks/deploy-clamav-server.yml

Zweck: Installation und Konfiguration eines zentralen ClamAV-Servers für Antivirus-Scanning. Dieser Server wird von Mailcow-Instanzen für Malware-Scanning verwendet.

Ziel-Hosts: clamav-servers (Inventory-Gruppe)

Benutzer: tincadmin (mit sudo-Rechten)

Verwendete Rollen:

  • deploy-clamd

Verwendungsbeispiel:

# ClamAV-Server bereitstellen
ansible-playbook playbooks/deploy-clamav-server.yml \
  -i inventories/icp-fra-pve1.yml \
  -K

# Mit spezifischer ClamAV-Version
ansible-playbook playbooks/deploy-clamav-server.yml \
  -i inventories/icp-fra-pve1.yml \
  -e "clamd_version=1.4.3" \
  -K

Abhängigkeiten:

  • deploy-clamd Rolle
  • Build-Tools (gcc, cmake, Rust)
  • Internet-Zugriff für ClamAV-Source-Download

Besonderheiten:

  • Kompiliert ClamAV von Source (Version 1.4.2)
  • Installiert FreshClam für Signatur-Updates
  • Konfiguriert systemd-Services
  • Erstellt dedizierte clamav-User/Group

Workflow:

  1. Installiert Build-Dependencies
  2. Erstellt clamav-Benutzer und -Gruppe
  3. Lädt ClamAV-Source herunter
  4. Kompiliert mit CMake
  5. Installiert Binärdateien
  6. Konfiguriert ClamAV-Daemon
  7. Konfiguriert FreshClam (Signatur-Updates)
  8. Erstellt und startet systemd-Services

Integration: Siehe Mail-Server → migrate-clamd.yaml

⚠️ Hinweis: Kompilation kann 10-20 Minuten dauern je nach System-Performance.


setup-chronyd.yml

Setup des Chronyd-NTP-Zeitservers.

Datei: playbooks/setup-chronyd.yml

Zweck: Installation und Konfiguration von Chrony für präzise Zeitsynchronisation auf Debian/Ubuntu-Systemen.

Ziel-Hosts: all (gefiltert auf Debian/Ubuntu)

Benutzer: tincadmin (mit sudo-Rechten)

Verwendete Rollen:

  • system (Task: setup-timeserver)

Verwendungsbeispiel:

# Chrony auf allen Hosts installieren
ansible-playbook playbooks/setup-chronyd.yml \
  -i inventories/icp-fra-pve1.yml \
  -K

# Auf einzelnem Host
ansible-playbook playbooks/setup-chronyd.yml \
  -i inventories/icp-fra-pve1.yml \
  --limit timeserver.example.com \
  -K

Abhängigkeiten:

  • system Rolle
  • Debian/Ubuntu Betriebssystem

Besonderheiten:

  • Einfaches Playbook ohne komplexe Logik
  • Deinstalliert NTPd (falls vorhanden, Konflikt mit Chrony)
  • Installiert Chrony
  • Konfiguriert NTP-Pools
  • Startet und aktiviert chronyd-Service

Workflow:

  1. Entfernt ntpd (falls installiert)
  2. Installiert chrony-Paket
  3. Konfiguriert NTP-Server-Pools
  4. Startet chronyd-Service
  5. Aktiviert Service beim Boot

Warum Chrony statt NTP:

  • Bessere Performance bei instabilen Verbindungen
  • Schnellere Synchronisation nach Boot
  • Geringerer Ressourcenverbrauch
  • Moderne Alternative zu ntpd

Rollen

Rolle: deploy-clamd

Zweck: Kompiliert, konfiguriert und stellt ClamAV-Antivirensoftware bereit.

Pfad: roles/deploy-clamd/

Hauptaufgaben:

1. install-dependencies.yml

Installiert Build-Tools und Abhängigkeiten.

Installierte Pakete:

  • Build-Essentials: gcc, g++, make, cmake
  • Rust-Toolchain (für ClamAV 1.4.x)
  • Python3 und pip
  • Entwicklungsbibliotheken: libssl-dev, libjson-c-dev, libpcre2-dev
  • Zusatztools: wget, curl, pkg-config

Besonderheit: Installiert Rust über rustup (erforderlich ab ClamAV 1.4.x)

2. setup-clamav-user-group.yml

Erstellt System-Benutzer und -Gruppe für ClamAV.

Funktionen:

  • Erstellt Gruppe clamav
  • Erstellt System-User clamav (no-login)
  • Erstellt Verzeichnisse:
    • /var/lib/clamav (Datenbank)
    • /var/log/clamav (Logs)
    • /var/run/clamav (PID-Files)
  • Setzt korrekte Berechtigungen

3. compile-clamav.yml

Lädt ClamAV-Source herunter und kompiliert.

Funktionen:

  • Download von GitHub (Version 1.4.2)
  • Entpackt Source
  • Konfiguriert mit CMake:
    cmake . \
      -DCMAKE_INSTALL_PREFIX=/usr/local \
      -DENABLE_SYSTEMD=ON \
      -DENABLE_JSON_SHARED=ON
    
  • Kompiliert mit make -j$(nproc)
  • Installiert mit make install

Dauer: 10-20 Minuten je nach CPU

4. configure-clamav.yml

Konfiguriert ClamAV-Daemon und FreshClam, erstellt systemd-Services.

Funktionen:

  • Generiert clamd.conf aus Template
  • Generiert freshclam.conf aus Template
  • Erstellt systemd-Service-Files:
    • clamav-daemon.service
    • clamav-freshclam.service
  • Reload systemd
  • Startet Services

Standardvariablen (defaults/main.yml):

Variable Default Beschreibung
clamd_version 1.4.2 ClamAV-Version zum Kompilieren

Templates:

clamav-config.j2

ClamAV-Daemon-Konfiguration.

Wichtige Einstellungen:

LogFile /var/log/clamav/clamd.log
LogFileMaxSize 10M
LogTime yes
LogSyslog yes

# Scanning-Parameter
MaxThreads 4
MaxDirectoryRecursion 15
MaxFileSize 100M

# Network
TCPSocket 3310
TCPAddr 0.0.0.0  # Lauscht auf allen Interfaces

freshclam-config.j2

FreshClam-Konfiguration (Virus-Signatur-Updates).

Wichtige Einstellungen:

DatabaseDirectory /var/lib/clamav
LogFile /var/log/clamav/freshclam.log

# Update-Häufigkeit
Checks 24  # 24x pro Tag (jede Stunde)

# Update-Quellen
DatabaseMirror database.clamav.net
DatabaseMirror db.local.clamav.net

# SecureInfo URLs für Premium-Signaturen
# (optional, falls lizenziert)

systemd-clamav-service.j2

Systemd-Service für ClamAV-Daemon.

[Unit]
Description=ClamAV Antivirus Daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/sbin/clamd
Restart=on-failure
User=clamav
Group=clamav

[Install]
WantedBy=multi-user.target

systemd-freshclam-service.j2

Systemd-Service für FreshClam.

[Unit]
Description=ClamAV Virus Database Updater
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/freshclam -d
Restart=on-failure
User=clamav
Group=clamav

[Install]
WantedBy=multi-user.target

Handler:

Handler Funktion
Reload Systemd Daemon Lädt systemd-Konfiguration neu
Start Clamd Service Startet ClamAV-Daemon
Start Freshclam Service Startet FreshClam-Service

Verwendung:

- name: Deploy ClamAV Server
  hosts: clamav-servers
  become: true
  roles:
    - deploy-clamd

Weiterführende Informationen:

  • Scanning-Performance: 50-200 MB/s je nach Hardware
  • RAM-Bedarf: ~1-2 GB (für Signatur-Datenbank)
  • Signatur-Updates: Automatisch jede Stunde
  • Network-Port: 3310 (TCP)

Rolle: system

Zweck: Grundlegende Systemkonfiguration und Administration.

Pfad: roles/system/

Hauptaufgaben:

1. install-basic-tools.yaml

Installiert essenzielle System-Tools.

Installierte Pakete:

  • git - Versionskontrolle
  • curl, wget - Download-Tools
  • vim - Editor
  • htop - Prozess-Monitor
  • net-tools - Netzwerk-Tools (ifconfig, netstat)
  • tcpdump - Netzwerk-Paket-Analyse
  • bind9-dnsutils - DNS-Tools (dig, nslookup)
  • gnupg - GPG-Verschlüsselung
  • sudo - Privilege Escalation

Verwendung:

- include_role:
    name: system
    tasks_from: install-basic-tools

2. install-docker.yaml

Installiert Docker von offiziellem Repo oder tinc-Mirror.

Funktionen:

  • Fügt Docker GPG-Key hinzu
  • Konfiguriert Docker APT-Repository
  • Installiert Docker CE, CLI, containerd
  • Konfiguriert Docker-Daemon
  • Startet und aktiviert Service

Variablen:

docker_install_source: "official"  # oder "tinc"

Verwendung:

- include_role:
    name: system
    tasks_from: install-docker

Siehe auch: Container-Management → install-docker.yml

3. install-docker-image-mirror.yaml

Konfiguriert Docker HTTP-Proxy für Image-Mirror.

Funktionen:

  • Installiert CA-Zertifikat für Proxy
  • Konfiguriert systemd-Override für Docker
  • Setzt HTTP_PROXY, HTTPS_PROXY Environment-Variablen
  • Restart Docker-Daemon

Variablen:

use_docker_image_mirror: true
docker_mirror_location: "SC"  # oder "tinc"

Proxy-Server:

  • SC (ServerCow): dim.servercow.com:3128
  • tinc: [2a07:6fc0:c:2809::20]:3128

Verwendung:

- include_role:
    name: system
    tasks_from: install-docker-image-mirror

4. setup-timeserver.yaml

Installiert und konfiguriert Chrony für NTP.

Funktionen:

  • Deinstalliert ntpd (Konflikt-Vermeidung)
  • Installiert chrony
  • Konfiguriert NTP-Pools:
    • 0.debian.pool.ntp.org
    • 1.debian.pool.ntp.org
    • 2.debian.pool.ntp.org
    • 3.debian.pool.ntp.org
  • Startet und aktiviert chronyd

Verwendung:

- include_role:
    name: system
    tasks_from: setup-timeserver

5. ssh-hardening.yaml

Ruft SSH-Schlüssel von URL ab und härtet sshd_config.

Funktionen:

  • Download autorisierter SSH-Keys von URL
  • Konfiguriert sshd_config mit Härtung:
    • PermitRootLogin without-password
    • PasswordAuthentication no
    • PubkeyAuthentication yes
    • Moderne Krypto-Algorithmen
  • Restart SSH-Service

Template: templates/sshd_config.j2

Verwendung:

- include_role:
    name: system
    tasks_from: ssh-hardening

⚠️ Vorsicht: Testen Sie auf einem Host zuerst, um Aussperrung zu vermeiden!

6. check-disk-utilization.yaml

Prüft mindestens 4 GB verfügbaren Speicherplatz.

Funktionen:

  • Ermittelt verfügbaren Speicher auf /
  • Schlägt fehl wenn weniger als 4 GB frei
  • Nützlich vor großen Updates

Verwendung:

- include_role:
    name: system
    tasks_from: check-disk-utilization

Verwendung in: Mail-Server → update-mailcow.yaml

7. special-admin-create.yaml

Erstellt Admin-Benutzer mit Sudo ohne Passwort.

Funktionen:

  • Erstellt Benutzer specialadmin (konfigurierbarer Name)
  • Fügt zu sudo-Gruppe hinzu
  • Konfiguriert passwordless sudo
  • Kopiert SSH-Keys

Verwendung:

- include_role:
    name: system
    tasks_from: special-admin-create
  vars:
    admin_username: myadmin

8. install-motd.yaml

Installiert custom Message of the Day (MOTD).

Funktionen:

  • Kopiert MOTD-File von files/motd
  • Zeigt bei jedem SSH-Login
  • Kann Branding, Warnungen, Informationen enthalten

File: roles/system/files/motd

Verwendung:

- include_role:
    name: system
    tasks_from: install-motd

Standardvariablen:

# Docker-Konfiguration
use_docker_image_mirror: true
docker_mirror_location: "SC"  # oder "tinc"
docker_install_source: "official"

# NTP-Server
ntp_servers:
  - 0.debian.pool.ntp.org
  - 1.debian.pool.ntp.org
  - 2.debian.pool.ntp.org
  - 3.debian.pool.ntp.org

Handler:

Handler Funktion
Reload systemd Lädt systemd-Konfiguration neu
Restart SSH Startet SSH-Daemon neu
Restart Docker Startet Docker-Service neu
Restart chronyd Startet Chrony-Service neu
Enable Docker Aktiviert Docker beim Boot
Start Docker Startet Docker-Service

Templates:

sshd_config.j2

SSH-Daemon-Konfiguration mit Härtung.

Wichtige Einstellungen:

# Authentifizierung
PermitRootLogin without-password
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no

# Krypto
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org

# Sicherheit
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 3
LoginGraceTime 20

Files:

files/motd

Custom Message of the Day.

Kann enthalten:

  • Systemidentifikation
  • Sicherheitshinweise
  • Kontaktinformationen
  • Maintenance-Ankündigungen

Verwendung der Rolle:

# Vollständige System-Setup
- name: System Base Configuration
  hosts: all
  become: true
  roles:
    - system

Rolle: updates

Zweck: Verschiedene Update-Mechanismen für unterschiedliche Distributionen.

Pfad: roles/updates/

Hauptaufgaben:

1. apk-based-update.yaml

Für Alpine Linux Updates.

Funktionen:

  • apk update
  • apk upgrade

2. apt-based-update.yaml

Für Debian/Ubuntu Updates.

Funktionen:

  • apt update
  • apt dist-upgrade
  • Kernel-Update-Prüfung

3. apt-cleaning.yaml

APT-Cache-Bereinigung.

Funktionen:

  • apt clean
  • apt autoclean
  • apt autoremove

4. debian-major-upgrade-apt.yaml

Upgrade auf neue Debian-Hauptversion.

Funktionen:

  • Ändert Paketquellen
  • apt update
  • apt dist-upgrade

Verwendung:

Diese Rolle wird als Fallback verwendet, wenn spezifischere Rollen nicht passen.

Siehe bevorzugt: os-updates Rolle


Best Practices

1. ClamAV-Server-Dimensionierung

Empfohlene Hardware:

  • CPU: 2+ Cores
  • RAM: 4 GB (2 GB für ClamAV, 2 GB für System)
  • Disk: 20 GB (10 GB für Signaturen und Logs)
  • Network: Gbit-Ethernet empfohlen

2. Chrony-Konfiguration

Verwenden Sie mehrere NTP-Server für Redundanz:

ntp_servers:
  - 0.de.pool.ntp.org
  - 1.de.pool.ntp.org
  - ptbtime1.ptb.de  # Deutsche Physikalisch-Technische Bundesanstalt
  - ptbtime2.ptb.de

3. Docker-Mirror nutzen

Für schnellere Image-Downloads in internen Netzwerken:

use_docker_image_mirror: true
docker_mirror_location: "tinc"

4. Basis-Tools konsistent installieren

Installieren Sie Basis-Tools auf allen Hosts:

ansible all -i inventories/icp-fra-pve1.yml \
  -m include_role \
  -a "name=system tasks_from=install-basic-tools" \
  -b

5. Regelmäßige ClamAV-Signatur-Updates

FreshClam aktualisiert automatisch. Überprüfen Sie regelmäßig:

# Auf ClamAV-Server
freshclam --version
journalctl -u clamav-freshclam -n 50

Fehlerbehebung

Problem: ClamAV-Kompilation schlägt fehl

Symptome: Build-Fehler während compile-clamav.yml

Lösung:

  1. Überprüfen Sie Rust-Installation:
    rustc --version
    cargo --version
    
  2. Installieren Sie fehlende Dependencies manuell
  3. Überprüfen Sie Internet-Verbindung für Source-Download
  4. Erhöhen Sie RAM (ClamAV-Build benötigt ~2 GB)

Problem: Chrony synchronisiert nicht

Symptome: Uhrzeit ist falsch oder driftet

Lösung:

  1. Überprüfen Sie Chrony-Status:
    chronyc tracking
    chronyc sources -v
    
  2. Testen Sie NTP-Server-Erreichbarkeit:
    chronyc -n sources
    ping 0.debian.pool.ntp.org
    
  3. Restart Chrony:
    systemctl restart chronyd
    

Problem: Docker-Mirror funktioniert nicht

Symptome: Image-Pulls schlagen fehl oder sind langsam

Lösung:

  1. Testen Sie Proxy-Erreichbarkeit:
    curl -I http://dim.servercow.com:3128
    
  2. Überprüfen Sie Docker-Daemon-Config:
    cat /etc/systemd/system/docker.service.d/http-proxy.conf
    
  3. Restart Docker:
    systemctl restart docker
    

Problem: SSH-Härtung sperrt aus

Symptome: Kein SSH-Zugriff nach Härtung

Prävention:

  • Testen Sie zuerst auf einem Non-Production-Host
  • Halten Sie Console-Zugriff bereit (Proxmox, IPMI)
  • Behalten Sie eine offene SSH-Session während Änderungen

Lösung:

  1. Zugriff über Console
  2. Revert sshd_config:
    cp /etc/ssh/sshd_config.backup /etc/ssh/sshd_config
    systemctl restart sshd
    

Nützliche Kommandos

ClamAV-Operationen

# Signatur-Version prüfen
clamdscan --version

# Manueller Scan
clamdscan /path/to/directory

# Signatur-Update manuell
freshclam

# ClamAV-Status
systemctl status clamav-daemon
journalctl -u clamav-daemon -f

Chrony-Operationen

# Zeitquellen anzeigen
chronyc sources -v

# Synchronisations-Status
chronyc tracking

# Force-Sync (bei großer Drift)
chronyc makestep

# Statistiken
chronyc sourcestats -v

System-Tools

# Installierte Basis-Tools prüfen
dpkg -l | grep -E 'git|curl|wget|vim|htop'

# Docker-Version
docker --version
docker compose version

# Verfügbarer Speicher
df -h

Navigation: ← Zurück: Virtualisierung | Nächstes: Cleanup-Aufgaben →