Postfix Mail Transfer Agent
Overview
- What you’ll learn: How to install, configure, and manage Postfix as a Mail Transfer Agent (MTA) on Ubuntu Server — including main.cf configuration, virtual domains, relay settings, and basic troubleshooting.
- Prerequisites: Email Protocols (SMTP, IMAP, POP3), basic Linux administration, DNS concepts
- Estimated reading time: 18 minutes
Introduction
Postfix is one of the most popular and widely deployed Mail Transfer Agents (MTAs) on the Internet. Originally written by Wietse Venema as a secure, fast alternative to Sendmail, Postfix has become the default MTA on many Linux distributions, including Ubuntu. It handles the SMTP side of email — accepting messages from clients and other servers, routing them, and delivering them to their final destination.
Postfix follows a modular architecture where different processes handle different tasks (receiving mail, delivering mail, managing queues). This design improves both security and performance, as each component runs with minimal privileges and can be independently configured.
In this lesson, you will learn how to install Postfix on Ubuntu Server, understand its configuration system, set up basic mail delivery, and troubleshoot common issues.
Installing Postfix
Postfix is available in the Ubuntu default repositories and can be installed with a single command. During installation, you will be prompted to select a configuration type. For a server that sends and receives mail directly, choose “Internet Site.”
# Install Postfix
$ sudo apt update
$ sudo apt install postfix -y
# During installation, select:
# General type of mail configuration: Internet Site
# System mail name: example.com
# Verify installation
$ sudo systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/lib/systemd/system/postfix.service; enabled)
Active: active (running)
# Check Postfix version
$ postconf mail_version
mail_version = 3.6.4
# Verify Postfix is listening on port 25
$ sudo ss -tlnp | grep :25
LISTEN 0 100 0.0.0.0:25 0.0.0.0:* users:(("master",pid=1234,fd=13))
Postfix Architecture
Postfix uses a master process (master) that supervises specialized daemon processes:
- smtpd: Receives incoming mail via SMTP from other servers and authenticated clients.
- smtp: Delivers outgoing mail to remote servers via SMTP.
- local: Delivers mail to local mailboxes.
- pickup: Picks up locally posted mail from the maildrop directory.
- qmgr: The queue manager that schedules mail for delivery.
- cleanup: Processes incoming mail before it enters the queue (header checks, rewriting).
Core Configuration: main.cf
The primary Postfix configuration file is /etc/postfix/main.cf. This file controls virtually all aspects of Postfix behavior. After installation, Ubuntu provides sensible defaults, but you will need to customize several parameters for a production environment.
# View the current non-default configuration
$ postconf -n
# Key parameters in /etc/postfix/main.cf:
# The hostname of this mail system
myhostname = mail.example.com
# The domain name used in outgoing mail
mydomain = example.com
# The domain appended to locally posted mail
myorigin = $mydomain
# Domains this server accepts mail for
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# Networks allowed to relay through this server
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
# Mailbox format — Maildir is recommended for modern setups
home_mailbox = Maildir/
# Size limit for messages (10 MB)
message_size_limit = 10240000
# The banner shown to connecting clients
smtpd_banner = $myhostname ESMTP $mail_name
# Disable the SMTP VRFY command (prevents address harvesting)
disable_vrfy_command = yes
After editing main.cf, always check for syntax errors and reload:
# Check configuration for errors
$ sudo postfix check
# Reload configuration without restarting
$ sudo systemctl reload postfix
# Or restart completely
$ sudo systemctl restart postfix
TLS Encryption and Authentication
In a production environment, all SMTP communication should be encrypted with TLS. Postfix supports both opportunistic TLS (STARTTLS on port 587) and mandatory TLS for client submissions.
# TLS configuration in /etc/postfix/main.cf
# Enable TLS for incoming connections
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_tls_security_level = may
smtpd_tls_loglevel = 1
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
# Enable TLS for outgoing connections
smtp_tls_security_level = may
smtp_tls_loglevel = 1
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# Modern TLS protocols only
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
# Enable SASL authentication for submission
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination
You must also enable the submission port (587) in /etc/postfix/master.cf:
# In /etc/postfix/master.cf, uncomment the submission line:
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
Managing the Mail Queue
Postfix maintains a queue of messages waiting for delivery. Understanding how to inspect and manage this queue is essential for troubleshooting delivery issues.
# View the mail queue
$ mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
A1B2C3D4E5 2048 Mon Mar 2 10:15:00 sender@example.com
recipient@remote.com
-- 1 Kbytes in 1 Request.
# Alternative: use postqueue
$ sudo postqueue -p
# Force immediate delivery of all queued mail
$ sudo postqueue -f
# Delete a specific message from the queue
$ sudo postsuper -d A1B2C3D4E5
# Delete ALL messages from the queue (use with caution)
$ sudo postsuper -d ALL
# View the content of a queued message
$ sudo postcat -q A1B2C3D4E5
# Check mail logs for delivery information
$ sudo tail -f /var/log/mail.log
Mar 2 10:15:01 mail postfix/smtp[5678]: A1B2C3D4E5: to=<recipient@remote.com>,
relay=mx.remote.com[198.51.100.10]:25, delay=0.5, delays=0.1/0.01/0.2/0.19,
dsn=2.0.0, status=sent (250 2.0.0 Ok: queued)
Key Takeaways
- Postfix is a modular, secure MTA that uses separate processes (smtpd, smtp, local, qmgr) for different tasks, each running with minimal privileges.
- The primary configuration file
/etc/postfix/main.cfcontrols hostname, domains, networks, TLS, and authentication; always runpostfix checkafter changes. - TLS should be configured for both inbound (smtpd_tls) and outbound (smtp_tls) connections; the submission port 587 should require encryption and authentication.
- The mail queue can be inspected with
mailq, flushed withpostqueue -f, and managed withpostsuper; delivery logs are in/var/log/mail.log. - For production use, always disable VRFY, set appropriate size limits, restrict relay access via
mynetworksand SASL authentication, and integrate with Dovecot for user authentication.
What’s Next
In the next lesson, you will learn about Dovecot, which complements Postfix by providing IMAP and POP3 access so users can retrieve their email with mail clients.
繁體中文
概述
- 學習目標:學習如何在 Ubuntu Server 上安裝、設定和管理 Postfix 作為郵件傳輸代理(MTA),包括 main.cf 設定、虛擬網域、中繼設定和基本故障排除。
- 先決條件:電子郵件協定(SMTP、IMAP、POP3)、基本 Linux 管理、DNS 概念
- 預計閱讀時間:18 分鐘
簡介
Postfix 是網際網路上最受歡迎和最廣泛部署的郵件傳輸代理(MTA)之一。最初由 Wietse Venema 編寫,作為 Sendmail 的安全、快速替代方案,Postfix 已成為許多 Linux 發行版(包括 Ubuntu)的預設 MTA。它處理電子郵件的 SMTP 端——接受來自用戶端和其他伺服器的郵件、路由郵件,並將郵件投遞到最終目的地。
Postfix 遵循模組化架構,不同的行程處理不同的任務(接收郵件、投遞郵件、管理佇列)。這種設計提高了安全性和效能,因為每個元件都以最小權限運行,並可獨立設定。
在本課程中,您將學習如何在 Ubuntu Server 上安裝 Postfix、了解其設定系統、設定基本郵件投遞,以及排除常見問題。
安裝 Postfix
Postfix 可在 Ubuntu 預設儲存庫中取得,只需一個命令即可安裝。在安裝過程中,系統會提示您選擇設定類型。對於直接發送和接收郵件的伺服器,請選擇「Internet Site」。
# 安裝 Postfix
$ sudo apt update
$ sudo apt install postfix -y
# 安裝期間選擇:
# 郵件設定的一般類型:Internet Site
# 系統郵件名稱:example.com
# 驗證安裝
$ sudo systemctl status postfix
# 檢查 Postfix 版本
$ postconf mail_version
mail_version = 3.6.4
# 驗證 Postfix 正在監聽連接埠 25
$ sudo ss -tlnp | grep :25
Postfix 架構
Postfix 使用主行程(master)來監督專門的守護行程:
- smtpd:透過 SMTP 從其他伺服器和經過身份驗證的用戶端接收傳入郵件。
- smtp:透過 SMTP 將外寄郵件投遞到遠端伺服器。
- local:將郵件投遞到本地信箱。
- pickup:從 maildrop 目錄中拾取本地張貼的郵件。
- qmgr:排程郵件投遞的佇列管理員。
- cleanup:在郵件進入佇列前處理傳入郵件(標頭檢查、重寫)。
核心設定:main.cf
Postfix 的主要設定檔是 /etc/postfix/main.cf。此檔案控制 Postfix 行為的幾乎所有方面。安裝後,Ubuntu 提供合理的預設值,但您需要為生產環境自訂多個參數。
# 檢視目前的非預設設定
$ postconf -n
# /etc/postfix/main.cf 中的關鍵參數:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
home_mailbox = Maildir/
message_size_limit = 10240000
disable_vrfy_command = yes
編輯 main.cf 後,始終檢查語法錯誤並重新載入:
# 檢查設定是否有錯誤
$ sudo postfix check
# 不重啟重新載入設定
$ sudo systemctl reload postfix
TLS 加密和身份驗證
在生產環境中,所有 SMTP 通訊都應使用 TLS 加密。Postfix 支援機會性 TLS(連接埠 587 上的 STARTTLS)和用戶端提交的強制 TLS。
# /etc/postfix/main.cf 中的 TLS 設定
# 為傳入連線啟用 TLS
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_tls_security_level = may
# 為外送連線啟用 TLS
smtp_tls_security_level = may
# 啟用 SASL 身份驗證
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
管理郵件佇列
Postfix 維護一個等待投遞的郵件佇列。了解如何檢查和管理此佇列對於排除投遞問題至關重要。
# 檢視郵件佇列
$ mailq
# 強制立即投遞所有佇列中的郵件
$ sudo postqueue -f
# 從佇列中刪除特定郵件
$ sudo postsuper -d A1B2C3D4E5
# 檢查郵件日誌中的投遞資訊
$ sudo tail -f /var/log/mail.log
重點摘要
- Postfix 是一個模組化、安全的 MTA,使用獨立的行程(smtpd、smtp、local、qmgr)處理不同任務,每個行程以最小權限運行。
- 主要設定檔
/etc/postfix/main.cf控制主機名稱、網域、網路、TLS 和身份驗證;變更後始終執行postfix check。 - 應為入站(smtpd_tls)和出站(smtp_tls)連線設定 TLS;提交連接埠 587 應要求加密和身份驗證。
- 可使用
mailq檢查郵件佇列,使用postqueue -f清除,使用postsuper管理;投遞日誌在/var/log/mail.log。 - 在生產環境中,始終停用 VRFY、設定適當的大小限制、透過
mynetworks和 SASL 身份驗證限制中繼存取,並與 Dovecot 整合進行使用者身份驗證。
下一步
在下一課中,您將學習 Dovecot,它透過提供 IMAP 和 POP3 存取來補充 Postfix,讓使用者可以使用郵件用戶端擷取電子郵件。
日本語
概要
- 学習内容:Ubuntu Server での Postfix のインストール、設定、管理方法を学びます。main.cf の設定、仮想ドメイン、リレー設定、基本的なトラブルシューティングを含みます。
- 前提条件:メールプロトコル(SMTP、IMAP、POP3)、基本的な Linux 管理、DNS の概念
- 推定読了時間:18分
はじめに
Postfix は、インターネット上で最も人気があり、広く展開されているメール転送エージェント(MTA)の一つです。元々 Wietse Venema によって Sendmail の安全で高速な代替として書かれた Postfix は、Ubuntu を含む多くの Linux ディストリビューションのデフォルト MTA になっています。メールの SMTP 側を処理し、クライアントや他のサーバーからメッセージを受け入れ、ルーティングし、最終宛先に配信します。
Postfix はモジュラーアーキテクチャに従い、異なるプロセスが異なるタスク(メールの受信、配信、キューの管理)を処理します。この設計により、各コンポーネントが最小限の権限で実行され、独立して設定できるため、セキュリティとパフォーマンスの両方が向上します。
このレッスンでは、Ubuntu Server に Postfix をインストールし、その設定システムを理解し、基本的なメール配信を設定し、一般的な問題のトラブルシューティングを行う方法を学びます。
Postfix のインストール
Postfix は Ubuntu のデフォルトリポジトリで利用可能で、1つのコマンドでインストールできます。インストール中に設定タイプの選択を求められます。メールを直接送受信するサーバーの場合、「Internet Site」を選択します。
# Postfix のインストール
$ sudo apt update
$ sudo apt install postfix -y
# インストール中の選択:
# メール設定の一般タイプ:Internet Site
# システムメール名:example.com
# インストールの確認
$ sudo systemctl status postfix
# Postfix バージョンの確認
$ postconf mail_version
mail_version = 3.6.4
# Postfix がポート 25 でリッスンしていることを確認
$ sudo ss -tlnp | grep :25
Postfix アーキテクチャ
Postfix はマスタープロセス(master)を使用して専門的なデーモンプロセスを監督します:
- smtpd:他のサーバーや認証されたクライアントから SMTP 経由で受信メールを受け取ります。
- smtp:SMTP 経由でリモートサーバーに送信メールを配信します。
- local:ローカルメールボックスにメールを配信します。
- pickup:maildrop ディレクトリからローカルに投函されたメールを拾います。
- qmgr:メール配信をスケジュールするキューマネージャ。
- cleanup:メールがキューに入る前に受信メールを処理します(ヘッダーチェック、書き換え)。
コア設定:main.cf
Postfix の主要設定ファイルは /etc/postfix/main.cf です。このファイルは Postfix の動作のほぼすべての側面を制御します。
# 現在のデフォルト以外の設定を表示
$ postconf -n
# /etc/postfix/main.cf の主要パラメータ:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
home_mailbox = Maildir/
message_size_limit = 10240000
disable_vrfy_command = yes
main.cf を編集した後は、常に構文エラーをチェックしてリロードします:
# 設定のエラーチェック
$ sudo postfix check
# 再起動せずに設定をリロード
$ sudo systemctl reload postfix
TLS 暗号化と認証
本番環境では、すべての SMTP 通信を TLS で暗号化する必要があります。Postfix は日和見的 TLS(ポート 587 での STARTTLS)とクライアント送信用の必須 TLS の両方をサポートしています。
# /etc/postfix/main.cf の TLS 設定
# 受信接続の TLS を有効化
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem
smtpd_tls_security_level = may
# 送信接続の TLS を有効化
smtp_tls_security_level = may
# SASL 認証を有効化
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
メールキューの管理
Postfix は配信待ちのメッセージのキューを維持します。このキューの検査と管理方法を理解することは、配信問題のトラブルシューティングに不可欠です。
# メールキューの表示
$ mailq
# キュー内のすべてのメールを即座に配信
$ sudo postqueue -f
# キューから特定のメッセージを削除
$ sudo postsuper -d A1B2C3D4E5
# メールログで配信情報を確認
$ sudo tail -f /var/log/mail.log
重要ポイント
- Postfix はモジュラーで安全な MTA であり、異なるタスクに個別のプロセス(smtpd、smtp、local、qmgr)を使用し、各プロセスは最小限の権限で実行されます。
- 主要設定ファイル
/etc/postfix/main.cfはホスト名、ドメイン、ネットワーク、TLS、認証を制御します。変更後は常にpostfix checkを実行してください。 - インバウンド(smtpd_tls)とアウトバウンド(smtp_tls)の両方の接続に TLS を設定する必要があります。送信ポート 587 は暗号化と認証を要求する必要があります。
- メールキューは
mailqで検査、postqueue -fでフラッシュ、postsuperで管理できます。配信ログは/var/log/mail.logにあります。 - 本番環境では、常に VRFY を無効にし、適切なサイズ制限を設定し、
mynetworksと SASL 認証でリレーアクセスを制限し、ユーザー認証のために Dovecot と統合してください。
次のステップ
次のレッスンでは、Postfix を補完する Dovecot について学びます。Dovecot は IMAP と POP3 アクセスを提供し、ユーザーがメールクライアントでメールを取得できるようにします。