Op Linux mailservers wordt meestal Exim of Postfix (MTA) in combinatie met Dovecot (MDA) gebruikt. Exim en Postfix zorgen voor het versturen van mail van de ene mailserver naar de andere. Dovecot zorgt dat mail die aan je VPS wordt geleverd, daadwerkelijk bij het juiste mailadres uit komt.
In deze tutorial laten wij zien hoe je Postfix en Dovecot installeert op een Linux-VPS met CentOS 7, Ubuntu, of Debian. In deze tutorial laten wij zien hoe je een lijst van 'virtual domains en mailboxes' gebruikt om mail te verwerken. Hiervoor maken wij gebruik van een database die beheerd wordt via MariaDB. Daarnaast laten wij zien hoe je SSL (TLS) van Let's Encrypt gebruikt om de verbinding te beveiligen en je firewall configureert voor je mailserver.
- Voor de stappen in deze handleiding heb je nodig:
- Een VPS met Ubuntu, Debian, of CentOS waarop (bij CentOS) de EPEL-repository is toegevoegd.
- Correct ingestelde reverse-DNS.
- Een domein waarvan de DNS naar je VPS verwijst, bijvoorbeeld via mail.voorbeeld.nl. In deze handleiding (onder mail) geven wij een voorbeeld hoe je de DNS van je VPS voor e-mail naar je VPS verwijst.
- Voer de stappen in dit artikel uit met sudo, of als root-user.
- MariaDB moet geïnstalleerd zijn op je VPS. Hoe je MariaDB installeert en configureert leggen we in deze handleidingen uit voor CentOS 7, CentOS 8, Ubuntu 18.04, Ubuntu 20.04 en Debian 9.
- De stappen in dit artikel zijn getest in CentOS 7 & 8, Ubuntu 20.04 en Debian 9.
Een database aanmaken
Voor de virtuele mailadressen raden wij aan een database te gebruiken. Hiermee is het eenvoudiger om domeinen en mailadressen toe te voegen dan vanuit de Postfix-configuratie zelf, bijvoorbeeld als je zelf een controlepaneel aanbiedt van waaruit mensen dit zelf kunnen doen. Je kunt dan eenvoudig gebruik maken van SQL statements.
Stap 1
Verbind met je VPS via SSH of de VPS-console in het TransIP-controlepaneel.
Stap 2
Update je VPS zodat je de meest recente software tot je beschikking hebt:
Ubuntu / Debian:
apt -y update
apt -y upgrade
CentOS:
yum -y update
Wij raden aan na een update je VPS te herstarten. Veel updates worden pas daadwerkelijk doorgevoerd na een reboot. Hierdoor komen eventuele problemen in de configuratie van je server dan sneller aan het licht en kun je eventueel nog een back-up terugplaatsen.
Stap 3
Start nu eerst een SQL-shell. Een nadere toelichting van SQL-commando's vind je in ons artikel over MariaDB beheren via command-line.
mysql -u root -p
Vervolgens genereer je de SQL-gebruiker, database en tabelen voor je mailserver. Eerst maak je de database aan met het volgende commando. Je bent vrij de naam 'mailserver' aan te passen.
CREATE DATABASE mailserver;
Maak de gebruiker aan met het commando (vervang 'user' en 'password' naar wens):
GRANT SELECT ON mailserver.* TO 'user'@'127.0.0.1' IDENTIFIED BY 'password';
Pas deze wijziging toe met het commando:
FLUSH PRIVILEGES;
Stap 6
Schakel over naar de database 'mailserver' die je zojuist hebt aangemaakt met het commando:
USE mailserver;
Stap 7
Maak vervolgens een tabel aan voor respectievelijk de virtuele domeinen, e-mailadressen (inclusief wachtwoord) en aliassen. De code hieronder kun je in zijn geheel kopiëren en plakken in Putty.
CREATE TABLE virtual_domains ( DomainId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, DomainName VARCHAR(50) NOT NULL ); CREATE TABLE virtual_mailboxes ( MailId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, DomainId INT NOT NULL, password VARCHAR(255) NOT NULL, Email VARCHAR(100) UNIQUE KEY NOT NULL, FOREIGN KEY (DomainId) REFERENCES virtual_domains(DomainId) ON DELETE CASCADE ); CREATE TABLE virtual_aliases ( AliasId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, DomainId INT NOT NULL, Source VARCHAR(100) NOT NULL, Destination VARCHAR(100) NOT NULL,FOREIGN KEY (DomainId) REFERENCES virtual_domains(DomainId) ON DELETE CASCADE
);
Stap 8
Sluit voor nu de SQL-shell met:
exit
Postfix installeren en configureren
Stap 1
Installeer Postfix met het commando:
Ubuntu/Debian:
apt -y install postfix postfix-mysql
policycoreutils-python-utils
Je krijgt na de installatie automatisch een korte installatiewizard te zien. Er wordt gevraagd om je hostname, die standaard al ingevuld wordt.
Vervolgens moet je een mail server configuration type selecteren. Kies hier voor 'internet site'.
CentOS:
yum -y install postfix postfix-mysql
policycoreutils-python-utils
Het kan zijn dat Postfix al aanwezig is op je VPS. Bij een minimal install van CentOS 7 en 8 in juni 2020 was dit het geval.
Stap 2
De configuratie van Postfix wordt aangestuurd via /etc/postfix/main.cf. Open dit bestand, bijvoorbeeld met:
nano /etc/postfix/main.cf
Stap 3
Pas de volgende waardes aan/voeg ze toe in het geopende bestand. Een deel van deze opties zijn optioneel. In de toelichting onder de code leggen wij de functie van deze opties uit en welke waardes je kunt gebruiken.
Een overzicht van alle beschikbare configuratieopties voor Postfix vind je hier.
Heb je voor de installatie van je VPS een SSH, one time password, of Cloud-Init installatie gebruikt? Stel dan voor de correcte werking van mydestination eerst je hostname in, zie deze handleidingen voor Ubuntu en CentOS.
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
smtp_bind_address = 123.123.123.123
smtp_bind_address6 = 2a01:7c8:d001:2::1
inet_interfaces = all
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost
smtpd_recipient_restrictions = permit_mynetworks
home_mailbox = Maildir/
Stap 4
De Postfix-configuratie heeft (bijna) geen default of uitgecommentarieerde code voor SSL/TLS. Scroll naar het eind van het bestand en voeg de volgende code toe/pas de bestaande waardes aan. In de toelichting leggen wij de code nader toe.
Let op: In de versie van december 2021 van Postfix is er een kopje 'TLS parameters' aanwezig. Voeg de onderstaande code daar toe / pas de bestaande code aan waar nodig.
#TLS configuration options append_dot_mydomain = no biff = no
config_directory = /etc/postfix dovecot_destination_recipient_limit = 1smtp_tls_security_level = may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scachesmtpd_tls_cert_file=/etc/letsencrypt/live/mail.example.com/fullchain.pem smtpd_tls_key_file=/etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous, noplaintext smtpd_sasl_tls_security_options = noanonymous smtpd_tls_auth_only = yes smtpd_tls_security_level = may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtpd_use_tls=yes
Stap 5
In deze stap voeg je de benodigde code toe om gebruik te maken van virtual mailboxes. Voeg aan het eind van het bestand de volgende code toe. In de toelichting leggen wij virtual mailboxes en de code nader toe.
# Virtual mail settings
virtual_transport = dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
virtual_mailbox_domains = mysql:/etc/postfix/virtual-mailbox-domains.conf
virtual_mailbox_maps = mysql:/etc/postfix/virtual-mailbox-users.conf
virtual_alias_maps = mysql:/etc/postfix/virtual-alias-maps.conf
Sla hierna de wijzigingen op en sluit het bestand (ctrl + x > y > enter).
Stap 6
In de vorige stap heb je de locatie opgegeven van de virtual_mailbox_domains-, virtual_mailbox_maps- en virtual_alias_maps-configuratiebestanden. Met de code hieronder maak je deze bestanden aan en configureer je ze.
Pas de waardes 'user', 'password' en 'mailserver' aan naar respectievelijk de naam en het wachtwoord van de database-gebruiker en de databse naam die je in stap 5 en 4 van het eerste onderdeel hebt ingesteld.
Tip: Je kunt de code bijvoorbeeld naar een tekst editor kopiëren en plakken, waar nodig de gegevens aanpassen en daarna in Putty plakken.
echo 'user = user' > /etc/postfix/virtual-mailbox-domains.conf
echo 'password = password' >> /etc/postfix/virtual-mailbox-domains.conf
echo 'hosts = 127.0.0.1' >> /etc/postfix/virtual-mailbox-domains.conf
echo 'dbname = mailserver' >> /etc/postfix/virtual-mailbox-domains.conf
echo "query = SELECT 1 FROM virtual_domains WHERE DomainName ='%s'" >> /etc/postfix/virtual-mailbox-domains.conf
echo 'user = user' > /etc/postfix/virtual-mailbox-users.conf
echo 'password = password' >> /etc/postfix/virtual-mailbox-users.conf
echo 'hosts = 127.0.0.1' >> /etc/postfix/virtual-mailbox-users.conf
echo 'dbname = mailserver' >> /etc/postfix/virtual-mailbox-users.conf
echo "query = SELECT 1 FROM virtual_mailboxes WHERE Email='%s'" >> /etc/postfix/virtual-mailbox-users.conf
echo 'user = user' > /etc/postfix/virtual-alias-maps.conf
echo 'password = password' >> /etc/postfix/virtual-alias-maps.conf
echo 'hosts = 127.0.0.1' >> /etc/postfix/virtual-alias-maps.conf
echo 'dbname = mailserver' >> /etc/postfix/virtual-alias-maps.conf
echo "query = SELECT Destination FROM virtual_aliases WHERE Source='%s'" >> /etc/postfix/virtual-alias-maps.conf
Stap 7
Pas de rechten aan op de bestanden zodat de root-user ze kan lezen en schrijven en de groep ze kan lezen:
chmod 640 /etc/postfix/virtual-mailbox-domains.conf chmod 640 /etc/postfix/virtual-mailbox-users.conf chmod 640 /etc/postfix/virtual-alias-maps.conf
Stap 8
Open /etc/postfix/master.cf:
nano /etc/postfix/master.cf
Stap 9
De configuratie in dit bestand beheerst alle processen die door Postfix worden gestart. Pas de configuratie aan, zodat het eerste deel er uit ziet als in het voorbeeld hieronder.
# # Postfix master process configuration file. For details on the format # of the file, see the master(5) manual page (command: "man 5 master" or # on-line: http://www.postfix.org/master.5.html). # # Do not forget to execute "postfix reload" after editing this file. # # ========================================================================== # service type private unpriv chroot wakeup maxproc command + args # (yes) (yes) (no) (never) (100) # ========================================================================== smtp inet n - n - - smtpd #smtp inet n - n - 1 postscreen #smtpd pass - - n - - smtpd #dnsblog unix - - n - 0 dnsblog #tlsproxy unix - - n - 0 tlsproxy submission inet n - n - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_tls_auth_only=yes -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject # -o smtpd_relay_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING #smtps inet n - n - - smtpd # -o syslog_name=postfix/smtps # -o smtpd_tls_wrappermode=yes # -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated,reject # -o milter_macro_daemon_name=ORIGINATING #628 inet n - n - - qmqpd pickup unix n - n 60 1 pickup cleanup unix n - n - 0 cleanup qmgr unix n - n 300 1 qmgr #qmgr unix n - n 300 1 oqmgr tlsmgr unix - - n 1000? 1 tlsmgr rewrite unix - - n - - trivial-rewrite bounce unix - - n - 0 bounce defer unix - - n - 0 bounce trace unix - - n - 0 bounce verify unix - - n - 1 verify flush unix n - n 1000? 0 flush proxymap unix - - n - - proxymap proxywrite unix - - n - 1 proxymap smtp unix - - n - - smtp relay unix - - n - - smtp -o syslog_name=postfix/$service_name # -o smtp_helo_timeout=5 -o smtp_connect_timeout=5 showq unix n - n - - showq error unix - - n - - error retry unix - - n - - error discard unix - - n - - discard local unix - n n - - local virtual unix - n n - - virtual lmtp unix - - n - - lmtp anvil unix - - n - 1 anvil scache unix - - n - 1 scache
Ga naar het eind van het bestand en voeg daar nog de volgende regels toe:
Ubuntu/Debian:
dovecot unix - n n - - pipe flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${recipient}
CentOS:
dovecot unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}
Sla tot slot je wijzigingen op en sluit het bestand (ctrl + x > y > enter).
Dovecot installeren en configureren
Stap 1
Installeer Dovecot met het commando:
Ubuntu/Debian:
apt -y install
dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql
CentOS:
yum -y install dovecot dovecot-mysql
Stap 2
De configuratie van Dovecot is verspreid over meerdere specifieke bestanden. Eerst pas je de algemene configuratie aan in /etc/dovecot/dovecot.conf.
nano /etc/dovecot/dovecot.conf
Stap 3
Pas de waardes van de onderstaande opties aan in het bestand zodat die overeenkomen met dit voorbeeld.
Ubuntu/Debian:
!include_try /usr/share/dovecot/protocols.d/*.protocol listen = *, :: !include conf.d/*.conf !include_try local.conf
CentOS:
protocols = imap pop3 lmtp listen = *, :: !include conf.d/*.conf !include_try local.conf
Sla de wijzigingen op en sluit tot slot het bestand (ctrl + x > y > enter).
CentOS 8/Ubuntu/Debian - Optioneel: