How to Install and Configure the Postfix Mail Server on Ubuntu

What is Postfix?

Postfix is a free, open-source Mail Transfer Agent (MTA) that routes and delivers electronic mail. While full-scale enterprise email (like Exchange or Google Workspace) requires massive infrastructure, installing a lightweight Postfix server on your Ubuntu machine is the perfect solution for allowing your web applications, cron jobs, and monitoring scripts to automatically send outbound notification emails directly to administrators.

Step 1: Install the Postfix Package

Open your terminal and ensure your package lists are up to date. Install Postfix using the apt package manager:

sudo apt update

sudo apt install postfix -y

During the installation, a text-based configuration wizard will appear.

Step 2: Navigate the Installation Wizard

When the wizard prompts you for the “General type of mail configuration”, use your arrow keys to select Internet Site and press Enter. This tells Postfix to send and receive mail directly using SMTP.

Next, you will be prompted for the “System mail name”. This should be a Fully Qualified Domain Name (FQDN) that resolves to your server (e.g., mail.yourdomain.com). If you are only sending internal alerts, the default hostname is acceptable. Press Enter to complete the installation.

Step 3: Edit the Main Configuration File

The core settings for Postfix reside in the /etc/postfix/main.cf file. Open it in a text editor:

sudo nano /etc/postfix/main.cf

Locate the inet_interfaces directive. By default, it may be set to all, which tells Postfix to listen for incoming mail on all network interfaces. If your server is only used by local applications to send outbound alerts, it is much more secure to restrict it to localhost:

inet_interfaces = loopback-only

Ensure the mydestination directive correctly lists your server’s hostname and localhost so it knows which emails to deliver locally.

Step 4: Restart the Postfix Service

Save and close the configuration file. Whenever you make changes to main.cf, you must restart the Postfix service to apply them:

sudo systemctl restart postfix

You can verify that the service is running smoothly without errors by checking the status:

sudo systemctl status postfix

Step 5: Send a Test Email

To verify that Postfix is successfully routing mail to the outside world, you can use the sendmail command directly from the terminal. Run the following command (replacing the email address with your personal inbox):

echo "This is a test alert from Postfix." | sendmail -v [email protected]

The -v flag will output verbose debugging information. Check your personal inbox (and the spam folder). If the email arrives, your Ubuntu server is now fully capable of dispatching automated alerts!

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.