DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Linux Postfix Mail Server

xFusionCorp Industries has planned to set up a common email server in Stork DC. After several meetings and recommendations they have decided to use postfix as their mail transfer agent and dovecot as an IMAP/POP3 server. We would like you to perform the following steps:

  1. Install and configure postfix on Stork DC mail server.
  2. Create an email account anita@stratos.xfusioncorp.com identified by 8FmzjvFU6S.
  3. Set its mail directory to /home/anita/Maildir.
  4. Install and configure dovecot on the same server.

Understanding the Components

Postfix

Postfix is a Mail Transfer Agent responsible for routing and delivering email. It handles SMTP connections, accepts incoming mail, and delivers it to local mailboxes or relays it to other servers. Postfix is known for its security, performance, and ease of administration.

Dovecot

Dovecot is an IMAP and POP3 server that allows users to access their email. It handles authentication and provides access to mailboxes stored on the server. Dovecot supports various mailbox formats, including Maildir and mbox.

Maildir Format

Maildir stores each email as a separate file in a directory structure. This format is more reliable than mbox because it avoids file locking issues and supports concurrent access. The structure consists of three subdirectories: cur for current messages, new for newly delivered messages, and tmp for temporary files during delivery.


Prerequisites

Before beginning, ensure the following are available:

  • Access to the mail server with root or sudo privileges
  • The mail server hostname and domain information
  • Network connectivity between the mail server and other infrastructure components

Server Details for This Tutorial

Detail Value
Server stmail01.stratos.xfusioncorp.com
User groot
Password Gr00T123
Email Address anita@stratos.xfusioncorp.com
Email Password 8FmzjvFU6S
Mail Directory /home/anita/Maildir

Installing Postfix

Connect to the mail server and switch to root.

# Password: Gr00T123
ssh groot@stmail01
sudo su -
Enter fullscreen mode Exit fullscreen mode

Install Postfix using the system package manager.

yum install postfix -y
Enter fullscreen mode Exit fullscreen mode

The installation includes Postfix and its dependencies. Postfix version 3.5.25 is installed on CentOS Stream 9.


Configuring Postfix

Edit the main Postfix configuration file at /etc/postfix/main.cf.

vi /etc/postfix/main.cf
Enter fullscreen mode Exit fullscreen mode

Add or modify the following parameters:

myhostname = stmail01.stratos.xfusioncorp.com
mydomain = stratos.xfusioncorp.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 172.16.238.0/24, 127.0.0.0/8
home_mailbox = Maildir/
Enter fullscreen mode Exit fullscreen mode

Parameter Explanation

Parameter Purpose
myhostname Fully qualified domain name of the mail server
mydomain Domain name used for email addresses
myorigin Domain used for outgoing mail
inet_interfaces Network interfaces Postfix listens on
mydestination Domains for which Postfix accepts mail
mynetworks Trusted networks for relaying
home_mailbox Mailbox format and location

The home_mailbox = Maildir/ setting is critical. It tells Postfix to deliver mail in Maildir format within the user's home directory.


Starting and Enabling Postfix

Start the Postfix service and enable it to start on boot.

systemctl start postfix
systemctl enable postfix
systemctl status postfix
Enter fullscreen mode Exit fullscreen mode

Verify that Postfix is running and listening on port 25.

ss -tlnp | grep :25
Enter fullscreen mode Exit fullscreen mode

Creating the Email Account

Create a system user for the email account. The username becomes the local part of the email address.

useradd anita
passwd anita
Enter fullscreen mode Exit fullscreen mode

When prompted, enter the password 8FmzjvFU6S twice.

Verify the user was created.

id anita
Enter fullscreen mode Exit fullscreen mode

Creating the Maildir Directory

Create the Maildir structure in the user's home directory.

mkdir -p /home/anita/Maildir/{cur,new,tmp}
Enter fullscreen mode Exit fullscreen mode

Set the correct ownership so the user can access the mailbox.

chown -R anita:anita /home/anita/Maildir
Enter fullscreen mode Exit fullscreen mode

Verify the directory structure.

ls -la /home/anita/Maildir/
Enter fullscreen mode Exit fullscreen mode

The output should show three subdirectories: cur, new, and tmp, all owned by anita.


Installing Dovecot

Install Dovecot using the system package manager.

yum install dovecot -y
Enter fullscreen mode Exit fullscreen mode

Dovecot version 2.3.16 is installed along with its dependencies, including clucene-core and libexttextcat.


Configuring Dovecot

Dovecot configuration is split across several files in the /etc/dovecot/ directory. Each file controls a specific aspect of the server.

Enable IMAP and POP3 Protocols

Edit /etc/dovecot/dovecot.conf.

vi /etc/dovecot/dovecot.conf
Enter fullscreen mode Exit fullscreen mode

Set the protocols line to enable both IMAP and POP3.

protocols = imap pop3
Enter fullscreen mode Exit fullscreen mode

