Top Menu

Wednesday, February 4, 2015

Configuring Mail Server using Sendmail on RHEL 6/CentOS 6

Configuring Mail server using Sendmail on RHEL 6/CentOS 6
Email is an important part of any Web site you create. In a home environment, a free web based email service may be sufficient, but if you are running a business, then a dedicated mail server will probably be required.
The Sendmail program is a very widely used Mail Transport Agent (MTA). MTAs send mail from one machine to another. Sendmail is not a client program, which you use to read your email. Sendmail is a behind-the-scenes program which actually moves your email over networks or the Internet to where you want it to go.
In this article I will show you how to use sendmail to create a mail server that will relay your mail to a remote user's mailbox or incoming mail to a local mail box. You'll also learn later in my next article how to retrieve and send mail via your mail server using with a mail client such as Outlook or any mail client.
How Sendmail Works
Incoming Mail
Usually each user in your home has a regular Linux account on your mail server. Mail sent to each of these users (username@xyz.com) eventually arrives at your mail server and sendmail then processes it and deposits it in the mailbox file of the user’s Linux account. Mail isn’t actually sent directly to the user’s PC. Users retrieve their mail from the mail server using client software, such as Microsoft’s Outlook or Outlook Express that supports either the POP or IMAP mail retrieval protocols.
Linux users logged into the mail server can read their mail directly using a text-based client, such as mail, or a GUI client, such as Evolution. Linux workstation users can use the same programs to access their mail remotely.
Outgoing Mail
The process is different when sending mail via the mail server. PC and Linux workstation users configure their e-mail software to make the mail server their outbound SMTP mail server.
If the mail is destined for a local user in the “xyz.com” domain, then sendmail places the message in that person’s mailbox so that they can retrieve it using one of the methods above.
If the mail is being sent to another domain, sendmail first uses DNS to get the MX record for the other domain. It then attempts to relay the mail to the appropriate destination mail server using the Simple Mail Transport Protocol (SMTP). One of the main advantages of mail relaying is that when a PC user A sends mail to user B on the Internet, the PC of user A can delegate the SMTP processing to the mail server.
Step: 1) Check if sendmail is installed or not:
# rpm –qa | grep sendmail
Step: 2) If it is not installed, then install the sendmail package using following command:
# yum install sendmail sendmail-cf
Configuration
By default sendmail configuration files are located at /etc/mail.
By default sendmail listen on local interface i.e 127.0.0.1, check using this command:
# ps -ef | grep -v grep | grep -i sendmail
 root  3595 1 0 00:20 ? 00:00:00 sendmail: accepting connections
 smmsp  3604  1  0 00:20 ? 00:00:00 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
# netstat -an | grep :25 | grep tcp
   tcp     0      0 127.0.0.1:25      0.0.0.0:*       LISTEN
Step: 3) To configure the sendmail to listen to all interface at the host, just comment the following line in “sendmail.mc”
# vi /etc/mail/sendmail.mc
     From:
     DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
     To
     dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

Step: 4) Now we need to build the “sendmail.cf” file using m4 macro:
# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
Step: 5) Restart Sendmail Service:
# service sendmail restart
Step: 6) Check using this command:
# netstat -an | grep :25 | grep tcp
   tcp  0  0 0.0.0.0:25    0.0.0.0:*                  LISTEN
Step: 7) Adding your domain entry in “/etc/mail/local-host-names” files:
Add all domains for which you will accept mail; there should be one domain per line.
For example, if this mail server was to accept mail for the domains “xyz.com”, ”xyz.co.in”
# vi /etc/mail/local-host-names
     xyz.com
     xyz.co.in
Step: 8) Restart the service of sendmail and make a entry for it to start at boot time:
# service sendmail restart
# chkconfig sendmail on
Step: 9) Validation and Testing
Create two test users for testing purpose (that will send and receive mail):
   # useradd testuser1
   # useradd testuser2
Login as one of the user (say testuser1) and try to send mail using mail command:
   $ mail -s "Test mail from testuser1" testuser2
   Hello this is the test mail
   .
   EOT
Now we need to check the Maillog “/var/log/maillog” in case of any issue
   # tail /var/log/maillog
   Aug 19 01:07:58 server001 sendmail[4019]: r7IJalr6004019: from=testuser1@xyz.com, size=37, class=0, nrcpts=1, msgid=, proto=SMTP, daemon=MTA,   relay=localhost [127.0.0.1]
   Aug 19 01:07:58 server001 sendmail[4022]: r7IJalr6004019: to=testuser2@xyz.com, ctladdr=testuser1@xyz.com (502/503), delay=00:00:39, xdelay=00:00:00, mailer=local, pri=30438, dsn=2.0.0, stat=Sent
Once mail has been delivered successfully now we need to check if mail is delivered to user (testuser2) mailbox or not. We would see something like the following output:
   $ mail
   N  1 testuser1@xyz.com   Mon Aug 19 01:07  13/503   "Sendmail Test"
Step: 10) Firewall Rule; Firewall port that needs to be open for sendmail daemon machine:
   # iptables -A INPUT -p tcp -m tcp –dport 25 -j ACCEPT
Save the Iptables rules and restart it.
   #service iptables save
   #service iptables restart
Congratulation you have successfully setup MTA service using Sendmail, now is the time to configure Dovecot service to fetch mail using your favorite MUA (Mail User Agent) like MS Outlook, Thunderbird etc.