Skip to main content

Netzwerk- & Firewall-Konfiguration (inkl. Script)

Härtung des KI-Servers vitruvian@pop-os auf Netzwerkebene: SSH absichern, UFW mit Whitelist, sinnvolle Trennung zwischen LAN-internen und öffentlich erreichbaren Services.


1. Zielsetzung

    SSH nur auf nicht-Standard-Port und ohne Passwort-Login UFW als einfache Firewall mit Whitelist-Ansatz KI-Services nur im LAN (192.168.0.0/24) erreichbar, nicht öffentlich Öffentlich nur das, was öffentlich sein muss (NPM 80/443, VPN 1194/udp) Fail2ban gegen Brute-Force auf SSH

    2. Netzwerk-Überblick

    Element Wert LAN-Subnet 192.168.0.0/24 Pop!_OS-Server 192.168.0.16 Synology NAS 192.168.0.82 Router/Gateway 192.168.0.1 SSH-Port (Pop!_OS) 1111 (statt 22) SSH-Port (Synology) 1111

    3. SSH-Härtung

    3.1 SSH-Key statt Passwort

    # Auf dem Client (Laptop)
    ssh-keygen -t ed25519 -C "vitruvian@pop-os"
    # Default-Pfad ~/.ssh/id_ed25519 akzeptieren, Passphrase empfohlen
    
    # Public Key auf den Server kopieren (initial noch mit Passwort)
    ssh-copy-id -p 22 vitruvian@192.168.0.16

    3.2 sshd_config anpassen

    sudo nano /etc/ssh/sshd_config

    Folgende Werte setzen/anpassen:

    Port 1111
    PermitRootLogin no
    PasswordAuthentication no
    PubkeyAuthentication yes
    ChallengeResponseAuthentication no
    UsePAM yes
    X11Forwarding no
    MaxAuthTries 3
    ClientAliveInterval 300
    ClientAliveCountMax 2
    AllowUsers vitruvian

    Konfiguration testen und Dienst neu starten:

    sudo sshd -t
    sudo systemctl restart ssh
    
    # Vom Client testen, BEVOR die alte SSH-Session geschlossen wird:
    ssh -p 1111 vitruvian@192.168.0.16

    Wichtig: Die bestehende SSH-Session offen lassen, bis der Test mit dem neuen Port funktioniert – sonst ist der Server bei Fehlkonfiguration aussperrt.

    4. UFW-Firewall

    4.1 Grundkonfiguration

    # UFW installieren (meist schon vorhanden)
    sudo apt install ufw -y
    
    # Default-Policy: alles ablehnen, ausgehend erlauben
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    
    # SSH zuerst! Sonst sperrt man sich aus
    sudo ufw allow 1111/tcp comment 'SSH'
    
    # Aktivieren
    sudo ufw enable
    
    # Status
    sudo ufw status verbose

    4.2 Regelwerk für KI-Server

    # Öffentliche Services (alle IPs)
    sudo ufw allow 80/tcp   comment 'HTTP NPM'
    sudo ufw allow 443/tcp  comment 'HTTPS NPM'
    sudo ufw allow 1194/udp comment 'OpenVPN'
    
    # DNS für Pi-hole (alle IPs im LAN brauchen Zugriff)
    sudo ufw allow from 192.168.0.0/24 to any port 53 comment 'DNS Pi-hole'
    
    # KI-Services nur LAN-intern
    sudo ufw allow from 192.168.0.0/24 to any port 8000 comment 'Ollama LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 8090 comment 'KI-CRM LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 3000 comment 'OpenWebUI LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 88 comment 'Dashy LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 180 comment 'Portainer LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 380 comment 'Nextcloud LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 1000 comment 'Flowise LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 1380 comment 'n8n LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 2380 comment 'Docmost LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 2480 comment 'BookStack LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 8053 comment 'Pi-hole Admin LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 8222 comment 'Vaultwarden LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 3001 comment 'Uptime Kuma LAN'
    sudo ufw allow from 192.168.0.0/24 to any port 3030 comment 'Grafana LAN'
    
    # Alle Regeln anzeigen
    sudo ufw status numbered

    4.3 UFW & Docker – die Falle

    Docker umgeht UFW per Default, indem es eigene iptables-Regeln in der DOCKER-USER-Chain einträgt. Container-Ports sind dadurch auch dann erreichbar, wenn UFW sie scheinbar blockiert.

    Lösung 1 (empfohlen): Container nur an LAN-Interface binden:

    # statt:
    ports:
      - "8090:80"
    
    # besser:
    ports:
      - "192.168.0.16:8090:80"

    Lösung 2: ufw-docker nutzen, das die DOCKER-USER-Chain mit UFW koordiniert (https://github.com/chaifeng/ufw-docker).

    5. Fail2ban

    sudo apt install fail2ban -y
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    sudo nano /etc/fail2ban/jail.local

    Wichtige Einstellungen in jail.local:

    [DEFAULT]
    bantime  = 1h
    findtime = 10m
    maxretry = 5
    ignoreip = 127.0.0.1/8 192.168.0.0/24
    
    [sshd]
    enabled = true
    port    = 1111
    filter  = sshd
    logpath = /var/log/auth.log
    maxretry = 3

    Dienst aktivieren und starten:

    sudo systemctl enable --now fail2ban
    sudo fail2ban-client status
    sudo fail2ban-client status sshd

    6. Komplett-Script: network-setup.sh

    #!/bin/bash
    # network-setup.sh
    # UFW + SSH-Härtung + Fail2ban auf Pop!_OS
    
    set -e
    
    echo "===== 1. SSH-Härtung ====="
    sudo sed -i 's/^#*Port .*/Port 1111/' /etc/ssh/sshd_config
    sudo sed -i 's/^#*PermitRootLogin .*/PermitRootLogin no/' /etc/ssh/sshd_config
    sudo sed -i 's/^#*PasswordAuthentication .*/PasswordAuthentication no/' /etc/ssh/sshd_config
    sudo sed -i 's/^#*MaxAuthTries .*/MaxAuthTries 3/' /etc/ssh/sshd_config
    sudo sshd -t && sudo systemctl restart ssh
    echo "  SSH neu konfiguriert auf Port 1111"
    
    echo "===== 2. UFW Grundkonfiguration ====="
    sudo apt install -y ufw fail2ban
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    sudo ufw allow 1111/tcp comment 'SSH'
    
    echo "===== 3. Public Services ====="
    sudo ufw allow 80/tcp   comment 'HTTP NPM'
    sudo ufw allow 443/tcp  comment 'HTTPS NPM'
    sudo ufw allow 1194/udp comment 'OpenVPN'
    
    echo "===== 4. LAN-only Services ====="
    LAN="192.168.0.0/24"
    for PORT in 53 88 180 380 1000 1380 2380 2480 3000 3001 3030 8000 8053 8090 8222; do
      sudo ufw allow from $LAN to any port $PORT
    done
    
    echo "===== 5. UFW aktivieren ====="
    sudo ufw --force enable
    sudo ufw status verbose
    
    echo "===== 6. Fail2ban ====="
    sudo tee /etc/fail2ban/jail.local > /dev/null << EOF
    [DEFAULT]
    bantime  = 1h
    findtime = 10m
    maxretry = 5
    ignoreip = 127.0.0.1/8 $LAN
    
    [sshd]
    enabled = true
    port    = 1111
    filter  = sshd
    logpath = /var/log/auth.log
    maxretry = 3
    EOF
    
    sudo systemctl enable --now fail2ban
    sudo fail2ban-client status
    
    echo "===== FERTIG ====="

    7. Verifikation

    # Auf dem Server
    sudo ufw status verbose
    sudo systemctl status ssh fail2ban
    sudo fail2ban-client status sshd
    
    # Von extern (Smartphone-Hotspot)
    nmap -p 22,1111,80,443 <public-IP>
    # Erwartung: 22 closed/filtered, 1111 filtered/closed (per UFW), 80/443 open

    8. Wartung & Troubleshooting

    Symptom Ursache Lösung Aussperrt nach UFW-Aktivierung SSH-Regel nicht vor ufw enable gesetzt Lokal an Konsole anmelden, sudo ufw allow 1111/tcp Container-Port von extern erreichbar trotz UFW Docker umgeht UFW Bindung an 192.168.0.16:PORT oder ufw-docker Fail2ban bannt mich selbst Eigene IP nicht in ignoreip fail2ban-client set sshd unbanip <IP> SSH-Verbindung wirft "permission denied" Public Key noch nicht hinterlegt Per Konsole / Recovery anmelden, ~/.ssh/authorized_keys pflegen Public Services nicht erreichbar Portforwarding am Router fehlt Router-Admin: 80/443/1194 weiterleiten

    9. Checkliste

      ☐ SSH-Login per Schlüssel funktioniert auf Port 1111 ☐ PasswordAuthentication no aktiv ☐ UFW aktiv mit Whitelist-Policy ☐ KI-Services nur aus 192.168.0.0/24 erreichbar ☐ Fail2ban aktiv und überwacht sshd ☐ Externer Scan zeigt nur 80/443/1194 offen ☐ network-setup.sh archiviert & im Backup-Set

      Querverweise: OpenVPN | nächste Seite: Vaultwarden – Self-hosted Passwortmanager