sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw enable
sudo ufw status verbose- Ubuntu 22.04 LTS oder 24.04 LTS
- Root/sudo Zugriff
- SSH-Zugang (VOR Aktivierung SSH-Regel erstellen!)
Wichtig: iptables-persistent darf NICHT installiert sein (Konflikt mit UFW):
dpkg -l | grep iptables-persistent
# Falls installiert:
sudo apt purge iptables-persistentsudo apt update
sudo apt install ufw# Eingehende Verbindungen: DENY (alles blockieren)
sudo ufw default deny incoming
# Ausgehende Verbindungen: ALLOW (alles erlauben)
sudo ufw default allow outgoing
# Routed Traffic (nur für Router/Gateways relevant)
sudo ufw default deny routedVor Aktivierung IMMER SSH-Zugang sichern!
# Option A: Rate-Limited (empfohlen)
sudo ufw limit 22/tcp comment 'SSH Rate-Limited'
# Option B: Standard Allow
sudo ufw allow 22/tcp comment 'SSH'
# Option C: Network-restricted (sicherste)
sudo ufw allow from 10.0.0.0/24 to any port 22 proto tcp comment 'SSH Management'sudo ufw enable
# Bestätigung mit 'y'# Übersicht
sudo ufw status verbose
# Mit Regel-Nummern (für Bearbeitung)
sudo ufw status numberedsudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'sudo ufw allow from 10.0.0.0/24 to any port 139,445 proto tcp comment 'Samba Management'
sudo ufw allow from 192.168.100.0/24 to any port 139,445 proto tcp comment 'Samba LAN'# Nur aus Management-Netzwerk
sudo ufw allow from 10.0.0.0/24 to any port 9090 proto tcp comment 'Prometheus'
sudo ufw allow from 10.0.0.0/24 to any port 3000 proto tcp comment 'Grafana'Drop-ins sind vorgefertigte Regel-Sets für spezifische Use Cases.
# 1. Drop-in auswählen
cat drop-ins/10-webserver.rules
# 2. Regeln anwenden
source drop-ins/10-webserver.rules
# Oder einzeln ausführen:
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'Verfügbare Drop-ins: drop-ins/README.md
Falls IPv6 nicht benötigt wird (CIS 3.1.1):
# /etc/default/ufw editieren
sudo nano /etc/default/ufw
# Ändern:
IPV6=no
# UFW neu laden
sudo ufw reloadVorteil: Weniger Regeln, kleinere Angriffsfläche.
# Logging Level setzen (empfohlen: medium)
sudo ufw logging medium
# Log-Datei prüfen
sudo tail -f /var/log/ufw.log| Level | Description |
|---|---|
| off | Kein Logging |
| low | Blocked Packets |
| medium | + Invalid + New Connections |
| high | + Rate-Limited Packets |
| full | Alles |
# 3.5.1.1 - UFW installiert
dpkg -l | grep ufw
# 3.5.1.3 - Service aktiv
systemctl is-enabled ufw
systemctl is-active ufw
# 3.5.1.7 - Default Deny
sudo ufw status verbose | grep -E "^Default:"# Alle Regeln anzeigen
sudo ufw status numbered
# Regel testen (von anderem Host)
nc -zv SERVER_IP 22
nc -zv SERVER_IP 80# Mit Port
sudo ufw allow 8080/tcp comment 'Custom Service'
# Mit Service-Name
sudo ufw allow ssh
sudo ufw allow http
# Network-restricted
sudo ufw allow from 192.168.1.0/24 to any port 3306# Nach Nummer
sudo ufw status numbered
sudo ufw delete 5
# Nach Regel
sudo ufw delete allow 8080/tcp# An Position 1 einfügen
sudo ufw insert 1 deny from 10.0.0.50 to any- TROUBLESHOOTING.md - Häufige Probleme
- DOCKER_NETWORKING.md - Docker/UFW Issues
- CIS_CONTROLS.md - Compliance-Checks
UFW läuft als systemd Service:
# Status
sudo systemctl status ufw
# Service-File prüfen
systemctl cat ufwFür automatisierte Monitoring-Scripts:
# /etc/sudoers.d/ufw-monitoring
marc ALL=(ALL) NOPASSWD: /usr/sbin/ufw status
marc ALL=(ALL) NOPASSWD: /usr/sbin/ufw status verbose
marc ALL=(ALL) NOPASSWD: /usr/sbin/ufw status numberedDetails: ../scripts/check-ufw-status.sh
# UFW Konfiguration sichern
sudo cp -r /etc/ufw /etc/ufw.backup.$(date +%Y%m%d)
sudo cp /etc/default/ufw /etc/default/ufw.backup.$(date +%Y%m%d)# Aus Backup wiederherstellen
sudo cp -r /etc/ufw.backup.YYYYMMDD/* /etc/ufw/
sudo ufw reload- CIS_CONTROLS.md - Compliance sicherstellen
- DOCKER_NETWORKING.md - Docker-Integration
- ../drop-ins/ - Passende Templates wählen
- ../examples/ - Produktionsbeispiele