Hier ist ein Bash-Skript, das Postfix (SMTP-Server) zusammen mit Dovecot (IMAP-Server) auf Ubuntu 22.10 installiert und konfiguriert.
#!/bin/bash # Postfix, Dovecot IMAP und SMTP Setup Script für Ubuntu 22.10 # Farbdefinitionen für die Ausgabe RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Funktion zum Überprüfen, ob das Skript als root ausgeführt wird check_root() { if [ "$EUID" -ne 0 ]; then echo -e "${RED}Error: Dieses Skript muss als root ausgeführt werden.${NC}" exit 1 fi } # Funktion zur Installation von Paketen install_packages() { echo -e "${YELLOW}Aktualisiere Paketquellen...${NC}" apt update echo -e "${YELLOW}Installiere erforderliche Pakete...${NC}" apt install -y postfix dovecot-imapd dovecot-pop3d mailutils opendkim opendkim-tools } # Funktion zur Konfiguration von Postfix configure_postfix() { echo -e "${YELLOW}Konfiguriere Postfix...${NC}" # Hauptkonfiguration postconf -e "myhostname = $(hostname -f)" postconf -e "mydomain = $(hostname -d)" postconf -e "myorigin = \$mydomain" postconf -e "mydestination = \$myhostname, localhost.\$mydomain, localhost, \$mydomain" postconf -e "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" postconf -e "inet_interfaces = all" postconf -e "home_mailbox = Maildir/" postconf -e "mailbox_command =" postconf -e "smtpd_banner = \$myhostname ESMTP \$mail_name" postconf -e "biff = no" postconf -e "append_dot_mydomain = no" postconf -e "readme_directory = no" # TLS Konfiguration postconf -e "smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem" postconf -e "smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key" postconf -e "smtpd_use_tls=yes" postconf -e "smtpd_tls_session_cache_database = btree:\${data_directory}/smtpd_scache" postconf -e "smtp_tls_session_cache_database = btree:\${data_directory}/smtp_scache" # SASL Authentifizierung für Dovecot postconf -e "smtpd_sasl_type = dovecot" postconf -e "smtpd_sasl_path = private/auth" postconf -e "smtpd_sasl_auth_enable = yes" postconf -e "smtpd_sasl_security_options = noanonymous" postconf -e "smtpd_sasl_local_domain = \$myhostname" postconf -e "broken_sasl_auth_clients = yes" # SMTP Restrictions postconf -e "smtpd_client_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination" postconf -e "smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination" postconf -e "smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination" # Maildir statt mbox postconf -e "virtual_mailbox_domains = \$mydomain" postconf -e "virtual_mailbox_base = /var/mail/vhosts" postconf -e "virtual_mailbox_maps = hash:/etc/postfix/vmailbox" postconf -e "virtual_minimum_uid = 1000" postconf -e "virtual_uid_maps = static:5000" postconf -e "virtual_gid_maps = static:5000" postconf -e "virtual_alias_maps = hash:/etc/postfix/virtual" # Erstelle Verzeichnisstruktur mkdir -p /var/mail/vhosts/$(hostname -d) groupadd -g 5000 vmail useradd -g vmail -u 5000 vmail -d /var/mail/vhosts -m chown -R vmail:vmail /var/mail/vhosts # Erstelle vmailbox und virtual Dateien echo "postmaster@$(hostname -d) $(hostname -d)/postmaster/" > /etc/postfix/vmailbox echo "info@$(hostname -d) $(hostname -d)/info/" >> /etc/postfix/vmailbox postalias /etc/postfix/vmailbox echo "postmaster@$(hostname -d) postmaster" > /etc/postfix/virtual postalias /etc/postfix/virtual # Postfix neu laden systemctl restart postfix } # Funktion zur Konfiguration von Dovecot configure_dovecot() { echo -e "${YELLOW}Konfiguriere Dovecot...${NC}" # Hauptkonfiguration cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak cat > /etc/dovecot/dovecot.conf <<EOF # Dovecot Konfiguration listen = * protocols = imap pop3 mail_location = maildir:/var/mail/vhosts/%d/%n mail_privileged_group = mail ssl = yes ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key auth_mechanisms = plain login auth_verbose = yes service auth { unix_listener /var/spool/postfix/private/auth { mode = 0660 user = postfix group = postfix } } userdb { driver = static args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n } passdb { driver = pam } namespace inbox { inbox = yes mailbox Drafts { special_use = \Drafts } mailbox Junk { special_use = \Junk } mailbox Sent { special_use = \Sent } mailbox Trash { special_use = \Trash } } EOF # Dovecot neu starten systemctl restart dovecot } # Funktion zur Konfiguration von OpenDKIM configure_opendkim() { echo -e "${YELLOW}Konfiguriere OpenDKIM...${NC}" # Erstelle Verzeichnisstruktur mkdir -p /etc/opendkim/keys/$(hostname -d) chown -R opendkim:opendkim /etc/opendkim/keys chmod go-rw /etc/opendkim/keys # Hauptkonfiguration cat > /etc/opendkim.conf <<EOF # OpenDKIM Konfiguration Syslog yes UMask 007 Socket inet:8891@localhost PidFile /var/run/opendkim/opendkim.pid Mode sv Canonicalization relaxed/simple KeyTable /etc/opendkim/key.table SigningTable refile:/etc/opendkim/signing.table ExternalIgnoreList refile:/etc/opendkim/trusted.hosts InternalHosts refile:/etc/opendkim/trusted.hosts AutoRestart yes AutoRestartRate 10/1h Background yes DNSTimeout 5 SignatureAlgorithm rsa-sha256 EOF # Trusted hosts echo "127.0.0.1" > /etc/opendkim/trusted.hosts echo "localhost" >> /etc/opendkim/trusted.hosts echo "*@$(hostname -d)" >> /etc/opendkim/trusted.hosts # Key table echo "default._domainkey.$(hostname -d) $(hostname -d):default:/etc/opendkim/keys/$(hostname -d)/default.private" > /etc/opendkim/key.table # Signing table echo "*@$(hostname -d) default._domainkey.$(hostname -d)" > /etc/opendkim/signing.table # Erstelle DKIM Key opendkim-genkey -b 2048 -d $(hostname -d) -D /etc/opendkim/keys/$(hostname -d) -s default -v chown opendkim:opendkim /etc/opendkim/keys/$(hostname -d)/default.private chmod 600 /etc/opendkim/keys/$(hostname -d)/default.private # Postfix für DKIM konfigurieren postconf -e "milter_default_action = accept" postconf -e "milter_protocol = 2" postconf -e "smtpd_milters = inet:localhost:8891" postconf -e "non_smtpd_milters = inet:localhost:8891" # OpenDKIM neu starten systemctl restart opendkim systemctl restart postfix } # Funktion zur Anzeige der Zusammenfassung show_summary() { echo -e "${GREEN}Installation abgeschlossen!${NC}" echo -e "${YELLOW}Zusammenfassung:${NC}" echo -e " - Postfix (SMTP) wurde installiert und konfiguriert" echo -e " - Dovecot (IMAP/POP3) wurde installiert und konfiguriert" echo -e " - OpenDKIM wurde installiert und konfiguriert" echo -e " - Mail-Verzeichnis: /var/mail/vhosts" echo -e " - Vmail Benutzer: vmail (UID 5000, GID 5000)" echo -e "\n${YELLOW}DKIM DNS Eintrag:${NC}" cat /etc/opendkim/keys/$(hostname -d)/default.txt echo -e "\n${YELLOW}Testen Sie die Konfiguration mit:${NC}" echo "telnet localhost 25 (SMTP)" echo "telnet localhost 143 (IMAP)" echo "openssl s_client -connect localhost:993 -crlf (IMAPS)" } # Hauptfunktion main() { check_root install_packages configure_postfix configure_dovecot configure_opendkim show_summary } # Skript ausführen main
Anleitung zur Verwendung:
- Speichern Sie das Skript als
mailserver_setup.sh
- Machen Sie das Skript ausführbar:
chmod +x mailserver_setup.sh
- Führen Sie das Skript als root aus:
sudo ./mailserver_setup.sh
Was das Skript tut:
- Installiert die erforderlichen Pakete:
- Postfix (SMTP-Server)
- Dovecot (IMAP/POP3-Server)
- OpenDKIM (für E-Mail-Signaturen)
- Zusätzliche Tools
- Konfiguriert Postfix mit:
- SMTP-Authentifizierung
- TLS-Unterstützung
- Maildir-Format für E-Mails
- Virtuelle Mailboxen
- Konfiguriert Dovecot mit:
- IMAP und POP3 Unterstützung
- TLS-Verschlüsselung
- Authentifizierung über PAM
- Maildir-Unterstützung
- Konfiguriert OpenDKIM für:
- DomainKeys Identified Mail (DKIM)
- E-Mail-Signaturen
- Zeigt eine Zusammenfassung der Konfiguration an, einschließlich des benötigten DKIM-DNS-Eintrags.
Wichtige Hinweise:
- Dieses Skript verwendet selbstsignierte Zertifikate. Für Produktionsumgebungen sollten Sie echte Zertifikate von einer Zertifizierungsstelle verwenden.
- Sie müssen den DKIM-DNS-Eintrag in Ihrer Domain-Zone hinzufügen.
- Überprüfen Sie die Firewall-Einstellungen, um sicherzustellen, dass die Ports 25 (SMTP), 143 (IMAP), 587 (Submission) und 993 (IMAPS) zugänglich sind.
- Für eine Produktionsumgebung sollten Sie zusätzliche Sicherheitsmaßnahmen wie Fail2ban und regelmäßige Updates implementieren.