How To Install and Setup Postfix on Ubuntu

Postfix is one of the most popular Mail Transfer agent nowadays. It comes as free and open source software package. Recent studies have showed that at least 25% of the mail servers on internet are using Postfix.

In order to provide best functionality and avoid getting your email server on a lot of spam lists , the first thing we need to do is to assign Fully Qualified Domain Name to your server and set the appropriate DNS records to point to it. Since we are installing email server, it is important to configure MX records on our DNS provider and point to our server.

There are many ways to install postfix, a) we can download its source code, compile and install, b) we can get pre-compiled .deb file for our operating system, or c) we can do it the easiest way using apt-get (recommended).

Prepare System

We need to make sure that our repositories and our operating system are up-to-date before proceeding with the install. There is no harm by running this commands (in fact it is recommended to keep your system up-to-date all the time, so you can execute these command every day if you like):

# apt-get update
# apt-get upgrade

Install Postfix

The next thing we need to do is to install postfix by executing:

# apt-get install postfix

In the older Ubuntu releases, postfix is installed without need of user’s interaction. However, if you are using one of the latest releases, during installation we will be asked which installation type we would prefer. In this dialog, we will select “Internet site” and enter the domain name for our server. After we do that, postfix is installed but we still need to make some configuration changes before we can start using our new email server.

Configure Postfix

By default, Postfix configuration files are located in /etc/postfix. The most important files are main.cf and master.cf. You don’t need to edit master.cf file (unless you are a postfix guru and feel very confident doing it). All configuration that we need to change is in main.cf file. Use your favorite text editor to open and edit /etc/postfix/main.cf file and make sure you set the following variables:

myhostname = your.domain.com

This variable defines who can use the server to send out emails. Make sure that it looks like in the example, which means only the server itself is allowed to send emails. If we open our server to the world without having additional authentication system installed, we will be identified as spammers within a few days.

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

We can use postfix to serve emails for more than one domain. If that is the case, the domains should be listed here, separated by commas:

mydestination = example1.com, example2.com

For more information regarding all postfix variables which can be configured, you can refer to the postfix documentation.

After we set this variables we need to apply the configuration, simply by reloading the postfix daemon:

# service postfix reload

Once this command is executed, postfix is ready to send and receive emails. However, if we want to be able to use our favorite email client to download our messages we must install POP3 or IMAP server. Dovecot would be a good choice for that, since it is also open source and very popular.

Testing Postfix

In order to test if postfix is up and running we can run the following command, you should see output similar to the one below:

If you see that, you have successfully setup Postfix on your Ubuntu server and you can now use it as your mail transfer agent (MTA). Be sure to follow us on social networks for the follow up of this article to setup Dovecot.