Mail server setup on Ubuntu Lucid

Update: I keep having to redo this setup and get tripped up on what to install all the time so here is the complete install line: apt-get install postfix dovecot opendkim postfix-mysql dovecot-pop3d dovecot-imapd which already assumes you’ve got mysql server installed.

Update: I just had to redo everything from scratch on a new build, here is how to add the virtual user:

groupadd -g 1001 virtual
useradd -g virtual -u 1001 virtual -d /var/spool/vmail -m

Update: I just got SMTP to work on a non default port. I applied the change described here, in my case I changed the line in master.cf so it looks like this:

26 inet n - n - - smtpd

I also applied the changes described by tumbleweed here, don’t know if they are required though, the above master.cf change for sure is. It broke mail receiving/fetching though, had to roll back and haven’t had time to investigate yet.

Update: The below tutorials seem to lack the proper apt-get line, well here it is: sudo apt-get install dovecot-imapd dovecot-pop3d postfix-mysql

Update: Also make sure you have 127.0.0.1 localhost in your /etc/hosts for smtp to work properly!

This is the first time I’ve seriously tried and actually managed to setup a mail server with all the bells and whistles like MySQL based storage of virtual users and passwords, authenticated SMTP relay (mail users can send via SMTP), POP3 and IMAP fetch of mail.

A big part of this massive success has got to do with the fact that I’m now using for instance Postfix 2.7 (apparently >= 2.3 is what you want to have, check which version you have with: postconf -d mail_version).


I first started out with this how to forge tutorial before I found what is probably the best linux email admin tutorial on the net, the ISPmail tutorial by Christoph Haas.

This development left me with a non ISPmail like setup when it comes to the mail directories which in my case looks like this: /var/spool/domain.com/user.name where the email address looks like this: user.name@domain.com. My mail user is also not called vmail but virtual.

Apart from that my setup does not differ from the ISPmail setup, below I merely show my main.cf, master.cf and dovecot.conf as that is something I find lacking with the ISPmail tutorial. You go through it never knowing if the reason for a problem you have is due to the fact that your own setup might differ in ways that are not discussed.

Therefore you have it below, the below config works but might not be optimized in various ways, like avoiding spam filters on outgoing mail for instance. My knowledge of these things are too limited to tell at the moment. This is a good start anyway.

In going through the ISPmail tutorial and you erase your /var/log/mail.log file and logging stops working you can get it running again by running: restart rsyslog.

myhostname = server-serverip.isp.net
mydomain = isp.net
mydestination = $myhostname, localhost.$mydomain, localhost
myorigin = isp.net
mynetworks = 67.89.45.34 127.0.0.0/8
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
smtpd_sasl_type=dovecot
smtpd_sasl_path=private/auth
smtpd_sasl_auth_enable=yes
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_auth_only=no
mailbox_size_limit = 0
recipient_delimiter = +
virtual_mailbox_domains=mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps=mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_uid_maps = static:1001
virtual_gid_maps = static:1001
virtual_alias_maps=mysql:/etc/postfix/mysql-virtual-alias-maps.cf,mysql:/etc/postfix/mysql-email2email.cf
virtual_transport=dovecot
dovecot_destination_recipient_limit=1

There is not much to add to the above, after creating the virtual user you can check what the GID and UID is by running id virtual. The 127.0.0.0/8 value is needed to allow code/scripts like Zend Mail (my preferred PHP email library) to send emails.

pickup    fifo  n       -       -       60      1       pickup
cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
	-o smtp_fallback_relay=
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix	-	n	n	-	2	pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}
dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=virtual:virtual argv=/usr/lib/dovecot/deliver -d ${recipient}

The above is basically default except for the two last rows which were added in the process of completing the ISPmail tutorial.

protocols = imap imaps pop3 pop3s
disable_plaintext_auth = no
log_timestamp = "%Y-%m-%d %H:%M:%S "
mail_location = /var/spool/vmail/%d/%n
mail_privileged_group = mail

protocol imap {
}

protocol pop3 {
  pop3_uidl_format = %08Xu%08Xv
}

protocol managesieve {

}

protocol lda {
  log_path = /var/spool/vmail/dovecot-deliver.log
  auth_socket_path = /var/run/dovecot/auth-master
  postmaster_address = hsarvell@gmail.com
  mail_plugins = sieve
}

auth default {
  mechanisms = plain login
  passdb sql {
    args = /etc/dovecot/dovecot-sql.conf
  }
  userdb passwd {

  }

  userdb static {
    args = uid=1001 gid=1001 home=/var/spool/vmail/%d/%n allow_all_users=yes
  }

  user = root
  
  socket listen {
    master {
        path = /var/run/dovecot/auth-master
        mode = 0600
        user = virtual
    }

    client {
        path = /var/spool/postfix/private/auth
        mode = 0660
        user = postfix
        group = postfix
    }
  }
  
  !include_try /etc/dovecot/auth.d/*.auth
}

dict {

}

plugin {

}

Also basically default with additions prescribed by the ISPmail tutorial, except for this line:

args = uid=1001 gid=1001 home=/var/spool/vmail/%d/%n allow_all_users=yes

It reflects my directory structure as explained above and my different uid and gid.

Note also the mail_plugins = sieve line, it’s not cmusieve like in the tutorial.

The /etc/dovecot/dovecot-sql.conf file:

driver = mysql
connect = host=127.0.0.1 dbname=mailserver user=root password=1234
default_pass_scheme = PLAIN-MD5
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u'

After going through the whole procedure on a new server I got this cryptic message in my /var/log/mail.log from the dovecot startup process:
dovecot: auth(default): Fatal: Socket already exists: /var/spool/postfix/private/auth
dovecot: dovecot: Fatal: Auth process died too early - shutting down

Commenting everything but the first mechanism statement in the file in /etc/dovecot/auth.d/ (mine is called 01-mail-stack-delivery.auth) took care of that problem.

Note that startup problems might be visible in /var/log/mail.log while runtime errors can be found in /var/log/mail.err.

Related Posts

Tags: , , ,