Postfix Mail Server On Centos

This howto explains how to setup postfix with features such as tls encryption, smtp auth, content filtering, spam protection, virus protection and grey listing. This howto uses postfix, amavisd-new, spam assassin, clamav and sqlgrey, all of which are in Centos software repositories.

Applicable to Centos Versions:

  • Centos 5.x
  • Centos 6.x

Requirements

Mail servers rely on port 25 (tcp) to send and receive mail. It is also helpful to have a static IP address, however, it is not needed with todays offerings for dynamic DNS services. Some providers don’t allow port 25 but this is mainly just restricted for “residential” lines. It is always good to keep Centos updated and this howto assumes you are updated and running the latest versions for your release.

  • Port 25 inbound
  • Updated Centos system

Assumptions Made in HowTo

This howto assumes the following:

  • Domain name: example.com
  • Host name: host.example.com
  • Firewall is already configured to allow port 25:tcp
  • IPv4
  • Local user account: local-user

Doing the Work

The first steps are to just get everything installed that will be needed to complete the full howto. We will be using MySQL server for postgrey. If you don’t want postgrey or you want to use a different database backend, either don’t install a database server or install your database server of choice. Information about using PostgreSQL will be added at some point. In Centos the default postfix in the base repository is not compiled with mysql support, it is alternatively provided in the centosplus repository.

  1. Install Needed Software
    1. Install the needed software and EPEL repository using yum and rpm. Please read this step VERY CAREFULLY before proceeding:
    2. note: these changes are TEMPORARY and should be reversed after the successful install of postfix. It also assumes you have yum-priorities (yum install yum-priorities) installed.

      vi /etc/yum.repos.d/CentOS-Base.repo Locate the following sections and edit them according to the instructions:

      [base]
      name=CentOS-$releasever – Base
      mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
      #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
      #exclude=*386 *586 *686
      priority=2 <– Change this value to 2, change it back to 1 once postfix is installed.
      #exactarch=1
      gpgcheck=1
      gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5

      [centosplus]
      name=CentOS-$releasever – Plus
      mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
      #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
      priority=1 <– Change or add this line to read exactly as you see here, add a # once postfix is installed.
      gpgcheck=1
      enabled=0
      gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5

      IMPORTANT: if you have ANY of these packages already installed from RPMForge or Base, remove them BEFORE you proceed:
      postfix postfix-pflogsumm mysql mysql-server spamassassin clamav amavisd-new cyrus-sasl clamav-update sqlgrey
      to check: rpm -qa | grep -i -E “(rf|postfix|amavisd-new|clamav)” | sort -d -u

      rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
      yum –disablerepo=\* –enablerepo=centosplus,base install postfix postfix-pflogsumm
      yum
      –disablerepo=\* –enablerepo=base,centosplus,epel install mysql mysql-server spamassassin clamav amavisd-new cyrus-sasl clamav-update sqlgrey

  2. Configure and Test Postfix
    1. Do some basic configuration to setup postfix before first starting it. Find the configuration variables and update them. Edit the /etc/postfix/main.cf configuration file and make the following changes:
    2. Start the server for the first time:
    3. Send a test mail to a local user using telnet:
    4. commands are in bold reponses are in italics

    5. Check the users mail with the ‘mail’ command when logged in as the local-user:
    6. If this worked delete the users mail and move on, ‘man mail’ for more information about the mail command.

  3. Do Some More Configuration for Postfix
  4. This section start to configure postfix to be more secure. There are some options that are personal preferences of the author and may be changed. They are as follows:

    • Mail is stored in $HOME
    • TLS required for sending mail remotely
    • Certificates in /etc/postfix
    • The ‘standard’ is /etc/pki/tls

    1. Update the /etc/postfix/main.cf postfix configuration file and make the following changes:
    2. These changes are additions to the configuration file and may be added at the end of the file.
    3. Move your certificates to the proper location (/etc/postfix/cert.pem and /etc/postfix/key.pem respectivly) and set proper permissions (600).

      If you don’t have a certificate already, you may generate a self signed cert with the following commands:

    4. Restart the server:
    5. Try to send test mail to your local-user account both from localhost and a remote server.
    6. This should work. It would also be a good test to make sure that your server will not relay mail so try to send mail to another host using your server. It is recommended to continue to send testing mail with telnet so the maximum amount of information is available to debug what is going wrong. You should notice a new response from the server after you ‘EHLO‘:

    7. Test if TLS is working correctly:
    8. commands are in bold reponses are in italics

      If you do not see ‘Ready to start TLS’, something is wrong with your TLS setup.

  1. Test SMTP Auth Using a Standard Mail Client
  2. Use your favorite mail client to test if SMTP auth is working. If TLS is not working, SMTP auth will also not work because this howto forces postfix to use TLS when doing SMTP auth.

    1. Start sasl:
    2. Start your favorite email client and send a test message to another server/mail system. Connect to the server with the following settings:
    3. In addition to a remote account, you could also send a test message to root, another account or yourself.

      • Host: host.example.com
      • User: local-user
      • Password: local-user’s password
      • Force TLS for SMTP
      • Force SMTP Auth

  3. Setup Amavisd-New, Spam Assassin, Clam-AV
  4. Amavisd-new is the content filter that will run the spamassassin and clamav checks. It could also be configured to do other checks and has many other features. Those addtional features are outside the scope of this howto and might be added later.

    1. Configure amavisd-new. Make the following changes to the /etc/amavisd/amavisd.conf config file:

      $myhostname is only needed when the server has not been assigned a FQDN, however, it does not hurt to set the variable; check with the command ‘hostname’

    2. Configure SpamAssassin to do extended checks such as rbl, pyzor, razor2, etc. Make the following changes to the /etc/mail/spamassassin/local.cf config file:
    3. Enable ClamAV to do automatic updates to virus definitions. Make the following changes to /etc/sysconfig/freshclam:

      Note: The change is to comment out this line.

    4. Update /etc/freshclam.conf to enable automatic updates:

      Note: The change is to comment out ‘Example’.

    5. Start everything up:

  5. Configure Postfix to Use the New Content Filtering System
  6. Postfix needs to be told to use the new content filtering system. A few things need to be changed to enable the new filtering system.

    1. Add the following to /etc/postfix/master.cf:
    2. Add the following to the /etc/postfix/main.cf config file:
    3. Restart postfix to apply the changes:

  7. Setup Grey Listing
  8. Grey listing is an anti-spam technique that is used to twart spammers from doing drive by spamming. There are two steps to get it working with postfix. Setting up the mysql database and then enabling the checks. You may use any supported database you would like but additional database configurations are outside of the scope of this howto. Replace sensitive information such as passwords with unique settings.

    1. Setup the mysql database:
    2. This assumes mysql server has not been setup and we are dealing with a fresh configuration. If mysql is already setup, you will need to use the ‘-p’ switch for the mysql commands
      and there is no reason to set a new mysql root password. Also note, you may use whatever user/database name you want but this will need to be updated in the conf file.

    3. This will bring you to the mysql shell where you can add the needed user and database for sqlgrey:
    4. Commands are in bold responses are in italics

    5. Set a root password for mysql:
    6. Configure sqlgrey for the database. Make the following changes to the /etc/sqlgrey/sqlgrey.conf config:
    7. Start the sqlgrey service:

  9. Setup Postfix to Do Grey Listing
  10. Postfix needs to be configured to check the greylisting service for the status of a sender.

    1. Configure postfix to do the greylist check. Make the following update to the /etc/postfix/main.cf config file:
    2. Restart postfix to apply the changes:

  11. Set Services to Run on Boot
  12. The combination of services need to get set to run on boot. Do so with ‘chkconfig':

Troubleshooting / How to Test

Test by sending mail from a remote service/server.

Common problems and fixes

The most common issue is networking issues. Please be sure your networking is setup correctly. For example the below is to allow port 25:tcp using iptables:

More Information

Any additional information or notes.

Disclaimer

We test this stuff on our own machines, really we do. But you may run into problems, if you do, come to #centoshelp on irc.freenode.net

Added Reading