Mail server

How to setup a Postfix mail server on Debian

Email is one of the most basic methods of messaging used since the development of the Internet. This has led to the development of many messaging tools and protocols. There are protocols for the compatible operation of systems with different architectures.

Mail servers perform tasks such as not losing users’ emails and helping users access their emails while online. Tools that deliver e-mail over local intranets and the Internet are called Mail Transfer Agents (MTAs).


Here’s how to install and configure a Postfix mail server on a Debian Linux installation.

Step 1: Installing Postfix mail server on Debian

In many cases, Postfix comes to the fore because of its ease of use, few parameters and high number of uses. Use the following command to install Postfix on your Debian-based Linux distribution:

Internet Sitesudo apt install postfix

If Sendmail is installed on your system, it would be best to uninstall it before installing Postfix.

sudo apt remove sendmail

During installation, Postfix will ask you a few questions. Select Website for the first incoming request and enter the desired domain name in the second prompt.

Like most Linux software, Postfix stores its settings in files. This is great because you can make any changes you want to Postfix just by editing text files. Postfix settings are usually located in the case. Just to be safe, you should always create a backup of the default case.

Step 2: Configurations for Postfix Mail Server

First, open the file with a text editor of your choice and modify its contents as desired. There is a lot of basic information inside the file, such as my field (domain name of the machine) and myhostname (full name of the SMTP server). Adjust the settings to make the server work. You can refer to the example below to get an idea.

vim /etc/postfix/

Step 3: Sending mail with Postfix

To send your first mail using the Postfix server, use the commands below. These commands add your IP address and domain name to the hosts case.

hostnamectl set-hostname
echo " fatih.lab mail.fatih.lab" >> /etc/hosts

Reboot your system to make sure the settings take effect. After restarting, check the contents of your hostname file with the command below.

hostname && cat /etc/hostname && hostname -s

Then check your port number using the following command to make sure everything is in order:

netstat -tlpn

You can now compose your first message and see how things go.

Send your first email with the command below:

echo "my first mail content" | mail -s "Hello Postfix" root

After this step, open the Maildir folder specified in the file to check if everything is fine. If all went well, the email you sent should be here.

cat /Maildir/new/[Press Tab]

Step 4: IMAP and Dovecot Setup

If you’ve ever had a mail server service, you’ve probably heard of mail protocols. These protocols are POP and IMAP. The choice you make has a huge impact on your email send, receive, and forward flows. While POP (Post Office Protocol) is the most popular type of email protocol, IMAP (Internet Message Access Protocol) is the protocol of choice for most users these days.

Below is an example of sending mail with Postfix and IMAP. But before that, it is important to have at least a superficial knowledge of IMAP.

IMAP acts as a bridge between your mail client and your mail server. Mail servers allow you to send and receive emails. However, IMAP lets you keep your emails on the server until you manually delete them. When you connect to an email client, such as Gmail or Outlook, the client uses IMAP to connect to the email server.

In addition, IMAP has many advantages. For example, you can access your emails from as many different devices as you want and from anywhere. It also downloads the relevant message only when you click on it.

Email attachments are not downloaded automatically with IMAP. So you can check your messages much faster and control which attachments you want to open.

As the world moves more and more towards mobile, IMAP naturally becomes more popular. The proliferation of smartphones, laptops, tablets, and other devices is making the demand for IMAP more intense than ever.

Dovecote configuration

With Dovecot it is possible to send network mail transfers using POP3 or IMAP. The first thing you need to do to make it work is to install it on your system with the following command:

sudo apt install dovecot-core dovecot-imapd

After installation, there will be files in the /etc/dovecote case. You need to make various adjustments to these files and configure Dovecot.

First, you need to establish a connection between your Maildir file and dovecote. The mail address line specifies the folder to store the mails. Replace the expression here with Maildir as follows. To do this, open the 10-mail.conf file with any editor you want.

vim /etc/dovecot/conf.d/10-mail.conf

And change the line starting with mail address at:

mail_location = maildir:~/Maildir

You will now establish a connection between Postfix and Dovecot. To do this, open the 10-master.conf file in the same way and modify the codes under the Postfix smtp-auth comment:

vim /etc/dovecot/conf.d/10-master.conf

Edit the following lines in the file:

unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix

Dovecot runs on port 143 by default. To check this, restart the dovecot service and check the port number with the command below:

netstat -tlpn

Now that everything is fine, it’s time to send a real email. To do this, create a new user using the commands below:

adduser testuser

Then connect to port 25 using the nc command.

nc localhost 25

The contents of your terminal’s login screen may vary slightly, but that’s normal. You can use CTRL+C to exit and try again if you encounter an error. The command you should use at this point is:

ehlo localhost

You are now at the step of editing the content of the email. The command structure you need to enter at this point is:

mail from: root
rcpt to: testuser
subject: testsubject
my mail content

There is one last place to check if the email transmission was successful. By configuration, this email must be in the Maildir folder. To do this, check if the email you sent exists in this folder with the following command:

cat /home/testuser/Maildir/new/[Press Tab]

As you can see, with Dovecot IMAP and Postfix you have successfully sent the desired content to testuser.

Why use Postfix on Linux servers?

As you can see from the examples, using Postfix is ​​very convenient and easy. Postfix is ​​very flexible for mail server administrators and developers. Moreover, it is compatible with many tools such as SQL, MySQL, Cyrus, LDAP, SASL, TSL and SSL. Compared to other MTAs, it performs better even on high traffic systems and with multiple users.

Postfix works quite well not only on Linux, but also on Unix and Windows. Even technology companies with high-tech servers such as Microsoft, Google, and Amazon have used Postfix in many areas. In addition, it is much more efficient in terms of security, especially compared to Sendmail. For these reasons, Postfix’s mail architecture is still preferred by mail server administrators even after 20 years.