هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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.
| Item | Value |
|---|---|
| Package | postfix (+ cyrus-sasl-plain on RHEL; a mail client: s-nail / mailutils) |
| Service | postfix.service |
| Main config | /etc/postfix/main.cf (postconf shows and changes it) |
| Lookup tables | for example /etc/postfix/sasl_passwd → postmap builds the database |
| Logs | journalctl -u postfix, /var/log/maillog (RHEL), /var/log/mail.log (Ubuntu) |
| Ports | 25 (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).
Install on RHEL, Rocky, Alma and Fedora
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.)
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.)
| Setting | Why |
|---|---|
inet_interfaces = loopback-only | Postfix 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]:587 | Send all mail through your provider (brackets = no MX lookup). |
smtp_sasl_* | Log in to the provider. |
smtp_tls_security_level = encrypt | Never 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.dbMany providers only accept mail from the logged-in address. Rewrite every sender to it:
/.+/ alerts@corp.example.compostconf -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 postfixSend root's mail (cron, system alerts) to a real person or team:
echo 'root: ops-team@example.com' >> /etc/aliases
newaliasesTest 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.
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 -cReal 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_interfacesandmynetworksinclude the server network.
Make it safe
Security checklist:
- Send-only:
loopback-only, emptymydestination— never an open relay. - TLS required to the relay (
encrypt); credentials file mode600. - 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
deferredgrows) and the logs for authentication errors. - Keep Postfix updated.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
SASL authentication failed / 535 5.7.3 Authentication unsuccessful | Wrong 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 first | TLS not used. | smtp_tls_security_level = encrypt. |
no mechanism available | SASL plugins missing. | Install cyrus-sasl-plain (RHEL) / libsasl2-modules (Ubuntu). |
Client does not have permissions to send as this sender | The 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 25 | Outgoing 25 is blocked by the cloud. | Use a relayhost on 587. |
Relay access denied | Another 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.db | postmap not run (or hash not supported). | Run postmap; on RHEL 10 use lmdb:. |
Next steps
- Quick how-tos: Send server mail through a relay.
- Command page: postconf.
- At work: Postfix — mail relay.
- Who sends the alerts: Alertmanager (email receivers), cron and systemd timers.
- DNS records for mail: BIND.
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.