Set the Mail Location

Edit /etc/dovecot/conf.d/10-mail.conf.

vi /etc/dovecot/conf.d/10-mail.conf
Enter fullscreen mode Exit fullscreen mode

Set the mail location to match the Postfix Maildir setting.

mail_location = maildir:~/Maildir
Enter fullscreen mode Exit fullscreen mode

This must match the home_mailbox setting in Postfix for mail delivery to work correctly.

Configure Authentication

Edit /etc/dovecot/conf.d/10-auth.conf.

vi /etc/dovecot/conf.d/10-auth.conf
Enter fullscreen mode Exit fullscreen mode

Set the authentication parameters.

disable_plaintext_auth = yes
auth_mechanisms = plain login
Enter fullscreen mode Exit fullscreen mode

Configure the Auth Service Socket

Edit /etc/dovecot/conf.d/10-master.conf.

vi /etc/dovecot/conf.d/10-master.conf
Enter fullscreen mode Exit fullscreen mode

Configure the auth service socket for Postfix integration.

service auth {
  unix_listener auth-userdb {
    mode = 0660
    user = postfix
    group = postfix
  }
}
Enter fullscreen mode Exit fullscreen mode

This allows Postfix to authenticate users through Dovecot.


Starting and Enabling Dovecot

Start the Dovecot service and enable it to start on boot.

systemctl start dovecot
systemctl enable dovecot
systemctl status dovecot
Enter fullscreen mode Exit fullscreen mode

Verify that Dovecot is running and listening on ports 110 and 143.

ss -tlnp | grep -E ":110|:143"
Enter fullscreen mode Exit fullscreen mode

Verification

Check Service Status

systemctl is-active postfix
systemctl is-enabled postfix
systemctl is-active dovecot
systemctl is-enabled dovecot
Enter fullscreen mode Exit fullscreen mode

All services should return active and enabled.

Check Listening Ports

ss -tlnp | grep -E ":25|:110|:143"
Enter fullscreen mode Exit fullscreen mode

Expected output shows Postfix on port 25 and Dovecot on ports 110 and 143.

Check User and Maildir

id anita
ls -la /home/anita/Maildir/
Enter fullscreen mode Exit fullscreen mode

The user should exist with the correct UID, and the Maildir should contain cur, new, and tmp directories.

Test SMTP

telnet localhost 25
Enter fullscreen mode Exit fullscreen mode

The connection should succeed and show the Postfix banner.

Test POP3

telnet localhost 110
user anita
pass 8FmzjvFU6S
Enter fullscreen mode Exit fullscreen mode

The authentication should succeed.


Troubleshooting

Postfix Warnings About Overriding

When configuration lines are duplicated, Postfix issues warnings. Remove duplicate entries from /etc/postfix/main.cf and restart Postfix.

Dovecot Fails to Start

Check the Dovecot logs for errors.

journalctl -u dovecot -n 50
doveadm log find
Enter fullscreen mode Exit fullscreen mode

Mail Not Delivered

Verify that the home_mailbox setting in Postfix matches the mail_location setting in Dovecot. Both should point to Maildir.

Authentication Failures

Ensure the auth service socket is configured correctly in 10-master.conf with the postfix user and group.

Port Conflicts

Ensure no other service is using ports 25, 110, or 143.

ss -tlnp | grep -E ":25|:110|:143"
Enter fullscreen mode Exit fullscreen mode

Configuration Summary

Component Configuration
Mail Server stmail01.stratos.xfusioncorp.com
Email Address anita@stratos.xfusioncorp.com
Password 8FmzjvFU6S
Mail Directory /home/anita/Maildir
Postfix Port 25
Dovecot IMAP Port 143
Dovecot POP3 Port 110
Mailbox Format Maildir

Best Practices

Use Maildir Format

Maildir is more reliable than mbox for concurrent access and avoids file locking issues.

Keep Postfix and Dovecot Settings Consistent

The mail location in Dovecot must match the home_mailbox setting in Postfix.

Enable Only Required Protocols

Enable only the protocols that are needed. If POP3 is not required, disable it.

Secure Authentication

Use TLS for authentication when possible. Configure Dovecot to use SSL/TLS certificates.

Monitor Logs

Regularly check mail logs for errors and delivery issues.

tail -f /var/log/maillog
Enter fullscreen mode Exit fullscreen mode

Regular Backups

Back up mail directories and configuration files regularly.


Conclusion

Setting up a mail server with Postfix and Dovecot provides a reliable and secure email solution for enterprise environments. Postfix handles mail transfer and delivery, while Dovecot provides IMAP and POP3 access to mailboxes. The Maildir format ensures reliable mail storage.

The key steps are installing both packages, configuring Postfix to deliver mail in Maildir format, creating user accounts with the correct mail directory structure, and configuring Dovecot to access those mailboxes. Verification of services, ports, and user accounts ensures the setup is complete and functional.

Top comments (0)