How to redirect all mail to one user in Postfix

Better Stack Team
Updated on December 21, 2023

To redirect all incoming mail to one user in Postfix, you can use the virtual alias map feature. This allows you to define a mapping for all incoming emails to be redirected to a specific user or email address. Here's how you can do it:

1. Edit the Postfix Virtual Alias Map Configuration

Open the Postfix main configuration file in your preferred text editor. The location of this file can vary depending on your system, but it's typically located at /etc/postfix/main.cf. You may need superuser privileges to edit this file, so use sudo or su if necessary.

 
sudo nano /etc/postfix/main.cf

2. Add or Modify the Virtual Alias Map Configuration

Add the following lines at the end of the main.cf file to specify the redirection:

 
virtual_alias_maps = hash:/etc/postfix/virtual

This line tells Postfix to use the /etc/postfix/virtual file as the source of virtual alias mappings.

3. Create or Edit the Virtual Alias File

Next, create or edit the virtual alias file (/etc/postfix/virtual). In this file, specify the email address you want to redirect and the target email address to which you want to redirect the mail. For example:

 
user@example.com   redirect@example.com

This means that all emails sent to user@example.com will be redirected to redirect@example.com.

4. Create the Hash Table for Virtual Alias Maps

After editing the virtual alias file, you need to create a hash table to make Postfix use the virtual alias mappings. Run the following command to build the hash table:

 
sudo postmap /etc/postfix/virtual

5. Reload Postfix

To apply the changes, reload the Postfix configuration:

 
sudo postfix reload

Test the Configuration

Send an email to the email address you specified in step 3 (user@example.com in the example). It should be redirected to the target email address (redirect@example.com).

Please replace the example email addresses with your actual addresses. This configuration will redirect all incoming emails to the specified user or email address.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github