This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
computing:mailserver [2022/12/25 20:44] – oemb1905 | computing:mailserver [2025/04/23 20:55] (current) – oemb1905 | ||
---|---|---|---|
Line 11: | Line 11: | ||
------------------------------------------- | ------------------------------------------- | ||
- | This tutorial is for users of Debian GNU/Linux who want to set up a proper email server.. This tutorial assumes you know how to set up A, AAAA, SPF, DKIM, DMARC, MX, and PTR records. Set an A record for example.org and mail.example.org. If you don't know how, then learn up, and do not proceed. //Thanks to LinuxBabe for a great jumping off point//. | + | This tutorial is for users of Debian GNU/Linux who want to set up a proper email server.. This tutorial assumes you know how to set up A, AAAA, SPF, DKIM, DMARC, MX, and PTR records. Set an A record for example.org and mail.example.org. If you don't know how, then learn up, and do not proceed. //Thanks to LinuxBabe for a great jumping off point// |
- | | + | |
+ | 127.0.0.1 mail.example.org localhost | ||
- | Edit the second line and add a line to the bottom similar to: | + | Install postfix and mailutils |
- | + | ||
- | < | + | |
- | < | + | |
- | + | ||
- | Install postfix and mailutils | + | |
- | + | ||
- | | + | |
- | <Internet Site> | + | |
- | <example.org> | + | |
Install firewall, open common ports for front facing website, and for imap/smtp: | Install firewall, open common ports for front facing website, and for imap/smtp: | ||
Line 30: | Line 22: | ||
sudo apt install ufw | sudo apt install ufw | ||
sudo ufw allow 22/tcp | sudo ufw allow 22/tcp | ||
- | sudo ufw allow 53/tcp | ||
sudo ufw allow 25/tcp | sudo ufw allow 25/tcp | ||
sudo ufw allow 587/tcp | sudo ufw allow 587/tcp | ||
sudo ufw allow 143/tcp | sudo ufw allow 143/tcp | ||
+ | sudo ufw allow 465/tcp | ||
sudo ufw allow 993/tcp | sudo ufw allow 993/tcp | ||
sudo ufw allow 80 | sudo ufw allow 80 | ||
Line 42: | Line 34: | ||
sudo postconf -e message_size_limit=52428800 | sudo postconf -e message_size_limit=52428800 | ||
- | Set hostname and aliases | + | Set hostname and aliases |
- | + | ||
- | | + | |
- | + | ||
- | Make sure that the hostname, origin, destination, | + | |
| | ||
myhostname = mail.example.com | myhostname = mail.example.com | ||
Line 121: | Line 109: | ||
smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 | smtp_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1 | ||
- | Now, we can install dovecot and configure | + | Now, let' |
- | sudo apt install dovecot-core dovecot-imapd | + | |
- | | + | smtpd_sasl_auth_enable = yes |
- | < | + | smtpd_sasl_type = dovecot |
- | sudo nano / | + | smtpd_sasl_path = private/ |
- | < | + | smtpd_sasl_security_options = noanonymous |
- | sudo adduser dovecot mail | + | |
- | + | Now, we can install dovecot and configure it to use IMAP and lmtp. Install the packages with '' | |
- | We will now configure dovecot to use lmtp and in so doing use spam sieve and other modules: | + | |
- | sudo apt install dovecot-lmtpd | ||
- | sudo nano / | ||
< | < | ||
- | Now, we need to edit '' | + | After that, open '' |
+ | |||
+ | < | ||
+ | |||
+ | Let' | ||
+ | |||
+ | sudo adduser dovecot mail | ||
+ | sudo adduser username mail | ||
+ | |||
+ | Now we can configure dovecot over at '' | ||
service lmtp { | service lmtp { | ||
Line 146: | Line 140: | ||
} | } | ||
- | Similarly, we need to edit postfix for lmtp: | + | Similarly, we need to edit postfix for lmtp as well with '' |
- | | + | mailbox_transport = lmtp: |
- | <mailbox_transport = lmtp: | + | smtputf8_enable = no |
- | | + | |
- | Next, let's configure dovecot authorization: | + | Next, let's configure dovecot authorization |
- | | + | disable_plaintext_auth = yes |
- | <disable_plaintext_auth = yes> | + | auth_username_format = %n |
- | | + | auth_mechanisms = plain login |
- | | + | |
| | ||
- | Now, configure SSL/TLS encryption in dovecot: | + | Now, configure SSL/TLS encryption in dovecot |
- | | + | ssl = required |
- | <ssl = required> | + | ssl_cert = </ |
- | | + | ssl_key = </ |
- | | + | ssl_prefer_server_ciphers = yes |
- | | + | ssl_min_protocol = TLSv1.2 |
- | | + | |
| | ||
- | SASL configuration | + | Set up the SASL listener |
service auth { | service auth { | ||
Line 183: | Line 174: | ||
openssl s_client -starttls smtp -connect mail.example.com: | openssl s_client -starttls smtp -connect mail.example.com: | ||
| | ||
- | Now it is time to setup an spf policy agent so that the incoming email that is received checks for validity of spf records. **Do not confuse this with creating an spf TXT record for your outgoing email.** | + | Now it is time to setup an spf policy agent so that the incoming email that is received checks for validity of spf records. **Do not confuse this with creating an spf TXT record for your outgoing email.** |
+ | |||
+ | policyd-spf | ||
+ | user=policyd-spf argv=/ | ||
+ | |||
+ | After that, let's set up '' | ||
- | | + | policyd-spf_time_limit = 3600 |
- | sudo nano / | + | smtpd_recipient_restrictions = |
- | < | + | |
- | < | + | |
- | sudo nano / | + | |
- | <policyd-spf_time_limit = 3600> | + | |
- | | + | |
- | <permit_mynetworks, | + | |
- | <permit_sasl_authenticated, | + | |
- | <reject_unauth_destination, | + | |
- | <check_policy_service unix: | + | |
- | Now, it is time to set up DKIM on your server. After creating the DKIM record/key on your server, you will need to create a corresponding TXT record for it to establish that anything over smtp with that signature is, in fact, you/your server. | + | Now, it is time to set up DKIM on your server. After creating the DKIM record/key on your server, you will need to create a corresponding TXT record for it to establish that anything over smtp with that signature is, in fact, you/your server. |
- | | + | Canonicalization |
- | sudo adduser postfix opendkim | + | Mode sv |
- | sudo nano / | + | SubDomains |
- | <Canonicalization | + | Nameservers |
- | | + | KeyTable |
- | | + | SigningTable |
- | | + | ExternalIgnoreList |
- | | + | InternalHosts |
- | | + | |
- | | + | |
- | | + | |
Now that the configuration for DKIM is ready, let's create the keys and content for the locations specified above: | Now that the configuration for DKIM is ready, let's create the keys and content for the locations specified above: | ||
Line 216: | Line 204: | ||
sudo chown -R opendkim: | sudo chown -R opendkim: | ||
sudo chmod 711 / | sudo chmod 711 / | ||
- | sudo nano / | + | |
- | | + | Once all the directories and key locations are created, let's open the signing table with '' |
- | | + | |
- | sudo nano / | + | |
- | | + | |
- | sudo nano / | + | |
- | | + | Now that the signing table is setup, we need to edit the key table with '' |
+ | |||
+ | default._domainkey.example.com | ||
+ | |||
+ | The trusted hosts is next, over in '' | ||
+ | |||
+ | 127.0.0.1 | ||
+ | localhost | ||
+ | .domain.com | ||
+ | |||
+ | We now need to cut the DKIM keys (and make sure to add TXT records on your DNS host later) as follows: | ||
+ | | ||
sudo mkdir / | sudo mkdir / | ||
sudo opendkim-genkey -b 2048 -d example.com -D / | sudo opendkim-genkey -b 2048 -d example.com -D / | ||
Line 228: | Line 227: | ||
sudo chmod 600 / | sudo chmod 600 / | ||
- | It's now time to create | + | To get the information you need for the DNS record, you can run '' |
sudo opendkim-testkey -d example.com -s default -vvv | sudo opendkim-testkey -d example.com -s default -vvv | ||
| | ||
- | Note that that output will display "key not secure" | + | Note that that output will display "key not secure" |
sudo mkdir / | sudo mkdir / | ||
sudo chown opendkim: | sudo chown opendkim: | ||
sudo nano / | sudo nano / | ||
- | | + | |
- | | + | In the dkim config file, enter: |
- | | + | |
- | sudo nano /etc/postfix/main.cf | + | |
- | < | + | |
- | < | + | After establishing the socket directory and location, let's configure our dkim defaults in '' |
- | < | + | |
- | < | + | |
- | It's now a good time to test your email quality with [[https://mail-tester.com|Mail Tester]] to see if you got a 10/10 score. When upgrading | + | SOCKET=" |
- | | + | Lastly, we need to configure postfix to be able to use opendkim in '' |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | To set up email header and/or body checks to prevent spam: | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | + | ||
- | You will then need to configure the files with whatever strings you expect spam headers or bodies to have, and either reject them and/or discard them. You will also need to rebuild the indexes. | + | |
- | | + | In addition to spf and dkim policies, it's also best practice to have a dmarc policy. Let's install opendmarc with '' |
- | </free mortgage quote/ | + | |
- | </repair your credit/ | + | |
- | sudo postmap / | + | |
- | | + | |
- | </free mortgage quote/ | + | |
- | </repair your credit/ | + | |
- | sudo postmap / | + | |
- | In general, be careful of setting your own TXT records | + | AuthservID OpenDMARC |
+ | TrustedAuthservIDs mail.yourdomain.com | ||
+ | RejectFailures true | ||
+ | IgnoreAuthenticatedClients true | ||
+ | RequireHeaders true | ||
+ | SPFSelfValidate true | ||
+ | Socket local:/ | ||
+ | |||
+ | The opendmarc socket also needs its directory created, similar to dkim. So let's create a directory | ||
- | sudo apt install opendmarc | ||
- | <no to db configure> | ||
- | sudo nano / | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | <Socket local:/ | ||
- | sudo mkdir -p / | ||
sudo chown opendmarc: | sudo chown opendmarc: | ||
sudo chmod 750 / | sudo chmod 750 / | ||
Line 289: | Line 269: | ||
sudo systemctl restart opendmarc | sudo systemctl restart opendmarc | ||
| | ||
- | Now, configure postfix to work with openDMARC. | + | Now, we need to configure postfix to work with openDMARC. |
- | | + | milter_default_action = accept |
- | <milter_default_action = accept> | + | milter_protocol = 6 |
- | | + | smtpd_milters = local: |
- | | + | non_smtpd_milters = $smtpd_milters |
- | | + | |
- | sudo systemctl restart | + | It's about time to setup spam controls, but before we do that, we need to make sure mission critical IMAP folders populate on clients. Let's open '' |
+ | |||
+ | | ||
+ | **auto = create** | ||
+ | special_use = \Drafts | ||
+ | } | ||
+ | |||
+ | Since I use lmtp, which is case-sensitive, | ||
+ | |||
+ | virtual_alias_maps = regexp:/ | ||
| | ||
- | This about covers everything. The only missing part is how to get past picky microsoft users and/or automate or simplify account creation. Okay, to view and/or delete messages from postfix | + | After you enter that block, let's open the file '' |
- | | + | |
- | | + | |
- | | + | |
- | | + | It's super important to include the domain above after the regex or all emails you send to that begin with your user name will wind up in that user's inbox. When finished configuring, |
+ | |||
+ | | ||
+ | permit_mynetworks, | ||
+ | permit_sasl_authenticated, | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | reject_sender_login_mismatch | ||
+ | permit | ||
+ | |||
+ | Personally, I disable most of the above rejects by design. I include them here more for others' | ||
+ | |||
+ | | ||
+ | body_checks = pcre:/ | ||
| | ||
- | If you have issues, it' | + | In these locations, '' |
+ | |||
+ | /free mortgage quote/ | ||
+ | /free mortgage quote/ | ||
+ | |||
+ | Once they are to your liking, make sure to compile them with '' | ||
+ | |||
+ | protocols = imap lmtp sieve | ||
+ | |||
+ | In '' | ||
+ | |||
+ | protocol lda { | ||
+ | mail_plugins = $mail_plugins sieve | ||
+ | } | ||
+ | |||
+ | Let's also adjust the lmtp agent in '' | ||
+ | |||
+ | protocol lmtp { | ||
+ | mail_plugins = quota sieve | ||
+ | } | ||
+ | |||
+ | Now it's time to setup spamassassin in postfix with the other milters. Let's open '' | ||
+ | |||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | |||
+ | It's now time to edit spam assassin' | ||
+ | |||
+ | #optional custom message | ||
+ | # | ||
+ | OPTIONS=" | ||
+ | #Reject emails with spamassassin scores > 15 [or desired score] | ||
+ | OPTIONS=" | ||
+ | |||
+ | Again, I do not use the reject or discard options but rather leverage spam assassin' | ||
+ | |||
+ | sieve_before = / | ||
+ | |||
+ | The sieve_before rule ensures spam assassin assesses the email right after they arrive and before sending them to dovecot and its configured delivery agents. Let's open '' | ||
+ | |||
+ | require " | ||
+ | if header :contains " | ||
+ | { | ||
+ | fileinto " | ||
+ | stop; | ||
+ | } | ||
+ | |||
+ | After creating the sieve rule, compile it. | ||
+ | |||
+ | sudo sievec / | ||
+ | |||
+ | This rule does one thing. It checks whether spam assassin identified the message as spam, and if so, it uses dovecot to file it in Junk. This means your spam assassin scores and config are what drive the success rate of this sieve rule. It's also important to note that this rule is global, and impacts all user names on the mail server. It's a good approach for the most heinous spam, leaving more customized rules to roundcube's sieve implementation, | ||
+ | |||
+ | report_contact webmaster@domain.com | ||
+ | #adjust score below to your use-case | ||
+ | required_score 5.0 | ||
+ | # | ||
+ | report_safe 0 | ||
+ | add_header all Spam-Flag _YESNO_ | ||
+ | add_header all Score _SCORE_ | ||
+ | add_header all Report _REPORT_ | ||
+ | add_header all Level _STARS_ | ||
+ | add_header all Status " | ||
+ | add_header all Checker-Version " | ||
+ | # | ||
+ | # | ||
+ | |||
+ | I included some header options, which can help with debugging. Also, I disable safe reporting and Subject rewriting because they alter the original email, which I think is overkill. In order to activate all that spam assassin can do, we need to have our own recursive DNS resolver, required by RBL services. Let' | ||
+ | |||
+ | dns_server 127.0.0.1 | ||
+ | score MISSING_FROM | ||
+ | score MISSING_DATE | ||
+ | score MISSING_HEADERS | ||
+ | score PDS_FROM_2_EMAILS | ||
+ | score FREEMAIL_FORGED_REPLYTO | ||
+ | score DKIM_ADSP_NXDOMAIN | ||
+ | score FORGED_GMAIL_RCVD | ||
+ | score FREEMAIL_FORGED_FROMDOMAIN | ||
+ | score HEADER_FROM_DIFFERENT_DOMAINS | ||
+ | score FREEMAIL_FROM | ||
+ | score ACCT_PHISHING | ||
+ | score AD_PREFS | ||
+ | score ADMAIL | ||
+ | score ADMITS_SPAM | ||
+ | score CONFIRMED_FORGED | ||
+ | score FROM_PAYPAL_SPOOF | ||
+ | score SPF_SOFTFAIL | ||
+ | score SPF_FAIL | ||
+ | whitelist_from *@statefarm.com | ||
+ | blacklist_from *@email.freethinkerdaily.com | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | require [" | ||
+ | # rule: | ||
+ | if anyof ( | ||
+ | header :contains " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | ) { | ||
+ | keep; | ||
+ | stop; | ||
+ | } | ||
+ | # rule: | ||
+ | if anyof ( | ||
+ | header :contains " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | ) { | ||
+ | fileinto " | ||
+ | stop; | ||
+ | } | ||
+ | # rule: | ||
+ | if anyof ( | ||
+ | header :contains " | ||
+ | header :contains " | ||
+ | header :contains " | ||
+ | ) { | ||
+ | fileinto " | ||
+ | stop; | ||
+ | } | ||
+ | |||
+ | These rules are processed sequentially. Monitor the Junk folder periodically | ||
+ | |||
+ | Disable the ''/ | ||
+ | |||
+ | / | ||
+ | missingok | ||
+ | daily | ||
+ | rotate 7 | ||
+ | create | ||
+ | compress | ||
+ | start 0 | ||
+ | } | ||
+ | |||
+ | Once that's done, let's create script and cronjob to send us daily reports by creating a file '' | ||
+ | |||
+ | #!/bin/sh | ||
+ | #/ | ||
+ | gunzip / | ||
+ | / | ||
+ | gzip / | ||
+ | sleep 2s | ||
+ | systemctl restart rsyslog | ||
+ | systemctl restart postfix | ||
+ | systemctl restart dovecot | ||
+ | exit 0 | ||
+ | |||
+ | The key here is that your script and zip and unzipping rules match the retention and naming conventions specified in logrotate. Since I floored the rotation at 0, the script always unzips the '' | ||
+ | |||
+ | 30 12 * * * /bin/bash / | ||
+ | |||
+ | You can also use the '' | ||
+ | |||
+ | ------------------------------------------- | ||
+ | |||
+ | The SASL module packages should be brought in as dependencies of postfix and/or dovecot. However, on upgrades, etc., they might be removed during dependency resolution. If you get "no sasl" report on your logs suddenly, despite everything working prior, use: | ||
+ | |||
+ | sudo apt-get install libsasl2-modules | ||
+ | |||
+ | If/when things are going wrong, turn on your detailed debugging logs and study them: | ||
+ | |||
+ | nano / | ||
+ | < | ||
+ | |||
+ | To check record health after you set your DNS records, you can do the following: | ||
| | ||
dig txt +short _dmarc.jonathanhaack.com | dig txt +short _dmarc.jonathanhaack.com | ||
Line 311: | Line 486: | ||
dig default._domainkey.jonathanhaack.com txt | dig default._domainkey.jonathanhaack.com txt | ||
dig default._domainkey.haacksnetworking.org txt | dig default._domainkey.haacksnetworking.org txt | ||
+ | dig txt +short jonathanhaack.com | ||
+ | dig txt +short haacksnetworking.org | ||
+ | dig -x 8.28.86.130 +short | ||
+ | dig -x 8.28.86.125 +short | ||
+ | sudo opendkim-testkey -d jonathanhaack.com -s default -vvv | ||
+ | sudo opendkim-testkey -d haacksnetworking.org -s default -vvv | ||
| | ||
+ | You should test email health with the CLI and/or use a service like [[https:// | ||
+ | |||
+ | echo "Hi, I am testing the subdomain email health." | ||
| | ||
- | --- //[[jonathan@haacksnetworking.org|oemb1905]] | + | Postfix has its own CLI control tools, such as but not exclusive to viewing email, deleting email, etc.: |
+ | |||
+ | mailq | ||
+ | postcat -q E900C4780073 | ||
+ | postsuper -d E900C4780073 | ||
+ | postsuper -d ALL | ||
+ | |||
+ | These tools prove helpful if/when emails get stuck, etc. | ||
+ | |||
+ | --- //[[alerts@haacksnetworking.org|oemb1905]] |