متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 80Postfix · Network

Install and set up Postfix (send mail from servers)

Servers need to send mail: cron results, alerts, backup reports, app emails (password resets, orders). Postfix is the standard mail server on Linux. The most common and safest setup is a send-only relay: Postfix accepts mail only from the server itself and passes it to a real mail service (Microsoft 365, Google Workspace, Amazon SES, SendGrid) over TLS with a login. This guide sets that up on RHEL and Ubuntu, adds sender rewriting, tests delivery, and explains the queue and the common errors.

  • 10 الدروس
  • 12 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

80.1

Know it before you install

Programs hand mail to Postfix (sendmail command or SMTP on localhost). Postfix puts it in a queue and delivers it — here to a relayhost on port 587 with STARTTLS and SASL login. If delivery fails, mail stays in the deferred queue and Postfix retries.

ItemValue
Packagepostfix (+ cyrus-sasl-plain on RHEL; a mail client: s-nail / mailutils)
Servicepostfix.service
Main config/etc/postfix/main.cf (postconf shows and changes it)
Lookup tablesfor example /etc/postfix/sasl_passwd → postmap builds the database
Logsjournalctl -u postfix, /var/log/maillog (RHEL), /var/log/mail.log (Ubuntu)
Ports25 (server-to-server), 587 (submission with login)

ملاحظة

Most cloud providers block outgoing port 25. A relay on port 587 through your mail provider avoids that and gives you good delivery (SPF/DKIM done by the provider).

80.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install postfix cyrus-sasl-plain s-nail
systemctl enable --now postfix

80.3

Install on Ubuntu and Debian

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y postfix libsasl2-modules mailutils
systemctl enable --now postfix

(Without the variable, the installer asks a question — choose Satellite system or Internet Site; we set everything below anyway.)

الأوامر في هذا الدرس

80.4

First configuration: a send-only relay

Set the main options with postconf -e (it edits main.cf safely):

postconf -e 'myhostname = web1.corp.example.com'
postconf -e 'myorigin = corp.example.com'
postconf -e 'inet_interfaces = loopback-only'
postconf -e 'inet_protocols = ipv4'
postconf -e 'mydestination ='
postconf -e 'relayhost = [smtp.office365.com]:587'
postconf -e 'smtp_sasl_auth_enable = yes'
postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'
postconf -e 'smtp_sasl_security_options = noanonymous'
postconf -e 'smtp_tls_security_level = encrypt'
postconf -e 'smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt'

(Ubuntu CA file: /etc/ssl/certs/ca-certificates.crt. On RHEL 10, use lmdb: instead of hash: for tables.)

SettingWhy
inet_interfaces = loopback-onlyPostfix accepts mail only from this server — it can not become an open relay.
mydestination = (empty)This server receives no mail for any domain; everything goes to the relay.
relayhost = [host]:587Send all mail through your provider (brackets = no MX lookup).
smtp_sasl_*Log in to the provider.
smtp_tls_security_level = encryptNever send the password or mail without TLS.

The login for the relay (a dedicated mailbox or SMTP credentials):

printf '[smtp.office365.com]:587 alerts@corp.example.com:APP_PASSWORD_HERE\n' > /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd.db

Many providers only accept mail from the logged-in address. Rewrite every sender to it:

/etc/postfix/sender_canonical
/.+/    alerts@corp.example.com
postconf -e 'sender_canonical_maps = regexp:/etc/postfix/sender_canonical'
postconf -e 'smtp_header_checks = regexp:/etc/postfix/header_checks'
echo '/^From:.*/ REPLACE From: Server alerts <alerts@corp.example.com>' > /etc/postfix/header_checks
postfix check
systemctl reload postfix

Send root's mail (cron, system alerts) to a real person or team:

echo 'root: ops-team@example.com' >> /etc/aliases
newaliases

80.5

Test delivery

echo "Test from $(hostname) at $(date)" | mail -s "Postfix relay test" ops-team@example.com
journalctl -u postfix -n 20 --output cat
postfix/smtp[2311]: 4F1A2C0B2E: to=<ops-team@example.com>, relay=smtp.office365.com[52.97.x.x]:587, delay=1.2, dsn=2.0.0, status=sent (250 2.0.0 OK …)

status=sent with dsn=2.0.0 = delivered to the relay. status=deferred = it will retry — read the reason in the same line.

80.6

Daily use: the queue and the real uses

postqueue -p
mailq
postqueue -f
postcat -q 4F1A2C0B2E | head -30
postsuper -d 4F1A2C0B2E
postsuper -d ALL deferred
postconf -n
grep 'status=' /var/log/maillog | awk '{print $NF}' | sort | uniq -c

Real uses at work:

  • Alerts and reports from cron, backups, monitoring and security tools.
  • App mail (password reset, orders) through a reliable provider.
  • A central relay host for many servers (they relay to it; it relays to the provider) — then inet_interfaces and mynetworks include the server network.

80.7

Make it safe

Security checklist:

  • Send-only: loopback-only, empty mydestination — never an open relay.
  • TLS required to the relay (encrypt); credentials file mode 600.
  • A dedicated sender account with an app password / SMTP credential, not a person's mailbox.
  • If you send directly to the internet (not recommended for servers): SPF, DKIM (OpenDKIM), DMARC and a correct reverse DNS.
  • Watch the queue (alert when deferred grows) and the logs for authentication errors.
  • Keep Postfix updated.

80.8

Update and remove

dnf -y upgrade postfix
apt-get install --only-upgrade -y postfix
postfix check && systemctl restart postfix
dnf -y remove postfix

80.9

Fix common problems

ProblemCauseFix
SASL authentication failed / 535 5.7.3 Authentication unsuccessfulWrong login, app password, or SMTP AUTH turned off for the mailbox.Check sasl_passwd + postmap; enable SMTP AUTH for that mailbox at the provider.
Must issue a STARTTLS command firstTLS not used.smtp_tls_security_level = encrypt.
no mechanism availableSASL plugins missing.Install cyrus-sasl-plain (RHEL) / libsasl2-modules (Ubuntu).
Client does not have permissions to send as this senderThe From address is not the login address.Use sender_canonical_maps (and header rewrite) as above.
Mail stays in deferred: Connection timed out to port 25Outgoing 25 is blocked by the cloud.Use a relayhost on 587.
Relay access deniedAnother host tries to send through this server.Normal for a send-only server; if it should relay, add its network to mynetworks and listen on that interface.
fatal: open database /etc/postfix/sasl_passwd.dbpostmap not run (or hash not supported).Run postmap; on RHEL 10 use lmdb:.

80.10

Next steps

1Why is inet_interfaces = loopback-only important for a server that only sends mail?

Postfix then accepts mail only from the server itself, so it can not be abused as an open relay.

2A mail shows status=deferred … Connection timed out to port 25. What is the usual fix in the cloud?

Send through a relayhost on port 587 with login, because the cloud blocks outgoing port 25.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل