Email for multiple virtual domains with exim4 dovecot

This is a Tutorial about setting up exim4 on Debian

Many mail servers need to handle mail for multiple virtual domains. This can easily be achieved with Sendmail, postfix, or qmail.
Here we’ll cover how it’s done with exim4 – the default mailserver for Sarge.

Installing Exim4 – if you need to

Because I’m going to want to scan incoming mail for viruses as they are delivered, rather than filter them out myself once I’ve accepted them I will install the heavy version of the server:

apt-get install exim4-daemon-heavy
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
exim4-base exim4-config
Suggested packages:
eximon4 exim4-doc-html exim4-doc-info
The following NEW packages will be installed:
exim4-base exim4-config exim4-daemon-heavy
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1033kB/1450kB of archives.
After unpacking 1155kB of additional disk space will be used.
Do you want to continue? [Y/n]


Once this has downloaded you will start the debconf configuration process.
The first question asked is ‘Do you wish to use a single monolithic configuration file, or split files?’ Here I chose the split file approach, to make future upgrades more simple.

Once that was done you’re left to choose the type of configuration you wish to use – I chose “an internet site”, because I want to send and receive mail directly. This choice does affect the subsequent questions you’ll have.
Choosing the mail name should be obvious, as should choosing which addresses you wish the server to listen upon.
Once that’s done the basic setup is complete – but we’ve still got to handle the virtual domain setup.

Generate Local Server-side Certificate
Run the command:



(adding the ‘–force’ option if you have already done this once. This is a debian specific operation, and there is no analogous step needed on other systems
Then execute:

openssl req -new -key /etc/exim4/exim.key -out /etc/exim4/exim.csr


Here comes the command to create a Dovecot certificate:

openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/certs/dovecot.pem -keyout /etc/ssl/private/dovecot.pem
sudo vi /etc/dovecot/conf.d/10-ssl.conf


put this only

ssl_cert = </etc/ssl/certs/dovecot.pem
ssl_key = </etc/ssl/private/dovecot.pem

sudo vi /etc/dovecot/conf.d/10-mail.conf


disable this line

######## mail_location = mbox:~/mail:INBOX=/var/mail/%u


Handling Virtual Domains
If you’ve just installed the server and you followed the debconf setup I did then you’ll have a server which will send and receive mail for a single domain.
The way that I’ve chosen to handle virtual domain mails, (which was used in the exim3 article) is to have a text file for each domain which contains a mapping between the email address and a local user.
For example assume you wish to handle mail for three domains:
Create three text files:


Each file will contain lines of the form:

address : username@localhost

Where “address” is the part of the email address to the left of the domain name, and “username” is the account name on the local system which should recieve that mail.
To make it more clear assume:
Alice gets all mail sent to
Bob gets all mail sent to
Eve has a mail account at, as does Trent.
This gives /etc/exim4/virtual/

eve : eve@localhost
trent : trent@localhost


For Bob who has a “catchall” address setup for –

* : bob@localhost


And similarly for Alice – /etc/exim4/virtual/

* : alice@localhost


You can also drop all mail for a user by using:

eve : :blackhole:

Or to generate a bounce with some text added so that the sender will know why they got it:

sharon : :fail: She no longer lives here.

Now we just need to make exim4 read these files to know what to do with the incoming mail, a simple enough job.
First of all we need to update the list of domains we handle mail for by editing the file /etc/exim4/conf.d/main/01_exim4-config_listmacrosdefs, change the current “local_domains” with this one:

domainlist local_domains = @:localhost:dsearch;/etc/exim4/virtual

This will cause the local_domains setting to include the domain list from the directory we’ve specified. (Which is why the text files are named after the domain name to which they apply).
Now that the list of domains has been updated we need to create a new file – edit the file /etc/exim4/conf.d/router/350_exim4-config_vdom_aliases and add the following contents to it:

driver = redirect
domains = dsearch;/etc/exim4/virtual
data = ${expand:${lookup{$local_part}lsearch*@{/etc/exim4/virtual/$domain}}}
pipe_transport = address_pipe
file_transport = address_file


Here we tell exim how to lookup the local recipients.
Once this has been done you can restart the server and see how it works:

/etc/init.d/exim4 restart


This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):