Build a Mail Server with Commodity Hardware and FreeBSD, Part 1

In the past, many SMBs were content to let their Internet Service Providers (ISPs) handle e-mail services. But now they're interested in running mail servers onsite, due to the widespread adoption of relatively inexpensive, high-speed, and always-on Internet access. Not to mention the ever-increasing amounts of e-mail being transmitted.

In this Recipe, I'll show you how to build a mail server for your SMB clients using simple, commodity PC hardware, the FreeBSD operating system, and several pieces of freely available open-source software. At first blush, this may appear to be a daunting task. But by following the steps in this Recipe, you'll find it's not difficult at all. In fact, you should be able to build the entire setup in just a couple of hours.

System builders who take advantage of this Recipe stand to increase their business with SMBs by fulfilling their need for low-cost, reliable, and on-site e-mail servers. Because the open-source software I recommend in this Recipe is well-suited to white box hardware, system builders have a wide variety of hardware they can use for such servers. Your customers will appreciate the control and security of a customized onsite solution, which can pay for itself in saved hosting fees.

This is Part 1 of a two-part Recipe. In this first part, I'll show you how to use FreeBSD, Postfix, and Dovecot to create a secure, basic e-mail server for a single domain. Then, in Part 2, I'll show how to add extended functionality—including spam detection, virus filtering, and Webmail access—to the basic server we'll build in this first part. Let's get started.

id
unit-1659132512259
type
Sponsored post

Ingredients: Software

Operating System: We'll use FreeBSD 6. Like Linux, FreeBSD is an open-source UNIX OS. But unlike Linux, which is, strictly speaking, just a kernel, FreeBSD was developed as an integrated OS. It is stable, secure, and designed for networking. FreeBSD also has excellent tools for remote management. In fact, several large Internet sites, including Yahoo, use FreeBSD for their heavy-duty networking tasks.

FreeBSD can be installed using a few different methods, but the one I suggest is to begin by booting from Disc 1 of the CD install set. You can either buy them from a plethora of vendors or download the ISO files from the FreeBSD.org site. Then burn them to disc using an application like Nero Ultra Edition on Windows, available directly from Nero.

FreeBSD 6.1 was released just as this article was going to press. While I used FreeBSD 6 in preparing this Recipe, my instructions apply to version 6.1 as well.

SMTP Service: Mail servers use the Simple Mail Transport Protocol (SMTP) to send and receive e-mail across the Internet. FreeBSD's default SMTP daemon or service is Sendmail. I prefer using Postfix in lieu of Sendmail. It's designed as a drop-in replacement that's more secure and simpler to configure.

E-mail Access: The system also needs something that lets users connect to receive e-mail from their mailboxes. The most common protocols for this are Post Office Protocol v3 and Internet Message Access Protocol v4 (POP3 and IMAP4, respectively). We'll use Dovecot, another open-source program written to provide better security than its predecessors. Dovecot implements the POP3 and IMAP protocols; it does not replace them.

Ingredients: Hardware

Here's the hardware we'll use for this Recipe.

While FreeBSD is available for several hardware platforms, I suggest you use common Intel or AMD PC hardware. FreeBSD makes efficient use of available resources, but handling e-mail can be demanding of system resources if you're serving a large number of users—especially when spam filters and virus filters are factored in. In light of this, I recommend the following minimum hardware:

Before you buy anything, I highly recommend consulting the hardware compatibility list in the FreeBSD FAQ from the FreeBSD Documentation Project site. You don't want to waste money on incompatible hardware. Thirteen Steps to Installing FreeBSD

Space limitations prevent me from going through the installation process in complete detail. In lieu of that, I refer you to the "Installing FreeBSD" section (Chapter 2) of the FreeBSD Handbook, which is available for free from the FreeBSD Documentation Project. You should bookmark the Handbook, as you'll find it to be a useful reference guide when working with FreeBSD. This Recipe will discuss the major steps in installation. Since the FreeBSD installer is menu-driven, you should be able to easily follow along.

Before going further, I need to explain the difference between "packages" and "ports" in FreeBSD. Packages are pre-compiled binary software applications that you can install using built-in management utilities, like pkg_add. Ports, by contrast, are applications you can download, compile from the raw source code, and then have installed. Packages are faster to install, because they are already compiled. Applications installed from ports take longer to install, but the end result is software optimized to run faster on your system. For a single domain server providing mail to a relatively limited number of users, the ease of using pre-compiled packages outweighs the performance gained by using ports. For this reason, I'll be discussing packages.

For the remainder of this Recipe, I use bold characters for file names and commands you'll need to type. Since FreeBSD is UNIX-based, commands and file names are case-sensitive. For example, Command with an upper-case C and command with a lower-case C are not equivalent. Finally, I use a percentage symbol (%) to represent the command prompt.

I'm assuming you'll use the entire hard disk for the installation, so any data already on the disk will be overwritten. Dual-booting FreeBSD with another OS is possible, but beyond the scope of this Recipe. Let's get started:

Three Steps to Configuring Postfix

Now that you've installed FreeBSD, it's time to work with Postfix. Although we have a basic Postfix installation already, you'll need to customize it for your specific server.

The Postfix configuration is stored in the file /usr/local/etc/postfix.main.cf. You should read through this file to become familiar with the many options you can configure. You'll need to make some edits to main.cf to customize it for your system. Note: Lines beginning with a hash mark ("#") are commented out; delete the hash mark to have Postfix read that line.

Here are three steps for customizing the Postfix installation:

Five Steps to Installing and Configuring Dovecot

Now you'll install and configure Dovecot to provide POP and IMAP services. Here's how:

% pkg-add "r dovecot
% cd /usr/local/etc
% cp dovecot.conf.sample dovecot.conf
#mail_extra_groups =
mail_extra_groups = mail
dovecot_enable="YES"

Three Steps to Creating Users Before your end users can send and receive e-mail, they'll need accounts on the server. Since this is a single-domain server and we're not using virtual domains, we will create system accounts in FreeBSD. Postfix and Dovecot will use these system accounts. Here's how to do it:

% adduser fred
% shutdown "r now

That's it! If you've followed the steps in this Recipe, you now have a basic e-mail server for a single domain.

This is Part 1 of a two-part TechBuilder Recipe. Part 2 looks at extending the basic e-mail server with Webmail access, antivirus scanning, and spam filtering.

DAVID MARKOWITZ is a computer network and integration consultant who serves small and medium businesses and home users in and around Philadelphia.