CentOS - Mail Server - Adding Domains and Users

Following from the previous article, we should have our firewall properly configured to allow the correct ports for our mail server.

Now we can start adding domains and users to our Postfix installation.

As discussed in the initial article where we configured the basic settings for our mail server, Postfix is modular in nature and many of the settings refer to other files on the slice.

Following along with this concept, we will be using a separate file that contains our users as well as one for the domains we will be hosting mail for.


Let's start by making the necessary file references in our Postfix configuration.

Go ahead and open the main.cf file so that we can make our changes:

sudo nano /etc/postfix/main.cf

Now under the MyDestination you should see this:

mydestination = $myhostname, localhost.$mydomain, localhost

This setting tells Postfix what domains it should accept mail for.

As we will be hosting mail for multiple domains we want to reference them all in one file.

To accomplish this, we set the mydestination line as follows:

mydestination = hash:/etc/postfix/mydomains


The same idea applies for our users, so with this file still open look for the alias_maps setting.

Notice this parameter is currently set to the /etc/aliases file as shown below:

alias_maps = hash:/etc/aliases

This line tells Postfix to look in this file for our users when receiving incoming mail. If the recipient is not present, the message will not get delivered as expected.

Editing the /etc/aliases file works great for one domain, but we want this configuration to work for multiple domains.

Therefore, let's add a new line under the alias_maps setting:

virtual_alias_maps = hash:/etc/postfix/virtual

Ok, now let's save the file and move forward with specifying our domains and users.


Before we can restart Postfix to take notice of the changes we have made, we need to actually create the mydomains file that we specified for the mydestination setting above.

sudo nano /etc/postfix/mydomains

Remember this is the file where we state which domains Postfix should accept mail for. Therefore, let's add the following:

localhost                 OK
mail.demoslice.com        OK
demoslice.com             OK

Anytime you make changes to this file the following command must be ran:

sudo /usr/sbin/postmap /etc/postfix/mydomains

Now let's do the same for our virtual file we are going to use to specify our users.

Virtual File

We start by creating the virtual file in the location we referenced in the Postfix configuration.

sudo nano /etc/postfix/virtual

And add your user as shown below:

demo@demoslice.com   demo

This tells Postfix to route mail intended for demo@demoslice.com to the system user titled 'demo' on the slice.

We also need to run the postmap command on this file when making changes as it recreates the database that Postfix parses:

sudo /usr/sbin/postmap /etc/postfix/virtual



Anytime we make edits to the main Postfix configuration file, we need to restart Postfix to incorporate these changes :

sudo /etc/init.d/postfix restart

All Done.


Adding domains and multiple users is now a simple process through the use of our mydomains and virtual files.

Now that we have completed all of the necessary steps to setup a fully working mail server on our slice, we can actually start to make use of it for its intended purpose.

Article Comments:

Sam commented Tue Jan 05 14:13:50 UTC 2010:

How would I go about changing a users password?

CC commented Tue Feb 16 23:04:55 UTC 2010:

How do you add aliases for the email address

for example, the only line you show above is demo@demoslice demo

If you also wanted Mrdemo demo, demos demo, etc. Where would you put it? in /etc/aliases or in the file above.

Jered commented Wed Feb 17 15:42:53 UTC 2010:

You can do that in the "virtual" file referenced in the article, following the same format as in the example (new lines for each new alias).

You actually can use /etc/aliases if you're only forwarding to local users, but the /etc/postfix/virtual file is a better choice if you're following the articles. That will make things easier for handling multiple domains later.

CC commented Thu Feb 18 09:56:18 UTC 2010:

To define demo as the catchall would you type the following?

@demoslice.com demo

Jered commented Fri Feb 19 16:21:13 UTC 2010:

Yes, CC, that should work for the catchall.

seanl commented Mon Apr 12 12:27:25 UTC 2010:

Is it possible to use something like the above virtual setup to forward mail to non local accounts ie for say multiple domains (contact pages) on the same box sending to an external mail service (google apps)

Adam commented Wed Nov 21 22:25:07 UTC 2012:

Why not give your more advanced users, or even the ones that want to learn, a write up on setting up Virtual Domains with Postfix, Dovecot and MySQL?

Want to comment?

(not made public)


(use plain text or Markdown syntax)