This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
computing:mailserver [2022/12/11 03:56] – oemb1905 | computing:mailserver [2025/04/23 20:55] (current) – oemb1905 | ||
---|---|---|---|
Line 2: | Line 2: | ||
* **mailserver** | * **mailserver** | ||
* **Jonathan Haack** | * **Jonathan Haack** | ||
- | * **Haack' | + | * **Haack' |
- | * **webmaster@haacksnetworking.org** | + | * **webmaster@haacksnetworking.org** |
------------------------------------------- | ------------------------------------------- | ||
- | + | ||
- | // | + | // |
------------------------------------------- | ------------------------------------------- | ||
- | 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// |
- | + | ||
- | | + | |
- | + | ||
- | Edit the second line and add a line to the bottom similar to: | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | Install postfix and mailutils | + | 127.0.1.1 example.org example |
+ | 127.0.0.1 mail.example.org localhost | ||
- | | + | 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 62: | Line 50: | ||
Now, set up the server block for your mail server' | Now, set up the server block for your mail server' | ||
- | sudo nano /etc/apache2/sites-available/ | + | sudo nano /etc/nginx/conf.d/ |
sudo mkdir -p / | sudo mkdir -p / | ||
| | ||
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 it to use IMAP, change | + | Now, let's configure and enable SASL support. Open ''/ |
+ | |||
+ | # SASL Authentication with Dovecot | ||
+ | smtpd_sasl_auth_enable = yes | ||
+ | smtpd_sasl_type = dovecot | ||
+ | smtpd_sasl_path = private/ | ||
+ | smtpd_sasl_security_options = noanonymous | ||
+ | |||
+ | Now, we can install dovecot and configure it to use IMAP and lmtp. Install | ||
+ | |||
+ | < | ||
+ | |||
+ | After that, open '' | ||
- | sudo apt install dovecot-core dovecot-imapd | ||
- | sudo nano / | ||
- | < | ||
- | sudo nano / | ||
< | < | ||
+ | |||
+ | Let's make sure dovecot is part of the mail group, including any users you intend to use email: | ||
+ | |||
sudo adduser dovecot mail | sudo adduser dovecot mail | ||
+ | sudo adduser username mail | ||
| | ||
- | sdsd | + | Now we can configure dovecot over at '' |
- | Note that when upgrading | + | service lmtp { |
- | + | unix_listener / | |
+ | mode = 0600 | ||
+ | user = postfix | ||
+ | group = postfix | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Similarly, we need to edit postfix for lmtp as well with '' | ||
+ | |||
+ | mailbox_transport = lmtp: | ||
+ | smtputf8_enable = no | ||
+ | |||
+ | Next, let's configure dovecot authorization with '' | ||
+ | |||
+ | disable_plaintext_auth = yes | ||
+ | auth_username_format = %n | ||
+ | auth_mechanisms = plain login | ||
+ | |||
+ | Now, configure SSL/TLS encryption in dovecot using your website/ | ||
+ | |||
+ | ssl = required | ||
+ | ssl_cert = </ | ||
+ | ssl_key = </ | ||
+ | ssl_prefer_server_ciphers = yes | ||
+ | ssl_min_protocol = TLSv1.2 | ||
+ | |||
+ | Set up the SASL listener by editing '' | ||
+ | |||
+ | service auth { | ||
+ | unix_listener / | ||
+ | mode = 0660 | ||
+ | user = postfix | ||
+ | group = postfix | ||
+ | } | ||
+ | } | ||
+ | |||
+ | If you have errors or can't connect your email client at this point, you can test your handshakes as follows: | ||
+ | |||
+ | openssl s_client -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.** Let's install spf policy with '' | ||
+ | |||
+ | policyd-spf | ||
+ | user=policyd-spf argv=/ | ||
+ | |||
+ | After that, let's set up '' | ||
+ | |||
+ | policyd-spf_time_limit = 3600 | ||
+ | smtpd_recipient_restrictions = | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | 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. Let's install opendkim with '' | ||
+ | |||
+ | Canonicalization | ||
+ | Mode sv | ||
+ | SubDomains | ||
+ | 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: | ||
+ | |||
+ | sudo mkdir -p / | ||
+ | sudo chown -R opendkim: | ||
+ | sudo chmod 711 / | ||
+ | |||
+ | Once all the directories and key locations are created, let's open the signing table with '' | ||
+ | |||
+ | ' | ||
+ | ' | ||
+ | |||
+ | 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 opendkim-genkey -b 2048 -d example.com -D / | ||
+ | sudo chown opendkim: | ||
+ | sudo chmod 600 / | ||
+ | |||
+ | To get the information you need for the DNS record, you can run '' | ||
+ | |||
+ | sudo opendkim-testkey -d example.com -s default -vvv | ||
+ | |||
+ | Note that that output will display "key not secure" | ||
+ | |||
+ | sudo mkdir / | ||
+ | sudo chown opendkim: | ||
+ | sudo nano / | ||
+ | |||
+ | In the dkim config file, enter: | ||
+ | |||
+ | Socket | ||
+ | |||
+ | After establishing the socket directory and location, let's configure our dkim defaults in '' | ||
+ | |||
+ | SOCKET=" | ||
+ | |||
+ | Lastly, we need to configure postfix to be able to use opendkim in '' | ||
+ | |||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | |||
+ | In addition to spf and dkim policies, it's also best practice to have a dmarc policy. Let's install opendmarc with '' | ||
+ | |||
+ | 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 for it '' | ||
+ | |||
+ | sudo chown opendmarc: | ||
+ | sudo chmod 750 / | ||
+ | sudo adduser postfix opendmarc | ||
+ | sudo systemctl restart opendmarc | ||
+ | |||
+ | Now, we need to configure postfix to work with openDMARC. Expand the milter to include openDMARC' | ||
+ | |||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | mailbox Drafts { | ||
+ | **auto = create** | ||
+ | special_use = \Drafts | ||
+ | } | ||
+ | |||
+ | Since I use lmtp, which is case-sensitive, | ||
+ | |||
+ | virtual_alias_maps = regexp:/ | ||
+ | |||
+ | 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, | ||
+ | |||
+ | smtpd_sender_restrictions = | ||
+ | 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' | ||
+ | |||
+ | header_checks = pcre:/ | ||
+ | body_checks = pcre:/ | ||
+ | |||
+ | 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="-u spamass-milter -i 127.0.0.1 -R SPAM_ARE_NOT_ALLOWED_HERE" | ||
+ | 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 | ||
+ | |||
+ | 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's use the DNS server unbound and install it as follows '' | ||
+ | |||
+ | 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 and refine whitelists as needed. If something escapes, like a full health dirty marketing scam, adjust your blacklist. That's all there is to it. Now that spam controls are setup, we need to setup some auditing tools to monitor how well our server is doing these tasks. For postfix, that tool is pflogsumm. Let's install it with '' | ||
+ | |||
+ | 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.haacksnetworking.org | ||
+ | dig default._domainkey.jonathanhaack.com 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." | ||
+ | |||
+ | 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. | ||
- | --- //[[jonathan@haacksnetworking.org|oemb1905]] | + | --- //[[alerts@haacksnetworking.org|oemb1905]] |