
Table of Contents
How to Configure Postfix Mail Server in Linux
Setting up a reliable mail server is essential for managing communication within your network or organization. Postfix, one of the most widely used Mail Transfer Agents (MTAs), offers powerful features and performance for Linux systems. This comprehensive guide walks you through the process of configuring a Postfix Mail Server on RHEL 7 and its derivatives like CentOS, Rocky Linux, and AlmaLinux.
1. What is a Mail Server?
A mail server (or email server) is a specialized server that handles the sending, receiving, and storing of emails. It plays a crucial role in relaying messages between client devices (like computers and smartphones) and other mail servers across the Internet. This tutorial I will guide you step-by-step on how to set up Postfix, a popular mail server on Linux.
2. Types of Mail Servers in Linux
Linux primarily supports two major mail servers:
- Sendmail
- Postfix
While both serve similar functions, Postfix has become the default in RHEL 7 and newer distributions due to its performance, security, and ease of configuration.
3. Understanding Mail Server Components: MUA, MTA, SMTP, MDA, and MRA
Before setting up Postfix, it’s important to understand how email communication works:
-
MUA (Mail User Agent): Email clients like Thunderbird or Outlook used to compose and send emails.
-
MTA (Mail Transfer Agent): Software like Postfix or Sendmail that transfers email between servers.
-
SMTP (Simple Mail Transfer Protocol): The protocol used for sending email from client to server or between MTAs. Improper SMTP configuration can lead to emails being email blocked by receiving servers.
-
MDA (Mail Delivery Agent): Handles the delivery of email to the user’s mailbox (e.g., Dovecot).
-
MRA (Mail Retrieval Agent): Applications like Fetchmail or Getmail that retrieve email from remote servers.
4. Key Components and File Paths
To configure Postfix Mail Server, you will interact with the following components and files that are important:
- Package:
postfixanddovecot - Main configuration Files:
/etc/postfix/main.cf/etc/dovecot/dovecot.conf
- Log File:
/var/log/mail.log - Mail Locations:
- User mails:
/var/spool/mail/<username> - Root user mails:
/var/spool/mail/root
- User mails:
- Daemon:
postfix - Default Port Number: 25
Related | Best CentOS and Redhat Alternatives | Open Source Linux OS
5. Step-by-Step: How to Configure Postfix Mail Server
To configure Postfix Mail Server on RHEL 7, Centos, Rocky Linux, and AlmaLinux follow these steps:
Step 1: Verify Hostname
Check the hostname of your server using the command and ensure hostname properly set:
hostname
Step 2: Install Required Packages
Install the Postfix and Dovecot packages:
yum install postfix* dovecot* -y
Step 3: Configure Postfix
Edit the Postfix main configuration file: ( You can use your preferred choice of file editors, such as ‘vi’, ‘vim’, or ‘nano’.”
vim /etc/postfix/main.cf
And add the following lines at the end:
myhostname = server.example.com mydomain = example.com myorigin = $mydomain inet_interfaces = all mydestination = $myhostname, localhost.$localdomain, localhost, $mydomain home_mailbox = Maildir/
Then save the file and exit.
Step 4: Configure Dovecot
Edit the Dovecot main configuration file:
vim /etc/dovecot/dovecot.conf
Add the following line:
protocols = imap pop3 lmtp
Then save and exit the from the file.
Step 5: Restart Services
Restart and enable the services to ensure they start on boot:
systemctl restart postfix dovecot systemctl enable postfix dovecot
Step 6: Configure Firewall
If the firewalld service is active and filtering the ports, allow the necessary ports through the firewall:
firewall-cmd --permanent --add-port=25/tcp firewall-cmd --reload
Step 7: Test Mail Delivery
Send a test email using the mail command:
mail -s "Test Mail" [email protected] Hi, this is a test email. (ctrl + d to send)
Login as the recipient user and check the mail directory:
su - username cd Maildir/new ls cat <mail_name>
6. Configuring Postfix as a Null Client
In certain setups, you may want to configure Postfix Mail Server as a null client to relay emails through another server.
Step 1: Edit the postfix main configuration File
Open the Postfix configuration file:
vim /etc/postfix/main.cf
And add the following lines:
relayhost = [mail.server.com] inet_interfaces = loopback-only mynetworks = 127.0.0.0/8 myorigin = server.example.com mydestination = local_transport = error: local delivery disabled
Then Save and exit.
Step 2: Restart Postfix
systemctl restart postfix systemctl enable postfix
Step 3: Configure Firewall
firewall-cmd --permanent --add-port=25/tcp firewall-cmd --reload
Step 4: Test Email Relay
Send a test email:
mail -s "Relay Test" [email protected] This is a test email for relay. (ctrl + d to send)
Read our exclusive EX200 RHCSA Exam Questions with Solutions: Your Path to RHEL 9 Certification article
7. Additional Commands for Managing Emails
- Send Email with Attachment:
mail -s "Hello" [email protected] < file.txt
- View Mail Queue:
mailq
- Verify Configuration File:
postfix check
- Delete Emails in Queue:
postsuper -d ALL
Other useful email commands:
-
To send a mail to the local system, no need to configure the mail server.
-
Configuration is required to send emails to remote systems.
How to send the mail to the user raju of the server9
# mail "mailto:[email protected]"
Type the message whatever you want (press Ctrl + d to exit and send the mail)
🧾What is mail command:
The mail command is a powerful utility in Unix/Linux systems that allows users to send and receive emails directly from the command line.
How to check and manage mail of a user i.e raju
Switch to the user i.e raju "su - raju" or ssh with user.
N Test mail1
N Test mail2
N Test mail3
N mnop
(there are four mails in the mail box)
& 1 # Read first mail
& q # Quit
& d # Delete
-
If the mail is new one then ‘N’ letter is appears before the mail. If it is already seen then there is no letter before the mail.
-
Press ‘q’ to quit the mail utility.
To send an email to multiple users, you can use the following command:
# mail or mutt -s "Hello" <user name1><user name2><user name3>
How to send the mail with attached file to the 2 users.
# mail -s "hello" <user name1><user name2><<File name>
How to view all the emails in your mailbox
&<type the mail number> (to read the specified mail by it's number)
& r (to send the replay mail to that user)
& p (to send the mail to the printer for printing)
& w (to write the contents of the mail into a file, ie., save the contents of the mail ina file)
& q (to quit the mail box)
& d (to delete the mail)
& d <mail number> (to delete the specified mail by it's number)
& d 1-20 (to delete the mails from 1 to 20 numbers)
# mail -s "hello" <user name>@<servername> . <domain name> (to send the mail to the remote system)
To see the emails that are currently in the queue, you can use
# mailq
Note: If the mail server is not configured or not running, then the sent mails will be in the queue.
How to to verify the postfix mail configuration file for syntax errors
# postfixcheck
Conclusion
Setting up email on a Linux server might sound technical, but once you learn how to configure Postfix Mail Server, the process becomes straightforward. With the right Postfix configuration, you can build a reliable, secure, and self-managed email system — perfect for everything from testing environments to live servers.