How to configure Postfix Mail Server in Linux

Photo of author
By Jay
— 5 min read
Photo of author
Written by
Photo of author
Verified by
Published On
— 5 min read

How to configure Postfix Mail Server in Linux

1. What is a Mail Server?

A mail server, also known as an email server, is a server responsible for managing and delivering emails across networks, especially the Internet. It plays a crucial role in receiving emails from client devices and forwarding them to other mail servers. Moreover, mail servers send emails directly to client devices such as home or office computers, as well as advanced mobile phones or smartphones. This guide will explore how to configure Postfix Mail Server on Linux systems.


2. Types of Mail Servers in Linux

Linux offers two primary types of mail servers:

  1. Sendmail
  2. Postfix

While both serve the purpose of sending and receiving emails, only one mail server can be active at a time on a system. Among these, Postfix has become the default mail server in RHEL 7 and later versions, including CentOS, Rocky Linux, and AlmaLinux.

3. Understanding MUA, MTA, SMTP, MDA, and MRA

To effectively configure Postfix Mail Server, it is essential to understand the components involved:

  • MUA (Mail User Agent): An email client like Thunderbird or Microsoft Outlook used for drafting and sending emails.
  • MTA (Mail Transfer Agent): Software like Postfix or Sendmail, which transfers emails between servers.
  • SMTP (Simple Mail Transfer Protocol): A protocol facilitating email transfer between MTAs.
  • MDA (Mail Delivery Agent): Software responsible for delivering emails to local mailboxes.
  • MRA (Mail Retrieval Agent): Applications that fetch emails from remote servers, such as fetchmail or getmail.

4. Key Components of a Mail Server

To configure Postfix Mail Server, you will interact with the following:

  • Package: postfix and dovecot
  • 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
  • Daemon: postfix
  • Port Number: 25

Read Also | Best CentOS and Redhat Alternatives | Open Source Linux OS

5. How to Configure Postfix Mail Server

To configure Postfix Mail Server on RHEL 7 and its derivatives, follow these steps:

Step 1: Verify Hostname
Check the hostname of your server using the command:

hostname

Step 2: Install Required Packages
Install the Postfix and Dovecot packages:

yum install postfix* dovecot* -y

Step 3: Configure Postfix
Edit the Postfix configuration file:

vim /etc/postfix/main.cf

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/

Save and exit the file.

Step 4: Configure Dovecot
Edit the Dovecot configuration file:

vim /etc/dovecot/dovecot.conf

Add the following line:

protocols = imap pop3 lmtp

Save and exit 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
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 Configuration File
Open the Postfix configuration file:

vim /etc/postfix/main.cf

Add the following:

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  

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 Also | EX200 RHCSA Exam Questions with Solutions: Your Path to RHEL 9 Certification

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 commands:

* To send a mail to the local system, no need to configure the mail server.

* To send a mail to the remote system, then only we have to configure the mail server.

# mail "mailto:[email protected]" (to send the mail to the raju user of the server9)

type the message whatever you want (press Ctrl + d to exit and send the mail)
 # su - raju (to switch to the raju user)
	$ mail (to check the mails of the raju user)
	N abcd
	N efgh
	N ijkl
	N mnop (there are four mails in the mail box)
	& 1 (to read the 1st mail) 

* 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.

# mail or mutt -s " hello " <user name1><user name2><user name3> type the matter whatever you want (press Ctrl + d to exit and send the mail to 3 users)
 $ mail (to see all the mail in the mail box)
	&<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)
# mailq (to see the mails in the queue) 

* If the mail server is not configured or not running, then the sent mails will be in the queue.

# mail -s "hello" <user name1><user name2><<File name> (send the mail with attached file to the 2 users)
# postfixcheck (to verify the mail configuration file for syntax errors) 

Conclusion

Configure Postfix Mail Server is essential for setting up email functionality in Linux environments. Whether you need a standard mail server or a null client, Postfix offers a reliable and flexible solution. By following this guide, you can successfully configure Postfix Mail Server on RHEL, CentOS, Rocky Linux, and AlmaLinux versions 7 and above.

Related Posts


About Author

Photo of author

Jay

I specialize in web development, hosting solutions, and technical support, offering a unique blend of expertise in crafting websites, troubleshooting complex server issues, and optimizing web performance. With a passion for empowering businesses and individuals online, I provide in-depth reviews, tech tutorials, and practical guides to simplify the digital landscape. My goal is to deliver clear, reliable, and insightful content that helps readers make informed decisions and enhance their online presence.

Leave a Comment