postfix

How To Set Up a Postfix E-Mail Server with Dovecot on Ubuntu 24.04 or Newer

Are you looking to host your own email server? Setting up a secure and reliable mail server can be a rewarding project, giving you full control over your email communications. In this guide, we’ll walk you through installing and configuring Postfix as your Mail Transfer Agent (MTA) and Dovecot as your Mail Delivery Agent (MDA) on Ubuntu 24.04 or newer.


Prerequisites

  • A server running Ubuntu 24.04 or newer
  • Root or sudo privileges
  • A registered domain name (e.g., example.com)
  • Basic understanding of Linux command line

Step 1: Update Your System

Before starting, ensure your system is up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Necessary Packages

Install Postfix, Dovecot, and supporting packages:

sudo apt install -y postfix dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd

During installation, you’ll be prompted to choose the mail server configuration type. Select “Internet Site” and enter your domain name when asked.


Step 3: Configure Postfix

3.1 Set the hostname

Ensure your server’s hostname matches your domain:

sudo hostnamectl set-hostname mail.example.com

Update /etc/hosts accordingly:

127.0.0.1   localhost
<your-server-ip> mail.example.com mail

3.2 Configure main Postfix settings

Open /etc/postfix/main.cf:

sudo nano /etc/postfix/main.cf

Add or modify the following settings:

myhostname = mail.example.com
mydomain = example.com
myorigin = /etc/mailname
inet_interfaces = all
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost =
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

Replace the SSL certificate paths with your own SSL certificates for production.


Step 4: Configure Dovecot

4.1 Enable Maildir format

Open /etc/dovecot/conf.d/10-mail.conf:

sudo nano /etc/dovecot/conf.d/10-mail.conf

Set mail location:

mail_location = maildir:~/Maildir

4.2 Enable authentication

Open /etc/dovecot/conf.d/10-auth.conf:

sudo nano /etc/dovecot/conf.d/10-auth.conf

Ensure these lines are uncommented:

disable_plaintext_auth = no
auth_mechanisms = plain login

4.3 Enable protocols

Open /etc/dovecot/conf.d/20-imap.conf and /etc/dovecot/conf.d/20-pop3.conf and verify:

protocols = imap pop3

Step 5: Create SSL Certificates

For secure communication, generate SSL certificates:

sudo mkdir -p /etc/ssl/private
sudo openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
  -subj "/C=US/ST=State/L=City/O=Organization/CN=mail.example.com" \
  -keyout /etc/ssl/private/ssl-cert-snakeoil.key \
  -out /etc/ssl/certs/ssl-cert-snakeoil.pem

Replace the paths with your actual SSL certificates in a production environment.


Step 6: Create Mail Users

Create system users for email accounts:

sudo adduser username

Ensure each user has a Maildir:

sudo mkdir -p /home/username/Maildir
sudo chown -R username:username /home/username/Maildir

Step 7: Restart Services

Apply your configurations:

sudo systemctl restart postfix
sudo systemctl restart dovecot

Step 8: Test Your Mail Server

Use telnet or an email client to connect to your server:

  • SMTP (sending mail): telnet mail.example.com 587
  • IMAP (receiving mail): telnet mail.example.com 993

Check for proper responses and ensure your firewall allows traffic on ports 25, 587, 993, and 110.


Additional Security & Tips

  • Firewall Configuration: Use ufw to allow necessary ports.
  • SPF, DKIM, DMARC: Implement these DNS records to improve email deliverability.
  • Backup: Regularly back up your configurations and data.

Final Thoughts

Hosting your own email server with Postfix and Dovecot on Ubuntu 24.04 or newer provides greater control and privacy. While initial setup requires attention to detail, maintaining a secure and well-configured mail server is achievable with ongoing management.

For more detailed guides or troubleshooting, check out our resources at Greenhost.cloud.


Disclaimer: Running your own mail server can be complex and may require ongoing maintenance and monitoring. Ensure you comply with your ISP and hosting provider policies regarding mail server hosting.