Friday, October 19, 2012

Seting up an Ubuntu Mail Server

Ubuntu Linux is quickly becoming one of the most popular Desktop Linux distributions. As Ubuntu gains in popularity in the desktop market it is also gaining in popularity as a server operating system as well. In this guide I will run though the basics needed to get an IMAP and SMTP mail server up an running on Ubuntu Linux.

Installing the needed Applications

Assuming you are starting from a fresh install of Ubuntu Server you will need to install the following packages:

  • postfix
  • sasl2-bin
  • courier-imap
  • courier-imap-ssl
You can install all the needed packages by running the following command:
sudo apt-get install postfix sasl2-bin courier-imap courier-imap-ssl


During the installation postfix will ask you for the general type of configuration, choose "Internet Site" and when the installer asks you for the hostname enter the fully qualified domain name (FQDN) that points to your server (i.e. server.example.com).

After all the packages have been installed you can move onto configuring them.

Configuration of Postfix

The first thing you need to do is configure postfix (our email server) to use SASL authentication. Using authentication will ensure that authorized users always have access to the mail server but will ensure that spammers and other malicious parties can't take use it to relay spam. Run the following lines of code to enable SASL authentication:

sudo postconf -e 'smtpd_sasl_local_domain ='

sudo postconf -e 'smtpd_sasl_auth_enable = yes'

sudo postconf -e 'smtpd_sasl_security_options = noanonymous'

sudo postconf -e 'broken_sasl_auth_clients = yes'

sudo postconf -e 'smtpd_recipient_restrictions =permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'

sudo postconf -e 'inet_interfaces = all'

sudo echo 'pwcheck_method: saslauthd' >> /etc/postfix/sasl/smtpd.conf

sudo echo 'mech_list: plain login' >> /etc/postfix/sasl/smtpd.conf

sudo adduser postfix sasl


Your Postfix installation is now configured to use authentication, however, logins will NOT be encrypted. This means that any username or password you send to the server will be sent in cleartext, which is extremely insecure. To encrypt the transmissions of usernames and passwords you will need to enable SSL encryption:
sudo postconf -e 'smtpd_tls_auth_only = no'

sudo postconf -e 'smtp_use_tls = yes'

sudo postconf -e 'smtpd_use_tls = yes'

sudo postconf -e 'smtp_tls_note_starttls_offer = yes'

sudo postconf -e 'smtpd_tls_key_file = /etc/ssl/private/smtpd.key'

sudo postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/smtpd.crt'

sudo postconf -e 'smtpd_tls_CA_file = /etc/ssl/certs/cacert.pem'

sudo postconf -e 'smtpd_tls_loglevel = 1'

sudo postconf -e 'smtpd_tls_received_header = yes'

sudo postconf -e 'smtpd_tls_session_cache_timeout = 3600s'

sudo postconf -e 'tls_random_source = dev:/dev/urandom'

sudo postconf -e 'myhostname = server.example.com'


Make sure you replace 'server.example.com' with the FQDN of your server. You may also replace the paths for the tls key, cert and CA file paths with the path to your custom certificates if you have one.

Next, you need to force postfix to use the maildir style of directory so that it will be compatible with the courier mail server.
sudo postconf -e 'home_mailbox = Maildir/'

sudo postconf -e 'mailbox_command ='

Configuration of Courier-imap

Next, you need to configure Courier as an IMAP server so that users can retrieve emails stored on the server:

sudo maildirmake /etc/skel/Maildir

sudo maildirmake /etc/skel/Maildir/.Drafts

sudo maildirmake /etc/skel/Maildir/.Sent

sudo maildirmake /etc/skel/Maildir/.Trash

sudo maildirmake /etc/skel/Maildir/.Templates

sudo cp -r /etc/skel/Maildir /home/myuser/

sudo chown -R myuser:usergroup /home/myuser/Maildir

sudo chmod -R 700 /home/myuser/Maildir **where myuser is the name of a user on the server (not root).


Note that every time you want to allow a new user email permissions you must copy the maildir folder into the user's home directory by issuing the command:
sudo cp -r /etc/skel/Maildir /home/user/


Finally, you should configure courier to use SSL encryption to increase security. Generate a self-signed SSL certificate using OpenSSL. Follow these instructions for more information about generating SSL certificates.

After you have generated your crt and key files you need to edit /etc/courier/imapd-ssl and provide the paths to your custom certificate files.

After following this guide you should hopefully have a functional email server based on the Ubuntu Linux operating system. Additional information about Ubuntu's email services can be found at ubuntu.com.