How to Query Postfix Virtual Alias Mappings from Command Line


2 views

Postfix's virtual alias mapping is a powerful feature that allows mail administrators to redirect emails to different addresses without creating full mailboxes. This is commonly configured in files like /etc/postfix/virtual or in database tables.

The most straightforward way to test virtual aliases is using the postmap command with the -q (query) option:

postmap -q "tim@domain.com" hash:/etc/postfix/virtual

This will return the destination address if the alias exists, or nothing if it doesn't.

First verify where your virtual alias maps are stored:

postconf virtual_alias_maps

This might return something like:

virtual_alias_maps = hash:/etc/postfix/virtual, ldap:/etc/postfix/ldap-virtual.cf

For hash/file-based maps:

postmap -q "tim@domain.com" hash:/etc/postfix/virtual

For LDAP maps:

postmap -q "tim@domain.com" ldap:/etc/postfix/ldap-virtual.cf

For MySQL maps:

postmap -q "tim@domain.com" mysql:/etc/postfix/mysql-virtual.cf

For more detailed debugging:

postmap -vq "tim@domain.com" hash:/etc/postfix/virtual

Here's a simple bash script to check multiple addresses:

#!/bin/bash
MAPS=$(postconf virtual_alias_maps | cut -d= -f2 | tr -d ' ')
for map in ${MAPS//,/ }; do
    echo "Checking $map:"
    postmap -q "$1" "$map"
done

Save as check_virtual.sh and run with:

./check_virtual.sh "tim@domain.com"

If you get no results:

  • Ensure the map file exists and is readable
  • Run postmap /etc/postfix/virtual to rebuild the hash if you recently edited the file
  • Check Postfix logs (/var/log/maillog or /var/log/mail.log) for errors

Given this /etc/postfix/virtual content:

# Virtual aliases
tim@domain.com     tim11@mailserver.domain.com
support@domain.com helpdesk@company.com

Querying would show:

$ postmap -q "tim@domain.com" hash:/etc/postfix/virtual
tim11@mailserver.domain.com

Postfix's virtual alias mechanism is commonly used for email forwarding and address mapping. When you need to verify whether an address exists in your virtual alias table and where it's routed, there are several command-line approaches available.

The most straightforward way is to use the postmap command with the -q query option:

postmap -q "tim@domain.com" hash:/etc/postfix/virtual

This will return the destination address if the alias exists. For example, if your virtual file contains:

tim@domain.com tim11@mailserver.domain.com

The command will output:

tim11@mailserver.domain.com

If you're unsure about the virtual alias file location, first check your Postfix configuration:

postconf virtual_alias_maps

Then use the returned file path with postmap:

postmap -q "tim@domain.com" hash:/path/returned/from/postconf

If you just want to quickly check the text file (before it's compiled to .db):

grep "^tim@domain.com" /etc/postfix/virtual

Remember that changes to the text file won't take effect until you run:

postmap /etc/postfix/virtual
postfix reload

Here's a simple bash script to check multiple addresses:

#!/bin/bash

VIRTUAL_FILE="/etc/postfix/virtual"
ADDRESSES=("tim@domain.com" "john@domain.com" "admin@domain.com")

for address in "${ADDRESSES[@]}"; do
    result=$(postmap -q "$address" hash:$VIRTUAL_FILE 2>/dev/null)
    if [ -n "$result" ]; then
        echo "$address → $result"
    else
        echo "$address not found in virtual aliases"
    fi
done

If you're getting no results but expect mappings:

  • Verify the virtual file exists at the path you're checking
  • Ensure you've run postmap on the file after making changes
  • Check file permissions (should be readable by postfix user)
  • Confirm you're querying the correct database type (hash: for most installations)

For systems using MySQL-backed virtual aliases:

postmap -q "tim@domain.com" mysql:/etc/postfix/mysql-virtual.cf

Where mysql-virtual.cf contains your database connection parameters.