How to set up a mail server in Ubuntu or Debian

This tutorial will discuss how to set up a working mail server in Ubuntu or Debian. As we know, the two major protocols used in a mail server are SMTP and POP/IMAP. In this tutorial, postfix will be used for SMTP, while dovecot will be used for POP/IMAP. Both are open source, stable and highly customizable.

Please note that securing a mail server is beyond the scope of this tutorial, and will be covered in future tutorials.

Prerequisites

Each domain should have a DNS server. It is recommended NOT to use a live domain for testing purposes. In this tutorial, a test domain example.tst will be used in a lab environment. A DNS server for this hypothetical domain should have the following records at the least.

  • Forward zone for example.tst:
    			IN MX 10	mail.example.tst.
    mail.example.tst.	IN A		192.168.10.1
    
  • Reverse zone for example.tst:
    192.168.10.1		IN PTR	mail.example.tst.
    

    While configuring a live mail server, these records can be changed based on system requirements.

    Setting Hostname

    First, the hostname of the mail server must be specified in /etc/hostname and /etc/hosts. The former should contain the hostname only.

    root@mail:~# vim /etc/hostname
    mail
    
    root@mail:~# vim /etc/hosts
    ## IP			Fully Qualified Domain Name 	Hostname ##
    192.168.10.1		mail.example.tst			mail
    

    Adding Users

    Every Linux user, by default, has a mailbox automatically created. These users and mailboxes will be used as email accounts and their respective mailboxes. Creating a user is very easy.

    root@mail:~# adduser sarmed

    Install and Configure SMTP

    Service Profile: postfix
    Configuration file directory /etc/postfix/
    Script /etc/init.d/postfix
    Log file /var/log/mail.log
    Script /etc/init.d/postfix
    Port number TCP/25

    SMTP: Installing postfix

    postfix is one of the most widely used SMTP servers because it is stable, lightweight, scalable, and highly customizable. Setting up postfix can be done using apt-get.

    root@mail:~# apt-get install postfix

    During installation, the type of email server and the domain name are specified.

    Since this mail server will send emails directly towards destination, "Internet Site" is used.

    The domain name of the mail server is also set. This will cause all mails originating from this mail server to have @example.tst as the sender's domain.

    The configuration files of postfix are stored in /etc/postfix. The following configuration files are important. Some of them may not be present and need to be created manually.

    • transport: Primarily used to define how a mail should be routed towards specific destination domains. Bypassing DNS queries can be a good example. In that case, one may need to send emails destined to domain XYZ.com directly to IP address X.Y.Y.X regardless of any DNS query results.
    • access: Can be used for security purposes like blocking senders/recipients and their domains.
    • aliases: Is used to define user aliases. For example, emails sent to userA should be received by userB and userC as well.
    • main.cf: Is the configuration file for postfix.

    SMTP: Preparing Configuration Files

    Time to prepare the configuration files. The transport and aliases files are not provided with the installation, and created manually.

    root@mail:~# cd /etc/postfix
    root@mail:/etc/postfix# touch transport aliases

  • main.cf
  • main.cf is backed up and then modified. The following lines are added/modified in the configuration file. For more detailed info about the parameters, refer to the official README and configuration document.

    root@mail:/etc/postfix# vim main.cf
    ## the name of the server ##
    myhostname = mail.example.tst
    
    ## alias definitions ##
    alias_maps = hash:/etc/postfix/aliases
    alias_database = hash:/etc/postfix/aliases
    
    ## transport definition ##
    transport_maps = hash:/etc/postfix/transport
    
    ## myorigin defines the domain name for emails originated from this server. In this case, all outgoing mail should have '@example.tst' as sender domain ##
    myorigin = example.tst
    
    ## mydestination parameter specifies what domains this machine will deliver locally, instead of forwarding to another machine. ##
    mydestination = mail.example.tst, localhost.example.tst, localhost, hash:/etc/postfix/transport
    
    ## the smarthost address. Not used in this tutorial and will be covered in the future##
    relayhost =
    
    ## the trusted sender networks. postfix will not forward mails originated from other subnets ##
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.10.0/24
    
    ## mailbox size in bytes. 0 denotes no limit ##
    mailbox_size_limit = 0
    
    ## postfix will listen on all available interfaces i.e. eth0, eth1, eth2 and so on ##
    inet_interfaces = all
    
  • transport
  • Mails destined to domain example.tst are defined to be delivered locally without any DNS queries.

    root@mail:/etc/postfix# vim transport
    example.tst	local:
    .example.tst	local:
    
    root@mail:/etc/postfix# postmap transport

  • aliases
  • Assuming all mails sent to userA should be received by userB as well, the aliases file is modified as stated below.

    root@mail:/etc/postfix# vim aliases
    userA: userA, userB
    
    root@mail:/etc/postfix# postalias aliases

    Note: The syntax 'userA: userB' specifies that the mail should be forwarded to userB only. userA will not receive a copy of the email.

    SMTP: Initiating the Service

    postfix can be started using the command.

    root@mail:~# service postfix restart

    The log file at /var/log/mail.log should provide useful information in case something fails. Whether or not the mail server is listening on TCP port 25 can also be verified using netstat.

    root@mail:~# netstat -nat
    tcp     0        0        0.0.0.0:25       0.0.0.0:*    LISTEN
    

    As it can be seen from the output, the server is listening on TCP port 25 for incoming connection requests.

    Install and Configure POP/IMAP

    Service Profile: dovecot
    Configuration file directory /etc/dovecot
    Script /etc/init.d/dovecot
    Log file /var/log/mail.log
    Script /etc/init.d/dovecot
    Port number TCP: 110 (POP3), 143 (IMAP), 993 (IMAPS), 995 (POP3S)

    POP/IMAP: Installing dovecot

    dovecot is without a doubt leading IMAP and POP server software used in the open source community. It is very easy to set up and configure dovecot. Once again, apt-get will be used to install dovecot.

    root@mail:~# apt-get install dovecot-common dovecot-pop3d dovecot-imapd

    Out of the box, dovecot can support POP3 and IMAP (plain text), as well as encrypted POP3S and IMAPS (secured). By default, dovecot will create and use a self-signed certificate for SSL encryption. Certificates can be manually created or imported later based on requirements. In this tutorial, a self-signed certificate generated by dovecot will be used.

    POP/IMAP: Preparing Configuration Files

    The following parameters are modified as needed.

    root@mail:~# vim /etc/dovecot/conf.d/10-mail.conf
    ## the location of the mailbox is specified in 'mbox' format ##
    mail_location = mbox:~/mail:INBOX=/var/mail/%u
    
    ## dovecot is granted necessary permission to read/write user mailboxes ##
    mail_privileged_group = mail
    

    That should be enough to start POP/IMAP service in the mail server.

    POP/IMAP: Initiating the Service

    Now that dovecot is installed and configured, it can be launched using the following command.

    root@mail:~# service dovecot restart

    Again, The log file (/var/log/mail.log) can provide important clues should something go wrong. Whether dovecot is running can also be verified using netstat.

    root@mail:/etc/dovecot/conf.d# netstat -nat
    tcp      0      0      0 0.0.0.0:110      0.0.0.0:*      LISTEN
    tcp      0      0      0 0.0.0.0:143      0.0.0.0:*      LISTEN
    tcp      0      0      0 0.0.0.0:993      0.0.0.0:*      LISTEN
    tcp      0      0      0 0.0.0.0:995      0.0.0.0:*      LISTEN
    

    Using the Mail Server with Mail User Agent (MUA)

    The mail server is now ready to be used. Email accounts can be configured using your favorite email client software in desktop, laptop, tablet or phone. Webmail can also be configured in the server, but setting up webmail will be covered in future tutorials. The following is a screenshot with necessary parameters in Mozilla Thunderbird.

    Troubleshooting Mail Server

    • The log file /var/log/mail.log is your best friend. Any clue about why email is not working can be found here.
    • Make sure that the firewall is properly configured.
    • Make sure that the DNS server has proper entries.

    To sum up, the demonstration in this tutorial is meant to run in a lab environment. A test DNS server with all necessary records can be deployed, and mails can be exchanged between users in the same server, i.e., same domain. To make things more interesting, multiple mail servers with different domains can be deployed to check how email communication works across domains, given that necessary DNS records are present.

    Valid DNS records are needed for live mail servers. The settings of postfix and dovecot can be tuned based on needs.

    Warning: For those who want to deploy live mail servers, or any mail server that has access to the Internet, make sure that your SMTP is secured. Attacks on SMTP can commonly originate from the Internet, as well as from malicious software within the LAN.

    Hope this helps.

    Subscribe to Xmodulo

    Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

    The following two tabs change content below.
    Sarmed Rahman is an IT professional in the Internet Industry in Bangladesh. He writes tutorial articles on technology every now and then from a belief that knowledge grows through sharing. During his free time, he loves gaming and spending time with his friends.

    28 thoughts on “How to set up a mail server in Ubuntu or Debian

    1. Thanks for the great tutorial. Just a small thing. You mention vim transport two times. When you configure aliases, shouldn't it be vim aliases?

    2. Nice howto and very informative. But my two cents: SASL will make this server more secure from SPAM.

    3. On Ubuntu you can just install 'mail-stack-delivery' package and get SMTP(s), IMAP(s), POP3(s), including SASL, managesieve and dovecot as delivery agent. For additional antispam and antivirus protection, you can, in addition, install amavisd-new-postfix.

    4. Will you write complete howto on: Postfix with spamassasin, clamav, razor, virtual domains with postfix admin. Thanks.

    5. Very good article. For more user friendlyness you could add an autoconfig.example.txt cname. This autoconfig is useful when you use Thunderbird to fill all the servername, ports, security, etc.

      I'll definitely give it a try.

      • Yes it is possible in lab environment. You could try by modifying the /etc/postfix/transport file to bypass dns queries. This tutorial could be helpful - http://xmodulo.com/2014/02/configure-mail-server-postfix-dovecot.html

        Here's what you could do-
        Scenario1:
        1. setup a mail server
        2. set hostname right for your server
        3. tweak transport file
        4. send email from one user to another within the server

        Scenario2:
        1. setup 2 mail servers
        2. set hosname right in both servers
        3. tweak transport file
        4. send email from one server to another

        The test should give you plenty of idea on how a mail server works.

        However, try to use a DNS if possible. You would get more experience out of the lab that way.

    6. Thank you for a great tutorial. I have a something similar set up on my site, but this is really very good indeed. I have learned a lot.

    7. Hello, when i try to send an email from my new address i have this error :
      554 5.7.1 : Relay access denied; from= to= proto=ESMTP helo=

      • Need more information for troubleshooting. Please verify that the IP adress/subnet of the sender is allowed in Postfix i.e. mynetworks in main.cf. Also, you could check whether any parameters are used in smtpd_sender/recipeint_restrictions.

    8. Thank you very much for this useful tutorial. Everything is clear and efficient. I set up this local mail server in less than 1 hour when I wasted days and days with other posts. I'm very exited to follow up with the rest of your publications.

      Great thanks.

    9. I followed your tutorial exactly. I can get mail, but I just can't send mail outside of the network. Do I have to configure relayhost or something?

      • I need more information to troubleshoot. Could you please share the mail server logs?

        There are a couple of things that could be checked-

        1. Is the domain name for your server valid?
        2. Can the PTR record of your server be mapped back to the mail server? Many mail servers check PTR records for security purposes.

        Still, looking at the mail log could provide specific clues on why mails are not being delivered.

    10. Thank you for providing a great tutorial! However, when attempting to connect to my mail server I receive auth failure. Below I have included the log:

      Apr 21 16:44:33 mail dovecot: imap-login: Login: user=, method=PLAIN, rip=66.87.148.130, lip=107.170.111.212, mpid=2162, TLS
      Apr 21 16:44:33 mail dovecot: imap(choard): Disconnected: Logged out bytes=30/372
      Apr 21 16:44:34 mail postfix/smtpd[2163]: warning: hostname 66-87-148-130.pools.spcsdns.net does not resolve to address 66.87.148.130: Name or service not known
      Apr 21 16:44:34 mail postfix/smtpd[2163]: connect from unknown[66.87.148.130]
      Apr 21 16:44:34 mail postfix/smtpd[2163]: lost connection after UNKNOWN from unknown[66.87.148.130]
      Apr 21 16:44:34 mail postfix/smtpd[2163]: disconnect from unknown[66.87.148.130]
      Apr 21 16:44:35 mail postfix/smtpd[2163]: warning: hostname 66-87-148-130.pools.spcsdns.net does not resolve to address 66.87.148.130: Name or service not known
      Apr 21 16:44:35 mail postfix/smtpd[2163]: connect from unknown[66.87.148.130]
      Apr 21 16:44:35 mail postfix/smtpd[2163]: lost connection after UNKNOWN from unknown[66.87.148.130]
      Apr 21 16:44:35 mail postfix/smtpd[2163]: disconnect from unknown[66.87.148.130]

      Thank you in advance for you time!

        • The issue seems to be with DNS. Could you please verify the following-

          1. The PTR record is properly defined.
          2. The hostname and domain name of the mail server is properly set in the server.
          3. The hostname and domain name of the mail server is properly set inside main.cf.
          4. Forward and reverse DNS queries are successful when run from the server itself.

          Please check the mentioned points and share the results with us. :)

    Leave a comment

    Your email address will not be published. Required fields are marked *

    Current day month ye@r *