+++ date = '2024-11-19T16:29:01Z' draft = true title = 'How to Set up a Mail Server on Alpine Linux with Postfix and Dovecot' +++ # About mail servers Simply put, a mail server sends and receives email. When `ruby@protonmail.com` emails `klaasje@gmail.com`, Protonmail's mail server's MTA sends the email to Google's mail server's MTA. Google's MTA then passes the email on to Google's MDA, which is responsible for storing the email. MDAs also run IMAP or POP3 servers so you can read your emails and send emails with an email client. IMAP and POP3 are protocols for email retrieval over [TCP/IP](https://en.wikipedia.org/wiki/Internet_protocol_suite). For sending email, you would use [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol). Or visualise it like this: Ruby's email client via SMTP -> Protonmail's MTA -> Google's MTA -> Google's MDA -> Klaasje's email client via IMAP A mail server is software which can be run on any computer, including yours. You can host a mail server on a server you already own that is hosting other services, so long as those other services aren't using any of the [mail ports](## Unblock your ports). # Why this tutorial? There are many tutorials on the internet about how to set up a mail server. I don't claim that mine is particularly better than anyone else's; I'm mostly writing this for my own reference so that I can recreate my own setup on new machines. There are existing [mail server tutorials for Alpine Linux](https://wiki.alpinelinux.org/wiki/Hosting_services_on_Alpine#Mail), including ones for Postfix and Dovecot. This tutorial aims to be "monolithic" (i.e. covers the entire mail server setup with all its components), making it easier to follow without trying to follow different tutorials that may not be 100% compatible with each other, and to be adapted to my own specific use-cases, which may not be yours. I am hugely indebted to [LinuxBabe's mail server tutorial for Ubuntu](https://www.linuxbabe.com/mail-server/setup-basic-postfix-mail-sever-ubuntu), which is actually what I followed to set up my own server. Essentially, this tutorial could be thought of as an Alpine Linux adaptation of LinuxBabe's tutorial. If you want to set up an Ubuntu or Debian mail server, I recommend following LinuxBabe's tutorial, which is written very clearly and is easy to follow. The mail server will be composed of the following software:
Component Software
Mail transfer agent Postfix
Mail delivery agent DKIM authentication and signing
Spam filter Amavis SpamAssassin
Antivirus ClamAV
MDA server-side filtering Pigeonhole
Postfix will be our SMTP server, and Dovecot will run an IMAP server for us. Postfix and Dovecot are required for the minimum of what you'd expect from a working mail server (i.e. a user can log in through a standard SMTP/IMAP/POP3 email client, read their emails, and send emails). The rest is optional and modular, i.e. you can opt to have e.g. Pigeonhole but not Amavis. We will end up with a small-scale mail server running on Alpine Linux with one domain, and we will use Unix user accounts as mail accounts. This tutorial was written for Alpine Linux 3.20.3, though will most likely work on other versions too. This tutorial assumes no prior knowledge about mail servers. # You will need ## A server Any computer will do. This tutorial is for Alpine Linux specifically. The relevant thing to consider for your server is that port 25 (the port for sending email) is not blocked. Most VPS hosts block port 25 because spammers commonly use VPSes to send spam, so you will need to find one that doesn't block port 25. Some VPS hosts block port 25 by default, but will unblock it upon request, and re-block it if they find you are spamming. ## A domain name Self-explanatory. You need a domain name and the ability to set its DNS records. # Before you start ## Set up your DNS records Firstly, pick a domain for your mail server. If you're sending emails from `domain.com`, `mail.domain.com` is a common choice. ### MX record An MX record denotes that your domain is used to send and receive email, and tells other MTAs the domain name of your mail server. We will use `mail.domain.com` for your MX record. For instance, my MX record looks like: ```dns-zone revsuine.xyz. 14400 IN MX 0 mail.revsuine.xyz ``` ### Mail server records (A, AAAA, and/or CNAME) Now you need to set a record stating the IP address of your mail server (`mail.domain.com` above). Depending on your setup, you may want to create a CNAME record pointing to `domain.com` if the IP address of `mail.domain.com` is the same as the IP address of `domain.com`, or an A record if the IP address is not shared with another domain. I use a CNAME record because the IP addresses of `mail.revsuine.xyz` and `revsuine.xyz` are the same, so my record is: ```dns-zone mail.revsuine.xyz. 14400 IN CNAME revsuine.xyz ``` If you use an A record, your record may look something like ```dns-zone mail.domain.com. 14400 IN A ip.address.here ``` If you use IPv6, you should also add an AAAA record, e.g.: ```dns-zone mail.domain.com. 14400 IN AAAA ip:address:here:: ``` ### A note on my DNS records I use one server with one IP address for hosting several services under one apex domain. For instance, my static website is hosted at `revsuine.xyz`; my Nextcloud is hosted at `cloud.revsuine.xyz`; and my mail server is hosted at `mail.revsuine.xyz`. To handle this setup, I've created a subdomain `master.revsuine.xyz` (you can call the subdomain anything you want) with an *A record* (because your [PTR record](### PTR record) is expected to be an A record, not a CNAME record) to my server's IP address. I set my server's hostname and PTR record to `master.revsuine.xyz`. I will also refer to this `master.revsuine.xyz` subdomain further down when configuring Postfix. ### PTR record A PTR record is used for "reverse DNS", or rDNS, lookup: instead of mapping a domain to an IP address, it maps an IP address to a domain. It is not managed through your DNS manager (e.g. whomever you bought your domain name from), but through whoever gives you your IP address. If you rent a server, be it a VPS or a dedicated server, your hosting provider whom you rent from will be able to manage PTR records. You may have an option to add a PTR record in the dashboard of your hosting provider, or you may have to contact their support to get a PTR record added. For the above stated reasons, my PTR record for my single server (which hosts all my services) is `master.revsuine.xyz`. If your hostname is `mail.domain.com`, your PTR record should be `mail.domain.com`. ## Unblock your ports After unblocking ports from your internet provider (e.g. if your VPS host blocks outgoing port 25), make sure the following TCP ports are open on your firewall: | Port | Usage | | ---- | --------------------------------- | | 25 | SMTP | | 143 | IMAP | | 465 | Email message submission over TLS | | 587 | Email message submission | | 993 | IMAPS (IMAP over TLS) | # Postfix