Results 1 to 5 of 5
  1. #1
    Joined
    Jul 2001
    Location
    UK
    Age
    51
    Posts
    20,229

    [Howto] A simple internal mail server using postfix and dovecot

    Introduction

    I thought I'd write this up as a guide, as much for my own benefit to document my setup but also because hopefully others may find it useful. Also, when researching I found very few basic guides as most dive straight off the deep end with an all singing, all dancing configuration that over complicates the basics for beginners.

    The objective is to set up an internal mail server for sending and receiving internal mail only using a fake domain name (example.com on an internal subnet of 192.168.0.0/24 is used in this example). This configuration will not send or receive external mail, and could be used at home on an internal network or within a small organization for internal communications.

    I am using CentOS 5 but this guide is equally applicable on any Linux distribution.

    Installing the Required Software

    Most Linux distributions come with Sendmail configured as the default mail transport agent (MTA). In this guide we are going to use Postfix in place of Sendmail as our MTA and Dovecot as our mail delivery agent (MDA). These are the only two bits of software we need, so install them using your distro's package manager (yum, apt-get etc), for example:

    Code:
    yum install postfix dovecot
    Configuring Postfix

    The first step is to configure our newly installed Postfix software. The main Postfix configuration file is /etc/postfix/main.cf and the Postfix website has some good example configurations. For our purposes, here are the lines that need uncommenting or changing from their default settings:

    Code:
    myhostname = hostname.example.com
    mydomain = example.com
    myorigin = $mydomain
    inet_interfaces = all
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
    mynetworks = 192.168.0.0/24, 127.0.0.0/8
    relay_domains = 
    home_mailbox = Maildir/
    myhostname is set to the fully qualified domain name where hostname is returned by 'uname -n'

    mydomain is your domain name

    myorigin is the domain name that locally-posted mail appears to come from and is delivered to.

    inet_interfaces = all sets the network interfaces that Postfix can receive mail on. These need to include at least localhost and the local domain.

    mydestination is the list of domains that will be delivered to.

    mynetworks is a list of trusted IP addresses that may send or relay mail through the server.

    relay_domains is a list of destination domains this system will relay mail to. By setting it to be blank we ensure that our mail server isn't acting as an open relay for untrusted networks.

    home_mailbox specifies whether to use mbox or Maildir format for our mailbox. In this case we have chosen to use Maildir format.

    Configuring Dovecot

    Dovecot is our mail delivery agent (MDA) and accepts mail from Postfix and delivers it to users mailboxes. Dovecot supports both mbox and Maildir style mailboxes and mail may be retrieved using IMAP, IMAPS, POP3 or POP3S protocols. Configuration for Dovecot is made to /etc/dovecot.conf and the following should be uncommented or set:

    Code:
    protocols = pop3
    mail_location = maildir:~/Maildir
    pop3_uidl_format = %08Xu%08Xv
    protocols In my example I am just using POP3. You can set as many or few allowed protocols as you like from imap imaps pop3 and pop3s.

    mail_location is set to Maildir format at the users home directory. Alternatives for mbox format are provided in the configuration file.

    pop3_uidl_format is a compatibility setting for Outlook 2003 and should be set.

    Finally, we need to create a /Maildir for each user on the system and set the appropriate permissions on that directory:

    Code:
    $ mkdir /home/myuser/Maildir
    $ chmod -R 700 /home/myuser/Maildir
    Start the Server

    Note: CentOS, Fedora and Red Hat users can use the system-switch-mail and system-switch-mail-gnome packages to automatically switch from Sendmail to Postfix.

    Now we're ready to start the server (don't forget to stop Sendmail first):

    Code:
    /etc/init.d/sendmail stop
    /etc/init.d/dovecot start
    /etc/init.d/postfix start
    and set services to start automatically:

    Code:
    chkconfig --level 345 sendmail off
    chkconfig --level 345 dovecot on
    chkconfig --level 345 postfix on
    Configure your Email Client

    Now you can configure your email clients (e.g Thunderbird) and start using your new email server. Set up email accounts for each user as user@example.com and set the POP3 and SMTP servers to the IP address of the server (or you can use the hostname if you have it mapped in /etc/hosts).

    Finally, as an added bonus, you can also receive root's mail direct to your inbox by adding your username as an alias for root in /etc/aliases:

    Code:
    root:		ned
    Last edited by Ned Slider; 11-18-2007 at 10:33 AM. Reason: corrected a few typo's

  2. #2
    Joined
    Dec 2000
    Location
    Out there, just read my posts
    Posts
    5,473

    Thumbs up Re: [Howto] A simple internal mail server using postfix and dovecot

    Nice work Ned, as always !

  3. #3
    Joined
    Jul 2001
    Location
    UK
    Age
    51
    Posts
    20,229

    Re: [Howto] A simple internal mail server using postfix and dovecot

    Quote Originally Posted by Jackal View Post
    Nice work Ned, as always !
    Thanks Jackal

    I'm hoping to eventually build on it to add extra functionality like external mail, spam filtering and AV scanning, but I want to build and test each step slowly so I fully understand as I go. It's going to be a long term project though

  4. #4
    Joined
    Dec 2000
    Location
    Out there, just read my posts
    Posts
    5,473

    Re: [Howto] A simple internal mail server using postfix and dovecot

    Quote Originally Posted by Ned Slider View Post
    Thanks Jackal

    I'm hoping to eventually build on it to add extra functionality like external mail, spam filtering and AV scanning, but I want to build and test each step slowly so I fully understand as I go. It's going to be a long term project though
    Cool, hopefully I'll get my main.cf to you this weekend, /me is lazy, and perhaps get SpamAssasin working again.

  5. #5
    Joined
    Jul 2001
    Location
    UK
    Age
    51
    Posts
    20,229

    Re: [Howto] A simple internal mail server using postfix and dovecot

    Bump, and so the project evolves...

    I should have made it very clear in my first post, but I'm totally new to the joys of mail servers so this is very much a learning curve for me, so now onto some more I've learned...

    What I didn't realise is just how easy this is to set up. The next stage in the development is to make our internal mail server also act as an external mail server to send and receive email internet wide.

    First sending mail - if your domain name is a real domain name that you've registered and own, then there's really nothing more to do to the above config. Simply change your email clients to use your new mail server as their smtp server for sending mail and everything should be fine. If it doesn't work, you may need to check your ISP hasn't blocked smtp (port 25). If you don't have a real domain name, then you'll need to use smtp_generic_maps to map user@example.com to user@gmail.com, for example.

    To receive mail we will need to set up a Mail eXchange (MX) record in DNS for our new mail server. Typically, the DNS records for our domain name may look like this:

    Code:
    @ IN A 555.555.555.555
    www IN A 555.555.555.555
    The first line is the top level domain catchall and points example.com to the IP address 555.555.555.555. The next line also ensures www.example.com goes to 555.555.555.555. This is the IP of your web server

    We need to add a new A record for our mail server before we can add the MX record. Lets suppose we want mail sent to a different server on IP 666.666.666.666, we create the A record and then the MX record to point to it:

    Code:
    mailhost IN A 666.666.666.666
    
    IN MX 10 mailhost.example.com.
    so mail to example.com is now pointed to 666.666.666.666 (note the "." dot at the end of the domain name - this is required).

    We should now be set up to receive external email on our domain - just don't forget to set up any needed port forwarding on your router if you're using one (you may also need to set proxy_interfaces if you're behind a NAT router).

    I have the sending part working, but haven't tested the receiving part yet. I've set up the A records in DNS but just haven't set the MX records yet to switch over the server.

    I guess the next thing for me to read up on is the security implications of running a mail server as I don't want to be an open relay for spammers so I need to understand how to secure my mail server before dumping it on the internet. As such, any advice or pointers to reading material on this topic would be much appreciated
    Last edited by Ned Slider; 12-19-2007 at 06:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •