Skip to content

Postfix installieren und konfigurieren

Als erstes installieren wir die benötigten Pakete.

apt install postfix postfix-mysql dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-mysql redis-tools

Nun erstellen wir ein Verzeichnis /etc/postfix/sql wo wir die SQL Querys für Postfix speichern.

mkdir -p /etc/postfix/sql

Wir setzen uns das Datenbank Passwort als Variable $sqlpw und verwenden diese in der Konfiguration.

sqlpw='<MYSECRET>'

Nun erstellen wir uns mysql_virtual_domains_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_domains_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_catchall_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query  = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
CONF

Als nächstes erstellen wir uns mysql_virtual_mailbox_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_mailbox_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_mailbox_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
CONF

Sobald die MySQL-Konfigurationsdateien erstellt sind, aktualisieren Sie die Konfiguration von Postfix.

postconf -e "virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf"
postconf -e "virtual_alias_maps = mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf"
postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf"

Hinweis

Mit dem postconfBefehl können Sie die tatsächliche Konfiguration anzeigen, Konfigurationswerte ändern oder andere Konfigurationsinformationen zum Postfix-Mailsystem anzeigen.

Die Lieferung vor Ort erfolgt durch Dovecot. Es nimmt E-Mails von einem MTA (Postfix) entgegen und stellt sie an das Postfach eines lokalen Benutzers zu.

postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"
postconf -e "default_transport = smtp"
postconf -e "relay_transport = smtp"

Erläuterung

Die eigentliche Zustellung der virtuellen Postfächer erfolgt per LMTP an Dovecot. Dovecot speichert die Mail im Maildir

Die TLS-Parameter konfigurieren wir mithilfe des Let's encrypt SSL-Zertifikats. Der Pfad zu den Zertifikaten muss ggf. angepasst werden. In meinem Beispile setzen wir die $domain auf exaqmple.com.

domain=example.com
postconf -e 'smtpd_tls_security_level = may'
postconf -e 'smtp_tls_security_level = may'

postconf -e "smtpd_tls_auth_only = yes"
postconf -e 'smtp_tls_note_starttls_offer = yes'
postconf -e 'smtpd_tls_loglevel = 1'
postconf -e 'smtpd_tls_received_header = yes'

postconf -e "smtpd_tls_cert_file = /etc/apache2/custom.d/certs.d/${domain}/fullchain.pem"
postconf -e "smtpd_tls_key_file = /etc/apache2/custom.d/certs.d/${domain}/privkey.pem"

postconf -e "smtpd_tls_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtp_tls_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtpd_tls_mandatory_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtp_tls_mandatory_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"

Erläuterung

  • Zertifikat und Private Key werden eingebunden.
  • may erlaubt TLS, erzwingt es aber nicht.
  • Unsichere Protokolle (SSLv2/v3, TLS 1.0/1.1) werden deaktiviert.
  • smtpd_tls_auth_only = yes erlaubt SMTP-Authentifizierung nur über TLS.
  • Die Basis Identität unseres Mailservers definieren wir wie folgt.
postconf -e "myhostname = box01.${domain}"
postconf -e "myorigin = /etc/mailname"
postconf -e "mydestination = \$myhostname, box01, localhost.localdomain, localhost"
postconf -e "inet_interfaces = all"
postconf -e "inet_protocols = ipv4"

Erläuterung

  • myhostname definiert den vollständigen Hostnamen des Mailservers.
  • myorigin bestimmt die Domain, mit der lokal versendete Mails signiert werden.
  • mydestination legt fest, für welche Domains der Server selbst zuständig ist (lokale Zustellung).
  • inet_interfaces = all sorgt dafür, dass Postfix auf allen Interfaces lauscht.
  • inet_protocols = ipv4 beschränkt den Betrieb auf IPv4.

Die Netzwerk und Relay Einstellungen des Mailservers definieren wir wie folgt.

postconf -e "mynetworks_style = host"
postconf -e "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128"
postconf -e "smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination"
postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination"

Erläuterung

  • mynetworks definiert vertrauenswürdige Clients.
  • Authentifizierte Benutzer dürfen relayen.
  • Nicht autorisierte Zustellungen werden verhindert → Open Relay Schutz.

Die SASL-Authentifizierung über Dovecot definieren wir wie folgt.

postconf -e "smtpd_sasl_type = dovecot"
postconf -e "smtpd_sasl_path = private/auth"
postconf -e "smtpd_sasl_security_options = noanonymous"
postconf -e "broken_sasl_auth_clients = yes"

Erläuterung

  • Postfix nutzt Dovecot zur Authentifizierung.
  • Clients authentifizieren sich über den UNIX-Socket private/auth.
  • Anonyme Logins sind nicht erlaubt.

Die Mailgröße und Zustellung des Mailservers definieren wir wie folgt.

postconf -e "message_size_limit = 52428800"
postconf -e "mailbox_size_limit = 0"
postconf -e "recipient_delimiter = +"

Erläuterung

  • Maximale Mailgröße: 50 MB
  • Kein Limit für Mailboxgröße (Quota übernimmt Dovecot)
  • user+tag@domain wird unterstützt

Postscreen (Spam-Schutz vor SMTP) definieren wir für den Mailserver wie folgt.

postconf -e "postscreen_greet_action = enforce"
postconf -e "postscreen_dnsbl_sites = zen.spamhaus.org*2 bl.spamcop.net"
postconf -e "postscreen_dnsbl_action = enforce"
postconf -e "postscreen_access_list = permit_mynetworks, cidr:/etc/postfix/postscreen_access.cidr"

Erläuterung

  • DNSBL-Abfragen gegen Spamhaus und Spamcop
  • Ablehnung bekannter Spam-IPs bereits vor SMTP-Dialog
  • Vertrauenswürdige Netze werden ausgenommen

Nachdem wir dien Postfix Mailserver soweit konfigiriert haben, prüfen wir die Konfiguration und starten den Postfix neu.

postfix check && systemctl restart postfix

Die aktuell gesetzen Parameter können wir uns mit folgendem Befehl anzeigen lassen.

postconf -n