Core Functionality and Purpose
Postfix relies heavily on lookup tables to separate configuration logic from data storage. Instead of embedding thousands of email addresses directly intomain.cf, you point Postfix to a lookup table file. When Postfix needs to resolve an address or check permissions, it queries this table. The most common types are hash tables (hash:/etc/postfix/virtual) and linear text files processed by utilities like postmap.
These tables serve several critical functions in a tech support context:
- Virtual Aliasing: Mapping multiple email addresses to a single mailbox or forwarding them to different destinations.
- Relay Access Control: Defining which clients or networks are allowed to send mail through the server.
- Address Rewriting: Modifying sender or recipient addresses before processing, often used for compliance or internal routing.
- Domain Verification: Checking if a domain is local or should be relayed externally.
Creating and Formatting Lookup Tables
To create a basic lookup table, you typically start with a plain text file. Each line contains a key (the input string) followed by whitespace and then the value (the output). For example, a virtual alias map might look like this:[email protected] [email protected]
[email protected] [email protected]
The format must be consistent. Keys cannot contain spaces unless they are quoted, and comments starting with # are ignored. Once the text file is populated, it must be compiled into a binary database format that Postfix can read efficiently. This is done using the postmap command.
For a file named /etc/postfix/virtual, you would run:
sudo postmap /etc/postfix/virtual
This command generates a companion file, usually /etc/postfix/virtual.db. Postfix will automatically detect the .db extension and use the appropriate reader. If you change the text file, you must re-run postmap to update the database. Failure to do so is a common source of errors where changes appear not to take effect.
Configuring Postfix to Use Lookup Tables
After creating and compiling your lookup table, you need to tell Postfix where to find it and what type of table it is. This is done inmain.cf or master.cf depending on the use case.
For virtual aliases, you add a line to main.cf:
virtual_alias_maps = hash:/etc/postfix/virtual
The prefix hash: tells Postfix to use the hash database reader. Other prefixes include btree:, ldap:, mysql:, and pgsql:. You can also combine multiple sources by separating them with commas:
virtual_alias_maps = hash:/etc/postfix/virtual, ldap:/etc/postfix/ldap-map.cf
When defining relay access controls, you might use smtpd_recipient_restrictions to reference a lookup table that lists allowed recipients. Always ensure that the path in the configuration matches the actual location of your compiled database file.
Troubleshooting Common Issues
Even with correct syntax, lookup tables can cause problems due to permissions, caching, or logical errors. Here are the most frequent issues:- Permissions Denied: Postfix runs as the
postfixuser (ordaemon). Ensure the lookup table files have read permissions for this user. A common fix is:sudo chown root:root /etc/postfix/virtual* && sudo chmod 644 /etc/postfix/virtual*. - Changes Not Applying: Remember to run
postmapafter editing text files. Also, reload Postfix withsudo systemctl reload postfixto pick up configuration changes. - Multiple Matches: In some table types, if multiple keys match, Postfix may return all matches or just the first one depending on the table driver. Check the documentation for the specific table type you are using.
- Loop Detection: If you rewrite addresses incorrectly, you might create a loop where Postfix tries to deliver an email back to itself. Monitor the mail log for "fatal: loop detected" errors.
Frequently Asked Questions
How do I view the contents of a compiled Postfix lookup table?
You can use the postmap -q command to query a specific key from a compiled database. For example, running postmap -q [email protected] hash:/etc/postfix/virtual will return the mapped value for that address.
What happens if a lookup table entry has a typo or invalid format?
Postfix will typically ignore the malformed line or fail to load the entire table, depending on the severity. It is best practice to test your configuration with postfix check before reloading, as it validates syntax and file permissions.
Can I use regular expressions in Postfix lookup tables?
Standard hash or btree tables do not support regex. However, you can use pcre: (Perl Compatible Regular Expressions) or regexp: table types in your main.cf configuration to perform pattern matching against lookup table entries.
Why are my virtual aliases not working despite correct configuration?
Common reasons include forgetting to run postmap after editing the source file, incorrect file permissions preventing Postfix from reading the database, or conflicts with other transport maps that override the virtual alias settings.
Is it safe to edit lookup tables while Postfix is running?
Yes, but you must reload Postfix after making changes to apply them. Directly editing the compiled .db file is dangerous and should never be done; always edit the source text file and regenerate the database.