Install and Configure a Postfix Mail Server

34554

 

There are a number of reasons why you would want to set up your own Linux mail server. You are in a company that has needs for a more reliable mail solution than anything the competition has to offer. Your company has a very limited IT budget and can’t afford Exchange or the CALs involved. Or, maybe you just want to expand your repertoire of Linux skills. Regardless of why, knowing how to set up a mail server on a Linux machine is an important task any Linux admin should know. And of course, in the spirit of all things Linux, there are a number of ways you can go in order to get that mail server up and running. Over the years I have found Postfix to be one of the easiest to set up and most reliable to deploy in most organizations.

I have deployed Postfix servers in single-user environments and up to three hundred user environments. It works like a champ no matter the size. And in this article I am going to show you how to get that Postfix server up and running in no time flat.

NOTE: For the purposes of this article I will be installing Postfix on an Ubuntu Server (the release is 10.04 but can be applied to 9.10) and I will use the fake domain mail.mymail.com. You will, of course, need to substitute your own domain (which must be a FQDN). 

Installation

You will be shocked at how simple it is to install the Postfix mail server. All you have to do is follow these steps:

1) Open up a terminal window (or, if you are using a GUI-less server just log in).

2) Issue the command sudo apt-get install postfix.

That’s it! Of course, depending upon the current state of your distribution, the installation may or may not have to install some dependencies. But this will happen automatically for you. The installation will also automatically start the Postfix daemon for you. So as soon as installation is complete you can test to make sure you can connect to your Postfix server with the command:

telnet localhost 25

You should see something like this:

Trying 127.0.0.1…
Connected to www.mymail.com.
Escape character is ‘^]’.
220 localhost.localdomain ESMTP Postfix (Ubuntu)

Now you might want to first make sure you can also connect to your domain in the same way with the command:

telnet www.mymail.com 25

Of course you will use your own FDQN in the above command (instead of mymail.com). Hopefully you will see the same output you did when you used localhost. If not, you will have to check to make sure your domain is pointing to your server or that port 25 traffic can get to your server from your router, switch, or firewall. Those issues are beyond the scope of this article however.

Now it is time to start configuration.

Configuring Postfix

The Postfix mail server has one main configuration file /etc/postfix/main.cf. This is where you will do the bulk of your configurations. Open this file up in your favorite text editor (mine is Nano) and look for the following section:

myhostname =
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =
relayhost =
mynetworks =
mailbox_command = procmail -a “$EXTENSION”
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

This is the section of the configuration file you must focus on. And, believe it or not, there isn’t much to do. Below are the sections you need to configure:

myhostname: This is the hostname of your machine. But don’t put the full hostname. If your machine hostname is mail.mydomain.com you will only use mydomain.

mydestination: This parameter specifies what destinations this machine will deliver locally. The default is:

mydestination = $myhostname localhost.$mydomain localhost

You could also use something like what I have used in the past (for simplicity’s sake):

mydomain.com mydomain localhost.localdomain localhost

This call is up to you. Either way will work; but the latter line will help to avoid mailloops.

mynetworks: This line is a bit trickier. This entry will define authorized destinations that mail can be relayed from. You would think that adding your subnet here would work. Sometimes that is the case; sometimes not. You could go with a mynetworks entry that looks like:

mynetworks = 127.0.0.1/8

The above entry is a safe entry and defines local machines only.

You could also have an entry that looks like:

mynetworks = 127.0.0.1/8 192.168.100.1/24

The above entry would authorize local machines and your internal network addresses.

I have found, however, that the above entries will cause problems with relaying due to constantly changing dhcp addresses. Because of this I have used the following, specialized entry which will avoid this issue:

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

Now, if your mail server serves up mail to your entire domain, you will need to add another entry to that section above. That entry is:

mydomain = mydomain.com

Again, as in all configurations above, the mydomain.com will be substituted with your real domain.

Now, save that configuration file and restart your mail server with the command:

sudo /etc/init.d/postfix reload

Your mail server should be up and running.

Users

Since this is a Linux mail server, you will need to make sure you have a user name that corresponds with every email address you need. If your server has a GUI you can just use the GUI tool for this. If your server is a GUI-less server you can create users with the command:

sudo useradd -m USERNAME

Where USERNAME is the actual name of the user. The next step is to give the username a password with the command:

sudo passwd USERNAME

Again, where USERNAME is the actual username. You will be prompted to enter the new password twice.

Test Your Server

Go to an external source and send an email to one of your users on your new mail server. To find out if it worked you can log on as that user and use the Alpine command line email reader (you might have to install that first with the command sudo apt-get install alpine). If you do not see an email show up you will want to check the log file /var/log/mail.err which should give you some clues as to what is going wrong.

Final Thoughts

Setting up a mail server has never been easier. Not only is Postfix a simple to set up server, it’s also simple to administer, as well as simple to troubleshoot. I hope you have as easy a time as I have had setting up and administering Postfix. Next time around, we’ll add a few features to this Postfix server.