Server
Difficulty: Advanced
3 min read

Postfix: Setting Up a Mail Server

Detailed tutorial to install and configure Postfix, an open-source Mail Transfer Agent (MTA), on Linux.

Back to tutorials
What is Postfix?
Postfix is an open-source Mail Transfer Agent (MTA). It is the software that runs on a server to send and receive emails using the SMTP protocol. It is known for its security, its speed and its ease of configuration compared to older alternatives such as Sendmail.

Why run your own mail server?

  • Full control: You own your data, your emails are not stored with a third party.
  • Unlimited addresses: Create as many addresses, aliases and forwards as you want for your domain.
  • Learning: It is an excellent project for understanding how the Internet works.

Warning: Running a mail server comes with significant responsibilities, particularly regarding security (fighting spam) and deliverability (not being flagged as a spammer).

Prerequisites

  • A Linux server (Ubuntu/Debian) with a static IP address.
  • A domain name (e.g. `example.com`) whose DNS records you can modify.
  • An `A` DNS record pointing to your server's IP (e.g. `mail.example.com`).
  • An `MX` DNS record pointing to your `A` record (e.g. `MX @ mail.example.com`).

Premium Content

This advanced tutorial is reserved for premium members.

9,90€ / month
  • All advanced tutorials
  • New content every week
  • Progress tracking
  • Cancel anytime

Written by

Morgann Riu

Cybersecurity and Linux administration expert. I share my knowledge through free tutorials and training to help system administrators and developers secure their infrastructures.

Frequently asked questions

My emails leave without any error but land in spam, what should I do?
A server that sends and receives correctly only does half the job. You need to publish an SPF DNS record listing the servers allowed to send for your domain, sign messages with DKIM (a cryptographic signature added to every email), then declare a DMARC policy telling receiving servers what to do when SPF or DKIM fail. Without those three records, most large providers will file your messages as junk no matter how clean your Postfix configuration is.
Is Postfix enough to fetch mail from Thunderbird or Outlook?
No. Postfix is a transfer agent: it accepts mail over SMTP and drops it into local mailboxes, but it exposes neither IMAP nor POP3. You need a retrieval server, in practice Dovecot (the dovecot-imapd and dovecot-pop3d packages), configured for the same mailbox format as Postfix. The two services are complementary, not competing.
The logs show the mail was delivered but the IMAP mailbox stays empty, why?
It is almost always a format mismatch between the two services. Postfix writes wherever home_mailbox in main.cf points, while Dovecot reads wherever mail_location in /etc/dovecot/conf.d/10-mail.conf points. If Postfix delivers in mbox while Dovecot expects Maildir, the message genuinely exists on disk but cannot be found over IMAP. Line up home_mailbox = Maildir/ with mail_location = maildir:~/Maildir, then restart both services.
certbot certonly --standalone fails, what is going wrong?
The --standalone mode spins up its own temporary web server to answer the challenge, so it needs port 80 free and reachable from the Internet. Stop whatever web server is holding that port for the duration of the request, and open it on the firewall. Also make sure the name passed to -d (for example mail.example.com) publicly resolves to this server's IP, otherwise validation cannot complete.
How do I confirm an email actually went out and trace its path?
Install the mailutils package and send a test message from the command line, for example echo "body" | mail -s "Subject" [email protected] to an external mailbox. Then follow /var/log/mail.log: every step is recorded there, from Postfix accepting the message to the delivery attempt against the remote server and its return code. That file is where you will see a rejection, a deferral or a TLS error.

Share this tutorial

Did you enjoy this article?

Comments

Checklist Sécurité Linux

30 points essentiels pour sécuriser un serveur Linux. Recevez aussi les nouveaux tutoriels par email.

Pas de spam. Désabonnement en 1 clic.