# 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

<table border="1" cellpadding="6" cellspacing="0" id="bkmrk-element-wert-lan-sub"><tbody><tr><th>Element</th><th>Wert</th></tr><tr><td>LAN-Subnet</td><td>192.168.0.0/24</td></tr><tr><td>Pop!\_OS-Server</td><td>192.168.0.16</td></tr><tr><td>Synology NAS</td><td>192.168.0.82</td></tr><tr><td>Router/Gateway</td><td>192.168.0.1</td></tr><tr><td>SSH-Port (Pop!\_OS)</td><td>1111 (statt 22)</td></tr><tr><td>SSH-Port (Synology)</td><td>1111</td></tr></tbody></table>

## 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 &amp; 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 &amp; Troubleshooting

<table border="1" cellpadding="6" cellspacing="0" id="bkmrk-symptom-ursache-l%C3%B6su"><tbody><tr><th>Symptom</th><th>Ursache</th><th>Lösung</th></tr><tr><td>Aussperrt nach UFW-Aktivierung</td><td>SSH-Regel nicht vor `ufw enable` gesetzt</td><td>Lokal an Konsole anmelden, `sudo ufw allow 1111/tcp`</td></tr><tr><td>Container-Port von extern erreichbar trotz UFW</td><td>Docker umgeht UFW</td><td>Bindung an `192.168.0.16:PORT` oder ufw-docker</td></tr><tr><td>Fail2ban bannt mich selbst</td><td>Eigene IP nicht in `ignoreip`</td><td>`fail2ban-client set sshd unbanip <IP>`</td></tr><tr><td>SSH-Verbindung wirft "permission denied"</td><td>Public Key noch nicht hinterlegt</td><td>Per Konsole / Recovery anmelden, `~/.ssh/authorized_keys` pflegen</td></tr><tr><td>Public Services nicht erreichbar</td><td>Portforwarding am Router fehlt</td><td>Router-Admin: 80/443/1194 weiterleiten</td></tr></tbody></table>

## 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 &amp; im Backup-Set

---

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