In Exim I have one file per domain where the filename itself is used to determine the domain for the aliases.

For example, I'd have a file named example.org with contents like this:

foo:      somelocaluser

This causes mail to foo@example.org to be delivered to somelocaluser.

In Postfix the common setup is to use the file named virtual and repeat the domain for every local part over and over again:

foo@example.org: somelocaluser
bar@example.org: somelocaluser

How can I achieve my Exim-style setup in Postfix and thus avoid the repetiton?


No, you can't

Your desired feature doesn't exist in postfix hash table. Postfix database lookup always get key from the entry of the file, it can't mix it with the file name.

More info: Postfix Lookup Table Overview

However, you can achieve the same with little scripting. The script should loop all files then append the file name string to first column of the entry then combine it.

The ugly script like this should do the job fine

for file in `find /tmp/mapfiles -type f -maxdepth 1`; do
    grep : "$file" | while read line; do
        mykey=$(echo "$line" | cut -d':' -f1)
        myvalue=$(echo "$line" | cut -d':' -f2)
        filename=$(basename "$file")
        echo "${mykey}@${filename}: ${myvalue}"

Don't forget to postmap or postalias the output file.

  • Thank you for your answer. Is there at least a way that allows me to add new domains on the fly without explicitly having to name them in the configuration. A wildcard - maybe something like virtual_alias_maps = hash:/etc/postfix/virtual_files/* ?
    – user569825
    Mar 13 '15 at 0:18
  • I think postfix configuration parser doesn't support that :(
    – masegaloeh
    Mar 13 '15 at 0:21
  • As my files are named like the virtual domain name they refer to, here is the less ugly script :) for file in /path/to/virtual/files/* ; do sed "s,:,@$file,g" $file ; done > /etc/postfix/virtual
    – user569825
    Jun 18 '15 at 11:21
  • Should be cd /path/to/virtual/files/ && for file in * ; do sed "s,:,@$file,g" $file ; done > /etc/postfix/virtual
    – user569825
    Jun 18 '15 at 11:34

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.