How to Configure Sendmail in Linux: A Step-by-Step Guide

Introduction

Sendmail is one of the oldest and most powerful Mail Transfer Agents (MTAs) used in Unix and Linux systems. This guide will walk you through configuring Sendmail on a Linux server, ensuring proper email functionality, security, and deliverability.

Prerequisites

Before configuring Sendmail, ensure you have:

  • A Linux-based system (RHEL, CentOS, Ubuntu, or Debian)
  • Root or sudo privileges
  • A valid domain name
  • A public IP address (if sending emails externally)

Step 1: Install Sendmail

Most Linux distributions include Sendmail in their repositories. Install it using the package manager:

For RHEL/CentOS:

sudo yum install sendmail sendmail-cf m4 -y

For Ubuntu/Debian:

sudo apt update
sudo apt install sendmail sendmail-bin -y

Verify installation:

sendmail -d0.1 -bv root

Step 2: Configure Sendmail

Sendmail’s configuration file is /etc/mail/sendmail.mc, which is compiled into /etc/mail/sendmail.cf.

  1. Open the configuration file:
sudo nano /etc/mail/sendmail.mc
  1. Uncomment or add the following lines to allow email relaying from the localhost:
define(`SMART_HOST',`smtp.yourisp.com')dnl
DAEMON_OPTIONS(`Port=smtp, Addr=127.0.0.1, Name=MTA')dnl
FEATURE(`relay_entire_domain')dnl
  1. Save and exit the editor.

Step 3: Generate sendmail.cf

Compile the new configuration:

sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

Step 4: Configure DNS (if sending external emails)

Ensure you have proper DNS records for email delivery:

  • MX Record: Defines your mail server.
  • SPF Record: Prevents spoofing.
  • DKIM Record: Authenticates outgoing emails.
  • PTR Record: Used for reverse DNS lookup.

Example SPF record:

v=spf1 mx -all

Step 5: Restart and Enable Sendmail

Restart the Sendmail service to apply changes:

sudo systemctl restart sendmail
sudo systemctl enable sendmail

Step 6: Test Email Sending

Use the mail command to test Sendmail:

echo "Test Email" | mail -s "Sendmail Test" user@example.com

Check the mail log for errors:

sudo tail -f /var/log/maillog

Step 7: Secure Sendmail

  • Restrict Open Relay: Ensure Sendmail does not allow unauthorized relays by modifying /etc/mail/access.
  • Use Authentication: Enable SMTP AUTH using SASL.
  • Enable TLS Encryption: Configure /etc/mail/sendmail.mc to use STARTTLS.

Conclusion

You have successfully installed and configured Sendmail on your Linux server. Implementing security measures ensures safe and reliable email delivery.

Detailed sendmail Guide

Reerences

By following this guide, you can optimize your mail server configuration and improve email deliverability.

Leave a Comment