Postfix is a free mail server software designed for use on Unix family operating systems. It belongs to the class of message transport agents (MTAs), which carry out the transfer of e-mails between email clients of users. The Ubuntu Linux + Postfix bundle is extremely popular for organizing such mail servers. Their configuration will be discussed in the article below.
Preparing to Install Postfix
Before you begin installing Postfix, you need to complete several procedures to properly configure the resource. It is assumed that the Ubuntu Linux operating system is already installed and ready for operation on the server.
Set the correct host name
By default, Postfix uses the hostname of the mail server to identify itself when communicating with other forwarding agents. The host name can be of two types: a simple word or a fully qualified domain name (Fully Qualified Domain Name, FQDN). When does it apply?
A simple word host name is commonly used for personal computers. If you use Linux on your home PC, then you can name it, for example, linux , debian, ubuntu . The FQDN consists of two parts: the host name and the domain name. For example, mail.yourdomain.co .
Here mail is the host name, yourdomain.com is the domain name. FQDN is typically used for Internet servers, and it should be used when configuring Postfix to send mail. The FQDN form above is standard for email servers.
In order to find out the FQDN of your server, enter the following command in the Ubuntu terminal: hostname –f.
If the server does not yet have an FQDN, it can be set using the hostnamectl utility. sudo hostnamectl set - hostname your - fqdn .
After that, log out of the system account and log back in. You can see the changes using the hostname –f command .
Checking the system time
Passing through Postfix, mail receives a timestamp. To do this, the server checks its system time. This mark is also recorded in its Postfix log ( / var / log / mail.log). Therefore, before installing Postfix, the system time must be set correctly.
Use the date command to find out the time zone and current system time on the Ubuntu server: user @ mail: ~ $ date . Sun Dec 31 06:37:19 BST 2017.
Set DNS records for the mail server
- MX record. The MX record (from English “mail exchanger”) tells other forwarding agents that your mail.yourdomain.com server is responsible for sending mail in your domain. MX record @ mail.yourdomain.com.
- Record A. The A-record establishes a connection between the FQDN and the IP address: mail.yourdomain.com <IP address>.
- PTR record. The PTR record (from English “pointer record”) establishes feedback between the IP address and the FQDN. It is the opposite of an A record and is used for reverse DNS queries. <IP address> mail.yourdomain.com
All 3 entries are set on the side of your hosting provider. As a rule, the service provider sets them automatically, but if necessary, you can specify them manually using the interface of managing your hosting.
The feedback between the A record and the PTR record is used to block spam. Many messaging agents only accept mail if the server is indeed associated with a specific domain. It is necessary to specify a PTR record so that messages from your server do not fall into the spam folder with senders.
To find out the PTR record for a specific IP address, run the following command in the console: dig - x < IP > + short or host < IP > .
After the preparation is complete, begin the installation of Postfix.
Postfix installation and configuration
To download Postfix, run the following two commands in a terminal on your Ubuntu server:
- sudo apt-get update;
- sudo apt-get install postfix -y.
For a newly installed Postfix, configuration starts by choosing the type of mail configuration:
- No configuration - during the installation process, no parameters will be configured.
- Internet Site - Postfix will be configured to send e-mail to other mail servers and receive messages from them.
- Internet with smarthost - Postfix server will be used to receive emails from other mail servers, but emails will be sent through a relay server.
- Satellite system - a relay will be used to receive and send mail.
- Local only - email will be forwarded only within the local account.
Next, enter your domain name as the name of the mail system, that is, what goes in the mail address after the @ symbol. For example, if your email address is info@yourdomain.com , then enter yourdomain.com as the name of the mail system.
Postfix setup in Ubuntu is now complete.
After installation, the Postfix server will automatically start and the configuration file /etc/postfix/main.cf will be generated in the / etc directory . Now we can check the version of Postfix with the following command:
user @ mail: ~ $ sudo postconf mail_version
mail_version = 2.11.0
We can also find out with the netstat utility that the main Postfix process is listening on TCP port 25: sudo netstat –lnpt.
Before sending the first test letter, it will not be superfluous to check whether port 25 is blocked by a firewall. You can use nmap utility to scan open ports. Run the following command in the terminal on some other Linux computer (for example, on your PC), substituting the real IP of your mail server into it: sudo nmap <your server IP>.
Typically, port 25 is open, as it is the standard port for email. If it is closed, you should make changes to the iptables firewall settings on the server. In this case, you need to allow incoming and outgoing connections to this port. If it is blocked by your hosting provider, contact a representative and ask to open it.
Sending a test letter
Actually, now we can send and receive letters in the Ubuntu console. If your user account on the server is called user , your mailing address will be user@yourdomain.com . As a test, you can send a letter to the resource administrator (root user) or to any Gmail, Yandex mail address and so on.
When Postfix is installed, the binary file agent sendmail is written to the / usr / sbin / sendmail directory. We can use it to send a test letter to Gmail, for example: echo "test" | sendmail youraccount @ gmail . com
This simple command tells sendmail to read the message from standard input and create the body of the email with the text “test”, and then send it to the specified Gmail email address. A letter with this text should be sent to your Google mailbox. Please note that you do not need to specify the sender address: it will automatically be inserted into the Postfix message metadata, during the configuration of which we set the name of the mail system.
Now let's try to reply to this message to check how Postfix receives messages. Incoming messages arriving at your mail server are stored in the / var / spool / mail / <username> and / var / mail / <username> directories. You can also find out the location of incoming emails with the command: postconf ail_spool_directory.
The Postfix message log is located in the /var/log/mail.log file.
Install and configure spam filter
In Postfix, to configure the spam filter, install spamassassin and spamc: apt-get install spamassassin spamc
SpamAssassin is a spam filter that will filter out unwanted mail on your server in accordance with the specified settings. Consider them.
The main configuration file for the spam filter is /etc/mail/spamassassin/local.cf , which can be opened using any text editor that is convenient for you. In particular, the following settings are considered significant for filtering, which you need to add or uncomment if necessary:
report_safe 0
required_score 8.0
rewrite_header Subject [SPAM]
- The report_safe parameter is recommended to be set to 0. In this case, incoming spam will receive the mark specified by the rewrite_header parameter in the header . If you set the parameter value to 1, then messages will be deleted.
- The required_score parameter is responsible for the sensitivity of the spam filter. The lower its value, the stricter the mail is filtered. For large mail servers serving more than a hundred accounts, the value of required_score is recommended to be set between 8.0 and 10.0.
Save the configuration file, and then enable and run the spam filter and update its configuration:
# systemctl enable spamassassin
# systemctl start spamassassin
# sa-update
Postfix and SpamAssassin Integration
In order to effectively integrate Postfix with a spam filter, you need to create a separate user and group for the spam filter process:
# useradd spamd -s / bin / false -d / var / log / spamassassin
Next, add the following line to the end of the /etc/postfix/master.cf configuration file:
spamassassin unix - nn - - pipe flags = R user = spamd argv = / usr / bin / spamc -e / usr / sbin / sendmail -oi -f $ {sender} $ {recipient}
After that, at the beginning of the file, indicate that spamassassin will work as a content filter ( content_filter parameter):
-o content_filter = spamassassin
Finally, restart Postfix to apply the changes:
# systemctl restart postfix
Spam filter setup is complete.
In order to test the functionality of SpamAssassin, you can run the following test. Send an email from another mail server (for example, Gmail or Yandex) to the email address on your server. Give it any title, and in the message body, enter:
XJS * C4JDBQADN1.NSBN3 * 2IDNEN * GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL * C.34X
When sending the above text to your server, for example, from a Gmail account, the following response will be received:
Verify SpamAssassin Detecting Spam Mails
Another message will be written to the log, which can be viewed using the following command:
# journalctl | grep spam
The log message contains the text: Monitor SpamAssassin Mail Logs
Additionally, you can check spamassassin directly from the console: # spamassassin - D </ usr / share / doc / spamassassin -3.4.0 / sample - spam . txt
The above command produces a fairly detailed result, which should include the line below: Test SpamAssassin Spam from Commandline.
Conclusion
By setting up your own private e-mail server, you can personally ensure the privacy of your e-mail correspondence, because only you, the server administrator, will have access to the messages stored on the server. On the other hand, creating a personal mail server requires a lot of effort.
By following the recommendations in the article, you can install and correctly configure the mail server based on Ubuntu Linux and Postfix to receive and send messages, as well as to filter spam. The instructions are valid for versions of Ubuntu starting from 12.04.