I have successfully compiled, configured and ran Dovecot with virtual users feature.

Here's part of my /etc/dovecot.conf configuration file:

mail_location = maildir:/home/%d/%n/Maildir

auth default {
    mechanisms = plain login

    userdb passwd-file {
        args = /home/%d/etc/passwd
    passdb passwd-file {
        args = /home/%d/etc/shadow

    socket listen {
        master {
            path = /var/run/dovecot/auth-worker
            mode = 0600

I faced one issue I can't resolve myself. Is there anyway to create users' domains mapping and provide username in mail_location?

Examples: 1. currently I have /home/domain.com/user/Maildir 2. I'd like to have /home/USER/domain.com/user/Maildir

Can I achieve this somehow?

Greets, Stojko

  • What is "USER" and how is it different from "user"?. The home directory can be specified in your virtual user database. The passwd-file has the same format as the system passwd file. Specify a home directory, and then use ~/ or %h in your mail_location. (e.g. mail_location = ~/Maildir). See the Dovecot wiki for the passwd format. – cyberx86 Apr 24 '12 at 2:05
  • @cyberx86 USER is system account, an owner of the account. Sorry for not being clear. – Stojko Apr 24 '12 at 8:17

Consider what you are asking for a moment. You want to include the user's home directory in the mail_location. On the surface it seems like a reasonable request - but you must ask - how does Dovecot know the user's home directory?

System users are stored in /etc/passwd - that file contains


(Note: instead of a password, you may have x with the password stored in /etc/shadow; or * to deactivate the account; comment is actually a 'gecos field')

If Dovecot reads this file - it is provided with the user's home directory - once it has that information, it can use it elsewhere (i.e. it will be able to use '~/to signify the home directory, and will set%h` to the same).

If, however, you are using virtual users - instead of system users - one of the fundamental ideas is that not every virtual user has to be a system user. In this case, Dovecot has no way of knowing if a virtual user that shares the same name as a system user is really the same user or not (it also cannot use the UID, since it is possible to have multiple virtual users map to the same UID). It will not assume anything beyond what is provided in your userdb.

A Userdb can provide the following information to Dovecot:

  • uid: User's UID (UNIX user ID), overrides the global mail_uid setting.
  • gid: User's GID (UNIX group ID), overrides the global mail_gid setting.
  • home: User's home directory, overrides the global mail_home setting. Although not required, it's highly recommended even for virtual users.
  • mail: Mail location, overrides the global mail_location setting.
  • Optional extra fields which are passed to the IMAP/POP3 process in environment variables.

In your configuration, you are using a passwd-file - that is, a file with the same format as the system passwd file, but which you have setup for this purpose (i.e. it is not the system passwd file). Dovecot accepts the following passwd-file format (and ignores the gecos and shell fields):


You can override certain defaults, on a per user basis, by specifying (space delimited) key=value extra fields (with keys prefixed with userdb_ in most cases). For instance, userdb_mail=Maildir:~/mail will override mail_location.

Just to reiterate, by using virtual users (and not using the system passwd file), you have told Dovecot that these users might not have any relation to system users, and so it is only going to use the information you provide it, without assuming anything else.

Finally, to try and actually answer your question:

  • You can use several variables in mail_location - these are defined on Dovecot's Variables page.
  • The variable %h or, preferably ~/ can be used to return the home directory that Dovecot retrieved from its UserDB query (and sets mail_home)
  • You should specify a home directory for all users in your UserDB
  • You can override mail_location by returning userdb_mail from your userdb query.

So, basically, set home in your userdb if you want to use the home directory in Dovecot, because it can't know what you don't tell it.


According to this link You can use "several variables in the mail_location setting"

%u: Full username.
%n: User part in user@domain, same as %u if there's no domain.
%d: Domain part in user@domain, empty if there's no domain.


mail_location = maildir:/home/%n/%d/%n/Maildir

should point to /home/user/domain/user/Maildir

  • Full username is user@domain.com, I'd rather like to hqave owner, system account here :) – Stojko Apr 24 '12 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.