Mail Servers in Linux – Postfix
Table of Contents
- Introduction
- What is Postfix?
- Prerequisites
- Implementation
- Conclusion
1. Introduction
In Linux, a mail server allows applications, scripts, and workloads to send email notifications.
For example, when a backup job finishes running, the system can send an email to notify the administrator whether the backup completed successfully or failed.
2. What is Postfix?
Postfix is a Mail Transfer Agent (MTA) that runs on Linux to send and receive emails.
Key features:
- Security
- Easy configuration
- Excellent performance
- Active community support
3. Prerequisites
- Linux server (RHEL / AlmaLinux)
- Internet connectivity (for package installation)
- A recipient email address
4. Implementation
-
Check whether Postfix is installed
rpm -qa | grep postfix
-
Install Postfix
Install Postfix and S-nail using DNF.
dnf install -y postfix s-nail
-
postfix: Handles email delivery on the Linux server. -
s-nail: Provides the mail command to compose and send emails from the terminal.
-
-
Start and Enable Postfix
systemctl enable --now postfix
Verify the service:
systemctl status postfix
-
Explore Postfix Configuration Files
Postfix configuration files are stored in:
cd /etc/postfix/
Important configuration files:
File Description main.cfMain Postfix configuration master.cfControls Postfix services and processes Open the main configuration:
vi /etc/postfix/main.cfSMTP Relay Server:
A relay server is an SMTP server that forwards outgoing emails to their destinations.
For example, you can configure Gmail SMTP as a relay server:
relayhost = [smtp.gmail.com]:587This configuration requires additional SMTP authentication and TLS settings. (read step 5)
-
Configure Gmail SMTP Relay
To allow Postfix to send emails through Gmail, configure SMTP authentication and TLS.
-
Install SASL authentication packages
dnf install -y cyrus-sasl-plain
-
Create a Gmail App Password
- Enable 2-Step Verification on your Google account.
- Generate an App Password from your Google Account security settings.
- Use the generated App Password instead of your normal Gmail password.
vvdk fpjl fwqx xnvh -
Create SMTP authentication file
vi /etc/postfix/sasl_passwdAdd:
[smtp.gmail.com]:587 tmeytngai@gmail.com:vvdk fpjl fwqx xnvh
-
Secure and create the authentication database
chmod 600 /etc/postfix/sasl_passwd postmap /etc/postfix/sasl_passwd chmod 600 /etc/postfix/sasl_passwd.db
-
Configure Postfix Main configuration
Open the main configuration file:
vi /etc/postfix/main.cfAdd the following settings at the bottom of the file:
# Gmail SMTP Relay relayhost = [smtp.gmail.com]:587 # SMTP Authentication smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous # TLS Encryption smtp_tls_security_level = encrypt smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
View the last 11 line
cat /etc/postfix/main.cf | tail -n 11
-
Restart Postfix
systemctl restart postfix systemctl status postfix
-
Install SASL authentication packages
-
Send an Email
Use the mail command to send an email
mail -s "Linux Mail" recipient@gmail.commail: Email command.s: Specifies the email subject."Linux Mail": Email subject.recipient@gmail.com: Recipient's email address.
You can also send an email with a predefined message:
echo "Testing email from Linux server" | mail -s "Linux Mail" recipient@gmail.com
In my email inbox, I got this
-
Important Notes
SMTP Ports
Port Purpose 25 SMTP server-to-server delivery 465 SMTP over implicit TLS 587 SMTP submission with STARTTLS - Direct email delivery from Postfix to Gmail generally uses port 25.
- Using Gmail as an SMTP relay generally uses port 587.
-
Verify Email Delivery
Check postfix service status
systemctl status postfixCheck mail queue:
postqueue -pCheck Postfix logs:
journalctl -u postfixOn RHEL-based systems, you can also check:
tail -f /var/log/maillog
5. Conclusion
Postfix provides email delivery capabilities on Linux. It is useful for sending automated notifications from scripts, applications, backup jobs, and system monitoring tasks.