Baixe Configuração de Mail server e outras Exercícios em PDF para Administração e Sistemas de Rede, somente na Docsity!
Updated On September 7, 2022
Table of Contents
Lab Environment Why DNS Server is needed for SMTP Mail Server Sample DNS Forward and Reverse Zone File on DNS Server Verify DNS Server configuration Configure Postfix Mail Server (CentOS/RHEL 7/8)
Configure postfix mail server and client with examples
(CentOS/RHEL 7/8)
WRITTEN BY - ADMIN
Postfix is a Mail Transport Agent (MTA) responsible for the transfer of e-mails between
mail servers using the SMTP protocol. In this article I will share the steps to configure
postfix mail server and client using postfix SMTP relay along with some examples to
check SMTP server check configuration and connection in CentOS/RHEl 7/8 Linux.
I have two Virtual Machines on Oracle VirtualBox installed on my Linux Server. We will use
Install Postfix rpm Configure master.cf file Configure main.cf Modify inet_interfaces Modify mydomain Modify myorigin Define myhostname Modify mydestination Modify mynetworks Modify relayhost Modify home_mailbox and mail_spool_directory Configure Postfix SMTP Relay (Client) Install postfix and sendmail Configure client DNS (update /etc/resolv.conf) Verify the DNS server configuration Configure /etc/postfix/main.cf Modify inet_interfaces Modify relayhost Modify mynetworks Modify mydestination How to check SMTP server configuration in Linux Log Files for troubleshooting Postfix Mail Server related issues How Postfix (MTA) Mail Server Works? Lab Environment
If you wish to configure postfix mail server for a single node where user's can send
mail locally to each other and you can receive email alerts for system activities on
localhost then you do not need DNS Server for your SMTP Mail Server.
A user deepak on a workstation server1.example.com will not be aware of user
amit available on server2.example.com which is where DNS server comes in.
We use MX records in DNS servers as they provide mail-routing information. They
specify mail exchangers for domains that is, the names of the mail hubs that
handle all the mail for a domain name.
So we configure postfix mail server which acts as MTA, this will act as SMTP relay
host and can receive message from user deepak from server1.example.com and
transfer it to user amit on server2.example.com and vice versa.
MTAs such as Postfix need a way to determine which host or hosts are the mail
hubs for a domain. DNS MX records provide this information.
I have already written another article with detailed explanation and steps to configure
BIND DNS Server in chroot environment in CentOS/RHEL 7 and 8 Linux. Additionally here
we need to add some MX and CNAME records to our existing DNS forward and reverse
zone files to configure postfix mail server.
ALSO READ:
Setup BIND DNS Server in Rocky Linux 8 [Step-by-Step]
Below is my sample forward zone file. Here I am using a single mail server which is my
localhost i.e. centos-8.example.com. I have also defined a CNAME record so I can use a
more familiar FQDN to mail server instead of centos-8.example.com
bash Sample DNS Forward and Reverse Zone File on DNS Server
And my sample reverse zone file. Similar to my forward zone file I have defined my
CNAME and MX record address.
[root@centos-8 ~]# cat /var/named/example.com.zone $TTL 1D @ IN SOA example.com root ( 4 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum @ IN NS localhost localhost IN A 127.0.0. ; Host Address example.com. IN A 192.168.0. centos-8 IN A 192.168.0. rhel-8 IN A 192.168.0. ; Mail Server example.com. IN MX 10 192.168.0. ; CNAME mail IN CNAME centos-8.example.com.
X
Using host command we can query the mail server (MX record)
bash
Now we are done with all the pre-requisites. It is time we configure postfix mail server.
Several steps are necessary to configure the Postfix server.The basic steps involved in
this process are
Edit the master.cf file
Determine local mail delivery method.
Edit the main.cf file.
Create an aliases table.
Start and test Postfix.
Create a boot script to start Postfix.
Create any user-defined files.
We will distribute these tasks in different articles or else this will become a long boring
article. let us try to have and configure postfix mail server (basic).
Install Postfix rpm
First thing first, to configure postfix mail server we need the main ingredient which is
postfix rpm. By default postfix is installed with most of the CentOS/RHEL 7/8 software
group but if not you can install it using yum
[root@centos-8 ~]# host -t mx example.com example.com mail is handled by 10 192.168.0.10.example.com. Configure Postfix Mail Server (CentOS/RHEL 7/8)
NOTE:
On RHEL system you must have an active subscription to RHN or you can configure a
local offline repository using which "yum" package manager can install the provided
rpm and it's dependencies.
bash
Postfix RPM installation automatically perform the following actions:
Create a new system user named postfix
Create a new system group named postfix
Create the /etc/postfix and /var/spool/postfix directories
Create all Postfix message queue directories
Create a default Postfix configuration file
Create a default Postfix aliases database
ALSO READ:
How to reject mail for unknown users in postfix (local_recipient_maps)
Configure master.cf file
The Postfix master daemon launches all of the other Postfix services as they are needed.
The various services, and how they are run, are specified in the master.cf file.
To configure postfix mail server (a basic SMTP Server) we do not need to do any
[root@centos-8 ~]# yum -y install postfix
to configure postfix mail server in this article, so I will only concentrate on these
directives:
ALSO READ:
How to backup and restore entire partition and file system using fsarchiver in Linux
Modify inet_interfaces
The inet_interfaces is used to dictate on which network card the SMTP mail server will
listen. It is by default set to " localhost " so it means by default postfix mail server will
only listen to all traffic coming on loopback address. Now this does not makes any sense
if you are going to use this mail server on the domain environment. Now if you are going
to use this SMTP mail server only on local machine to deliver mail from crontab jobs to
root or specific users then that is fine but if you wish to use SMTP mail server in domain
environment then we need to change this.
We will use all, to use all the addresses that are available on our SMTP mail server
machine
bash
NOTE:
Now in my case I only have one interface so this is fine but if you have multiple
interface on you SMTP mail server node then you can use $myhostname. Make sure
you also define this variable in the file when you configure postfix mail server.
bash inet_interfaces = all myhostname = centos-8.example.com
Modify mydomain
Next we need to set the local domain-name of the mail server. For example, if our
mailserver's FQDN is mailserver.example.com and this mailserver is responsible for
delivering mail for the whole private example.com domain, the domain name will be
example.com
bash
NOTE:
This address will get appended to any mail message that you are sending out of the
server. If you have not defined this then the system will use myhostname instead of
domain name.
Modify myorigin
The myorigin parameter defines the format of the origin address for all messages sent
by the Postfix system. By default, the myorigin parameter will assume the value of the
myhostname parameter.
ALSO READ:
Top 15 tools to monitor disk IO performance with examples
If myorigin is set to myhostname then any message delivered will contain FQDN of the
localhost. So if a user deepak sends mail from centos-8.example.com then his address
mydomain = example.com
This allows the Postfix server to accept messages for addresses in the following formats:
bash
Messages sent to any of the four addresses are accepted and delivered to the
Webmaster user on the local Postfix server.
Modify mynetworks
The mynetworks parameter is used to control which SMTP clients Postfix will relay mail
for. By default, Postfix will relay mail from any client whose IP address matches the
settings in the mynetworks parameter.
The mynetworks parameter contains a list of IP network addresses, along with subnet
values, to specify alternative network restrictions on SMTP clients. The format of the
mynetworks parameter is
bash
where ipaddress1 and ipaddress2 represent IP address network values
Here, I have provided the value of my subnet
webmaster [email protected] [email protected] [email protected] mynetworks = ipaddress1, ipaddress2, ...
bash
This restricts clients that can use the Postfix server as an SMTP relay host.
Modify relayhost
The relayhost parameter defines Postfix SMTP relay host. There are two formats of the
relayhost parameter:
bash
The first format identifies a SMTP relay host mail server by its DNS name. Postfix
forwards all outbound mail messages to this host. The second format identifies the relay
host by its numeric IP address. You should use the second format for Postfix servers that
use dial-up connections to the relay host. Since the Postfix server is not connected to the
Internet full time to resolve the relay host DNS name, it is best to refer to it using the IP
address. This prevents problems in mail delivery due to DNS errors. In our case we use
Postfix MTA as SMTP relay host.
bash mynetworks = 192.168.0.0/24, 127.0.0.0/ relay_host = gateway.my.domain relay_host = [an.ip.add.ress] relayhost = [centos-8.example.com]
Install postfix and sendmail
We will use postfix as the main configuration file although we plan to use come client
tools to send the mail which requires sendmail rpm to be installed.
bash
Install some more tools which we will need in this article
bash
We need nslookup tool to verify our client DNS configuration which is provided by
bind-utils
telnet will be used to make sure our SMTP port 25 is reachable
mailx and sendmail will be used as client software to send mails to remote server
Configure client DNS (update /etc/resolv.conf)
Now on our primary DNS server we had already defined an A and PTR record for
rhel-8.example.com so on the client node we just need to update /etc/resolv.conf
bash
Here 192.168.0.10 is the IP Address of our DNS Server
Verify the DNS server configuration [root@rhel-8 ~]# yum -y install postfix [root@rhel-8 ~]# yum -y install bind-utils telnet mailx sendmail [root@rhel-8 ~]# cat /etc/resolv.conf
Generated by NetworkManager
search example.com nameserver 192.168.0.
We will perform few DNS lookup to make sure the DNS server is reachable
bash bash
So our A and MX record are working properly.
Configure /etc/postfix/main.cf
Now we must configure and modify certain values in our postfix main.cf to be able to
send mails using SMTP relay server
Modify inet_interfaces
Similar to our SMTP Mail Server we will modify inet_interfaces value to all
bash Modify relayhost
We will use our Postfix mail server as SMTP relay host, so we will give our postfix mail
[root@rhel-8 ~]# nslookup rhel- Server: 192.168.0. Address: 192.168.0.10# Name: rhel-8.example.com Address: 192.168.0. [root@rhel-8 ~]# host -t mx mail.example.com mail.example.com is an alias for centos-8.example.com. inet_interfaces = all
These are the directives we will modify for our client side postfix configuration to
configure postfix mail server.
NOTE:
For this article I have disabled firewalld and selinux config. You can add SMTP
rule to your firewalld once your configuration is working properly
bash
Next start your postfix service on client node
bash
You can use telnet to make sure port 25 is reachable
bash
There are different mail clients available such as mutt, mail etc. You can use SWAKS
(Swiss Army Knife SMTP) to check SMTP server configuration in Linux. Once we
firewall-cmd --permanent --add-service=smtp && firewall-cmd --reload
[root@rhel-8 ~]# systemctl start postfix [root@rhel-8 ~]# telnet localhost 25 Trying ::1... Connected to localhost. Escape character is '^]'. 220 rhel-8.example.com ESMTP Postfix QUIT 221 2.0.0 Bye Connection closed by foreign host. How to check SMTP server configuration in Linux
configure postfix mail server, the next step would be to check SMTP server configuration
and make sure the mails are sent and delivered successfully.
ALSO READ:
How to disable ICMP and ICMPv6 redirects in Linux
We install SWAKS on our RHEL 8 host which was our client. Now SWAKS is not available
on the RHEL repo so you must install EPEL repo. Next install SWAKS using yum
bash
So on our node we have below version of swaks installed to check SMTP server
configuration
bash
Now, to check SMTP server configuration using the standard SMTP mail port 25, with our
Postfix server running on the IP address 192.168.0.10, we are sending a mail remotely to a
Linux system user deepak which has a system user account on our Postfix server:
bash [root@rhel-8 ~]# yum install swaks [root@rhel-8 ~]# rpm -q swaks swaks-20181104.0-5.el8.noarch