Postfix ist ein sicherer und leistungsfähiger Mail Transfer Agent (MTA). Er wird häufig für den E-Mail-Versand von Servern und Anwendungen eingesetzt.

Installation

# Debian/Ubuntu
apt install postfix

# RHEL/CentOS
dnf install postfix

# Konfigurationstyp bei Installation: "Internet Site"

# Starten
systemctl enable postfix
systemctl start postfix

Grundkonfiguration

main.cf

# /etc/postfix/main.cf

# Grundeinstellungen
myhostname = mail.example.de
mydomain = example.de
myorigin = $mydomain

# Netzwerk
inet_interfaces = all
inet_protocols = ipv4

# Lokale Zustellung
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

# Relay-Einstellungen
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.1.0/24
relay_domains =
relayhost =

# Mailbox
home_mailbox = Maildir/
mailbox_size_limit = 51200000

# Sicherheit
smtpd_banner = $myhostname ESMTP
biff = no
append_dot_mydomain = no

Konfiguration testen

# Syntax prüfen
postfix check

# Konfiguration anzeigen
postconf -n

# Neu laden
postfix reload

TLS-Verschlüsselung

Let's Encrypt Zertifikat

certbot certonly --standalone -d mail.example.de

TLS konfigurieren

# /etc/postfix/main.cf

# TLS für ausgehende Mails
smtp_tls_security_level = may
smtp_tls_loglevel = 1

# TLS für eingehende Mails
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.de/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.de/privkey.pem
smtpd_tls_loglevel = 1

# Moderne Protokolle
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1

SMTP-Relay

Über externen Provider

# /etc/postfix/main.cf

relayhost = [smtp.provider.de]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt

Credentials

# /etc/postfix/sasl_passwd
[smtp.provider.de]:587 username:password
# Hash erstellen
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd*
postfix reload

SMTP-Authentifizierung

SASL mit Dovecot

# /etc/postfix/main.cf

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname

smtpd_recipient_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination

master.cf für Submission

# /etc/postfix/master.cf

submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_reject_unlisted_recipient=no
  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject

Spam-Schutz

Grundlegende Einschränkungen

# /etc/postfix/main.cf

smtpd_helo_required = yes

smtpd_helo_restrictions =
    permit_mynetworks,
    reject_invalid_helo_hostname,
    reject_non_fqdn_helo_hostname

smtpd_sender_restrictions =
    permit_mynetworks,
    reject_non_fqdn_sender,
    reject_unknown_sender_domain

smtpd_recipient_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination,
    reject_invalid_hostname,
    reject_non_fqdn_hostname,
    reject_non_fqdn_sender,
    reject_non_fqdn_recipient,
    reject_unknown_sender_domain,
    reject_unknown_recipient_domain,
    reject_rbl_client zen.spamhaus.org,
    reject_rbl_client bl.spamcop.net

Greylisting

# /etc/postfix/main.cf
smtpd_recipient_restrictions =
    ...
    check_policy_service inet:127.0.0.1:10023

Alias und Weiterleitungen

/etc/aliases

# /etc/aliases
postmaster: root
root: admin@example.de
info: user1, user2
support: user1
# Alias-Datenbank aktualisieren
newaliases

Virtuelle Aliase

# /etc/postfix/main.cf
virtual_alias_maps = hash:/etc/postfix/virtual

# /etc/postfix/virtual
info@example.de     user1@example.de
sales@example.de    user2@example.de, user3@example.de
@example.de         catchall@example.de
postmap /etc/postfix/virtual
postfix reload

Queue-Management

# Queue anzeigen
mailq
postqueue -p

# Alle Mails erneut senden
postqueue -f

# Bestimmte Mail löschen
postsuper -d QUEUE_ID

# Alle Mails löschen
postsuper -d ALL

# Deferred Mails löschen
postsuper -d ALL deferred

Logging

# Mail-Logs
tail -f /var/log/mail.log

# Nur Postfix
journalctl -u postfix -f

# Mail-Statistiken
pflogsumm /var/log/mail.log

Header-Manipulation

Rewriting

# /etc/postfix/main.cf
smtp_header_checks = regexp:/etc/postfix/header_checks
# /etc/postfix/header_checks
/^Received:.*internal\.example\.de/    IGNORE
/^X-Mailer:/    IGNORE

Sender-Rewriting

# /etc/postfix/main.cf
sender_canonical_maps = hash:/etc/postfix/sender_canonical

# /etc/postfix/sender_canonical
root    noreply@example.de
www-data    noreply@example.de
postmap /etc/postfix/sender_canonical

Rate Limiting

# /etc/postfix/main.cf

# Verbindungslimits
smtpd_client_connection_rate_limit = 50
smtpd_client_message_rate_limit = 100

# Pro Empfänger
smtpd_recipient_limit = 100

# Anvil (Statistiken)
anvil_rate_time_unit = 60s

SPF, DKIM, DMARC

DNS-Einträge

; SPF
example.de.     IN TXT "v=spf1 mx ip4:1.2.3.4 -all"

; DKIM
mail._domainkey.example.de. IN TXT "v=DKIM1; k=rsa; p=PUBLIC_KEY"

; DMARC
_dmarc.example.de. IN TXT "v=DMARC1; p=quarantine; rua=mailto:dmarc@example.de"

OpenDKIM

apt install opendkim opendkim-tools

# Key generieren
opendkim-genkey -s mail -d example.de
# /etc/postfix/main.cf
milter_default_action = accept
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891

Firewall

# UFW
ufw allow 25/tcp   # SMTP
ufw allow 587/tcp  # Submission
ufw allow 465/tcp  # SMTPS

# firewalld
firewall-cmd --permanent --add-service=smtp
firewall-cmd --permanent --add-port=587/tcp
firewall-cmd --reload

Test

# Lokal testen
echo "Test" | mail -s "Test Mail" user@example.de

# SMTP-Verbindung testen
telnet mail.example.de 25
HELO test.de
MAIL FROM:<test@test.de>
RCPT TO:<user@example.de>
DATA
Subject: Test
Test Message
.
QUIT

# Mit swaks
swaks --to user@example.de --from test@example.de --server localhost

Troubleshooting

# Queue prüfen
mailq

# Logs analysieren
tail -f /var/log/mail.log | grep -E "status=|reject"

# Konfiguration testen
postfix check
postconf -n | grep -i relay

# Mail-Header analysieren
# → Received-Header von unten nach oben lesen

Zusammenfassung

| Datei | Funktion | |-------|----------| | /etc/postfix/main.cf | Hauptkonfiguration | | /etc/postfix/master.cf | Dienste | | /etc/aliases | System-Aliase | | /etc/postfix/virtual | Virtuelle Aliase | | /var/log/mail.log | Logs |

| Befehl | Funktion | |--------|----------| | postfix reload | Konfiguration neu laden | | postfix check | Syntax prüfen | | mailq | Queue anzeigen | | newaliases | Aliase aktualisieren | | postmap | Hash-Dateien erstellen |

| Port | Dienst | |------|--------| | 25 | SMTP | | 587 | Submission (mit Auth) | | 465 | SMTPS |

Fazit

Postfix ist robust und sicher für den Mail-Versand. TLS und Authentifizierung sind für moderne Mail-Server essentiell. SPF, DKIM und DMARC verbessern die Zustellbarkeit. Für einen vollständigen Mailserver wird zusätzlich Dovecot für IMAP/POP3 benötigt.