Basic security settings
Before we look at the advanced security measures, we should make sure that the basic settings are correct. This includes restricting access to the Postfix server. In the file /etc/postfix/main.cf
you should add or adjust the following lines:
inet_interfaces = loopback-only mynetworks = 127.0.0.0/8 [::1]/128
These settings restrict access to the local host and prevent the server from being misused as an open relay. An open relay can be used by spammers to send unwanted emails, which can severely damage the reputation of your server. It is therefore crucial to carry out this basic protection.
Activate TLS encryption
The use of TLS (Transport Layer Security) is essential to ensure the confidentiality of e-mail communication. Add the following lines to the main.cf
-file:
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key smtpd_tls_security_level = may smtp_tls_security_level = may
These settings activate TLS for incoming and outgoing connections. Make sure that you use valid SSL certificates, ideally from a trusted certification authority (CA). A correctly implemented TLS protects your emails from interception and manipulation during transmission. Further information on TLS configuration can be found in the official [Postfix documentation](https://www.postfix.org/TLS_README.html).
Set up SASL authentication
The Simple Authentication and Security Layer (SASL) provides an additional layer of security. Add these lines to the main.cf
added:
smtpd_sasl_type = dovecot smtpd_sasl_path = private/auth smtpd_sasl_auth_enable = yes smtpd_sasl_security_options = noanonymous smtpd_sasl_local_domain = $myhostname
This configuration assumes that you are using Dovecot as your SASL provider. Adjust the settings accordingly if you are using a different provider. SASL authentication prevents unauthorized users from sending emails via your server, which significantly increases security.
Protection against denial-of-service attacks
To protect your server from overloading, you can set connection limits. Add these lines to the main.cf
added:
smtpd_client_connection_rate_limit = 50 smtpd_client_message_rate_limit = 100 anvil_rate_time_unit = 60s
These settings limit the number of connections and messages that a client can send per minute. By limiting this, you can prevent your server from being overloaded by mass requests or spam emails. This is an important step to ensure the availability of your mail server.
Implement HELO/EHLO restrictions
Many spam senders use invalid or falsified HELO/EHLO host names. You can block such connections with the following settings:
smtpd_helo_required = yes smtpd_helo_restrictions = permit_mynetworks, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname
These rules require a valid HELO/EHLO hostname and reject connections with invalid or incompletely qualified domain names. This makes it more difficult for spammers to send spoofed emails as they must provide correct HELO/EHLO information.
Introduce transmitter restrictions
To prevent misuse of your server, you can set restrictions for senders:
smtpd_sender_restrictions = permit_mynetworks, reject_non_fqdn_sender, reject_unknown_sender_domain, reject_unauth_pipelining
These rules reject emails from incompletely qualified sender addresses or unknown sender domains. This reduces the likelihood of your server being used for spam or phishing, while improving the overall quality of emails received.
Configure recipient restrictions
Similar to the sender restrictions, you can also define rules for recipients:
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination, reject_non_fqdn_recipient, reject_unknown_recipient_domain
These settings prevent your server from being misused as a relay for unauthorized destinations and reject emails to invalid recipient addresses. This further increases the security of your server and at the same time ensures the integrity of email communication.
Implement greylisting
Greylisting is an effective method for reducing spam. First install the Postgrey package:
sudo apt install postgrey
Then add the following line to the main.cf
added:
smtpd_recipient_restrictions = ... (existing settings) check_policy_service unix:private/postgrey
This configuration first forwards incoming emails to the Postgrey service, which generates temporary rejections for unknown senders. Email servers that send legitimate emails retry delivery after a delay, effectively eliminating spam senders who often only attempt to send once.
Activate SPF check
The Sender Policy Framework (SPF) helps to prevent e-mail spoofing. First install the required package:
sudo apt install postfix-policyd-spf-python
Then add these lines to the main.cf
added:
policyd-spf_time_limit = 3600s smtpd_recipient_restrictions = ... (existing settings) check_policy_service unix:private/policyd-spf
This configuration activates the SPF check for incoming emails. SPF checks whether the email was sent from an authorized server for the specified domain, which helps to prevent spoofing and increase the credibility of your email communication.
Implement DKIM signing
DomainKeys Identified Mail (DKIM) adds a digital signature to outgoing emails. First install OpenDKIM:
sudo apt install opendkim opendkim-tools
Then configure OpenDKIM and add these lines to the main.cf
added:
milter_protocol = 2 milter_default_action = accept smtpd_milters = unix:/var/run/opendkim/opendkim.sock non_smtpd_milters = unix:/var/run/opendkim/opendkim.sock
These settings activate DKIM signing for outgoing emails. DKIM increases security by ensuring that emails have not been changed unnoticed and strengthens trust in the authenticity of messages.
Set up DMARC guidelines
Domain-based Message Authentication, Reporting and Conformance (DMARC) is based on SPF and DKIM. Add a DMARC DNS entry for your domain and install OpenDMARC:
sudo apt install opendmarc
Configure OpenDMARC and add this line to the main.cf
added:
smtpd_milters = ... (existing settings), inet:localhost:8893
This configuration enables DMARC checking for incoming emails. DMARC allows domain owners to set policies on how receiving servers should handle failed SPF or DKIM checks and provides detailed reports on email authentication.
Regular updates and monitoring
Security is an ongoing process. Make sure that you update your Postfix system regularly:
sudo apt update sudo apt upgrade
Also monitor the Postfix logs for suspicious activity:
tail -f /var/log/mail.log
Regular updates close known security gaps and improve the stability of your mail server. Continuous monitoring of the logs enables you to detect unusual activities at an early stage and react to them quickly.
Additional safety measures
In addition to the basic and advanced security measures, there are additional steps you can take to further increase the security of your Postfix server:
Firewall configuration
Make sure that your firewall has only opened the necessary ports for the mail server. Typically, these are port 25 (SMTP), port 587 (submission) and port 993 (IMAP via SSL). Use tools such as ufw
or iptables
to control access to these ports and block unwanted connections.
Intrusion Detection Systems (IDS)
Implement an intrusion detection system such as Fail2Ban
to detect repeated failed login attempts and automatically block IP addresses that show suspicious behavior. This reduces the risk of brute force attacks on your mail server.
Backups and restoration
Carry out regular backups of your configuration files and important data. In the event of a security incident, you can restore quickly and minimize service interruptions. Store backups in a secure location and regularly check the integrity of the backup data.
User and rights management
Manage user accounts carefully and only assign the necessary rights. Use strong passwords and consider implementing multi-factor authentication (MFA) to further secure access to the mail server.
Best practices for the maintenance of Postfix
Ongoing maintenance of your Postfix server is critical to maintaining security and performance. Here are some best practices:
- Regularly check the configuration: Regularly check your
main.cf
and other configuration files to ensure that all security measures are implemented correctly. - Log analysis: Use tools to automatically analyze your mail logs to quickly identify anomalies and potential security incidents.
- Software updates: Update not only Postfix, but also all dependent components such as Dovecot, OpenDKIM and OpenDMARC regularly.
- Monitoring and alerts: Implement a monitoring system that notifies you of unusual activities or error messages.
Avoid common errors in the Postfix configuration
When configuring Postfix to maximize security, there are common mistakes that should be avoided:
- Open relay: Make sure that your server is not configured as an open relay by setting the
inet_interfaces
andmynetworks
-settings correctly. - Invalid TLS certificates: Always use valid and up-to-date SSL certificates in order to use TLS encryption effectively.
- Missing authentication: Activate SASL authentication to prevent misuse of your server.
- Insufficient rate limits: Set appropriate connection limits to prevent denial-of-service attacks.
- Missing SPF/DKIM/DMARC: Implement comprehensive email authentication methods to ensure the integrity and authenticity of your emails.
Summary
Configuring Postfix for maximum security requires careful planning and regular maintenance. By implementing the measures described in this article, you can significantly improve the security of your email server. Remember that security is an ongoing process. Stay up to date on new threats and best practices to keep your Postfix server protected. Use the resources and communities available to educate yourself and stay on top of the latest technology.
For further information and detailed instructions, please visit the official [Postfix documentation](https://www.postfix.org/documentation.html) and other trusted sources in the field of email security.