How-To: Postfix and Virtual Hosts

2 minute read

Postfix comes as the default mail server under ubuntu. By default, it is set such as it can basically send system notice mail to the user you predefined during install.

During this how-to, I’m going to explain how you can set virtual domains that will forward your mail back to another email adress of yours.

This how-to will go through:

  1. Installation of postfix.
  2. Explain the default settings and show how-to configure a virtual host

Installation of POSTFIX

Postfix is usually shipped with any Linux Distribution. Under a Debian based system, you just have to run:

[email protected]:~#apt-get install postfix

This is all you need for a basic setting. People who want to know more, might give a try to:

[email protected]:~# apt-cache search postfix | grep ^postfix
postfix - A high-performance mail transport agent
postfix-dev - Postfix loadable modules development environment
postfix-doc - Postfix documentation
postfix-ldap - LDAP map support for Postfix
postfix-mysql - MYSQL map support for Postfix
postfix-pcre - PCRE map support for Postfix
postfix-pgsql - PGSQL map support for Postfix
postfix-gld - greylisting daemon for postfix, written in C, uses MySQL

POSTFIX Default settings

There is 3 main files that need to get your attention:

  • /etc/mailname : the visible mail name of the system
  • /etc/aliases : Postfix local alias database format
  • /etc/postfix/main.cf : Postfix configuration parameters

The actual main.cf installed is a really stripped down version of the parameters used by postfix.

Let’s go into it. First

/etc/mailname : This is where you set the domain name of the system, has seen by the other. It needs, in most cases, to look like a real domain name, otherwise, the next smtp server on the road might refuse the mails originating from your machine.

In this example, I choose debuntu.local.

/etc/aliases :

The place you define aliases. For instance, it is quite good to redirect all mail to root to your normal user.

Here:

# Added by installer for initial user
root: myuser

From now on, you will be able to get system notice using mutt with your myuser user.

/etc/postfix/main.cf :

The place we are going to make a few changes. Let’s say I want to be able to relay mail sent to user at mydomain.org to my personnal adress [email protected]

In the first place I need to define a virtual alias domain.

virtual_alias_domains = mydomain.org

Then, we need to tell postfix where the alias database is:

virtual_alias_maps = hash:/etc/postfix/virtual

Adding those 2 lines is sufficient to make your box treat the mail sent to debuntu.org. Now, we need to tell postfix what to do with the mails.

Create and Edit the file /etc/postfix/virtual and add:

[email protected] [email protected]
[email protected] myuser
@mydomain.org [email protected]

At the first line, we say that we want all the mail to [email protected] to be forwarded to [email protected]

On the second line we tell postfix to deliver the mails to [email protected] to the Unix user myuser.

On line 3, we define a catch-all adress which will forward any mails to mail account to the mail account [email protected].

This is it!

Now we need to regenerate the aliases database as well as the virtual mail aliases dataase. To do so, execute the following commands:

# newaliases
# postmap /etc/postfix/virtual

And restart postfix:

# /etc/init.d/postfix restart

And you are done :).