Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
tutoriaux:install-email-server:install-email-server-part-4 [2023/01/06 16:41] – supprimée - modification externe (Unknown date) 127.0.0.1 | tutoriaux:install-email-server:install-email-server-part-4 [2024/07/06 01:17] (Version actuelle) – frater | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Part 4 - SPF & DKIM Setup ====== | ||
+ | After completing [[tutoriaux: | ||
+ | |||
+ | ===== What are SPF and DKIM Records? ===== | ||
+ | |||
+ | SPF and DKIM are two types of TXT records in DNS that allow you to detect email spoofing and help legitimate emails deliver into the recipient’s inbox instead of the spam folder. If your domain is abused by email spoofing, then your emails are likely to land in the recipient’s spam folder if they didn’t add you to the address book. | ||
+ | |||
+ | **SPF** (Sender Policy Framework) record specifies **which hosts or IP addresses are allowed to send emails on behalf of a domain**. You should allow only your own email server or your ISP’s server to send emails for your domain. | ||
+ | |||
+ | **DKIM** (DomainKeys Identified Mail) uses a private key to **add a signature to emails sent from your domain**. Receiving SMTP servers verify the signature by using the corresponding public key, which is published in your domain’s DNS zone. | ||
+ | |||
+ | ===== Create an SPF Record in DNS ===== | ||
+ | |||
+ | In your DNS management interface, create a new TXT record like below. | ||
+ | |||
+ | |||
+ | ^ Record Type ^ Name ^ Value ^ | ||
+ | | TXT | @ | v=spf1 mx ~all | | ||
+ | |||
+ | Where: | ||
+ | |||
+ | * **TXT** indicates this is a TXT record. | ||
+ | * Enter '' | ||
+ | * **v=spf1** indicates this is an SPF record and the SPF record version is SPF1. | ||
+ | * **mx** means all hosts listed in the MX records are allowed to send emails for your domain and all other hosts are disallowed. | ||
+ | * **~all** indicates that emails from your domain should only come from hosts specified in the SPF record. Emails that are from other hosts will be flagged as untrustworthy. Possible alternatives are '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | Some folks might think that '' | ||
+ | |||
+ | You can also list a specific IP address in your SPF record. | ||
+ | |||
+ | <code dns> | ||
+ | TXT @ | ||
+ | </ | ||
+ | |||
+ | Note that some DNS managers require you to wrap the SPF record with quotes like below. | ||
+ | |||
+ | <code dns> | ||
+ | TXT @ " | ||
+ | </ | ||
+ | |||
+ | To check if your SPF record is propagated to the public Internet, you can use the '' | ||
+ | |||
+ | On Debian, you need to install the '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | <code bash>dig example.com txt</ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | You can also use the [[https:// | ||
+ | |||
+ | ===== Configure SPF Policy Agent on Debian Server ===== | ||
+ | |||
+ | We also need to tell our Postfix SMTP server to check SPF record for incoming emails. This doesn’t help outgoing email delivery but will help with detecting forged incoming emails. | ||
+ | |||
+ | Install required packages: | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt install postfix-policyd-spf-python | ||
+ | </ | ||
+ | |||
+ | Then edit the Postfix master process configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following lines at the end of the file, which tells Postfix to start the SPF policy daemon when it’s starting itself. | ||
+ | |||
+ | < | ||
+ | policyd-spf | ||
+ | user=policyd-spf argv=/ | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Next, edit Postfix main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Append the following lines at the end of the file. The first line specifies the Postfix policy agent timeout setting. The following lines will impose a restriction on incoming emails by rejecting unauthorized email and checking SPF record. | ||
+ | |||
+ | < | ||
+ | policyd-spf_time_limit = 3600 | ||
+ | smtpd_recipient_restrictions = | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then restart Postfix. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Next time, when you receive an email from a domain that has an SPF record, you can see the SPF check results in the raw email header. The following header indicates the sender sent the email from an authorized host. | ||
+ | |||
+ | < | ||
+ | ===== Set up DKIM on Debian Server ===== | ||
+ | |||
+ | Install OpenDKIM which is an open-source implementation of the DKIM sender authentication system. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Then add postfix user to opendkim group. | ||
+ | |||
+ | <code bash> | ||
+ | Edit OpenDKIM main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | Uncomment the following lines. | ||
+ | |||
+ | < | ||
+ | Canonicalization | ||
+ | Mode sv | ||
+ | SubDomains | ||
+ | </ | ||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | Change it to | ||
+ | |||
+ | < | ||
+ | Nameservers | ||
+ | </ | ||
+ | |||
+ | This tells OpenDKIM to use 8.8.8.8, 1.1.1.1 and 9.9.9.9 to query DNS records. It’s always a good idea to use more than one name server. If one fails, the other can still answer DNS queries. Separate each IP address with a comma. | ||
+ | |||
+ | Next, add the following lines at the end of this file. | ||
+ | |||
+ | < | ||
+ | # Map domains in From addresses to keys used to sign messages | ||
+ | KeyTable | ||
+ | SigningTable | ||
+ | |||
+ | # Hosts to ignore when verifying signatures | ||
+ | ExternalIgnoreList | ||
+ | |||
+ | # A set of internal hosts whose mail should be signed | ||
+ | InternalHosts | ||
+ | </ | ||
+ | |||
+ | Save and close the file. | ||
+ | ===== Create Signing Table, Key Table, and Trusted Hosts File ===== | ||
+ | |||
+ | Create a directory structure for OpenDKIM | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Change the owner from '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | sudo chmod go-rw / | ||
+ | |||
+ | Create the signing table. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line to the file. This tells OpenDKIM that if a sender on your server is using a '' | ||
+ | |||
+ | < | ||
+ | *@example.com | ||
+ | *@*.example.com | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then create the key table. | ||
+ | |||
+ | <code bash> | ||
+ | Add the following line, which tells the location of the private key. | ||
+ | |||
+ | < | ||
+ | |||
+ | Save and close the file. Next, create the trusted hosts file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following lines to the newly created file. This tells OpenDKIM that if an email is coming from localhost or from the same domain, then OpenDKIM should not perform DKIM verification on the email. | ||
+ | |||
+ | < | ||
+ | 127.0.0.1 | ||
+ | localhost | ||
+ | |||
+ | *.example.com | ||
+ | </ | ||
+ | Save and close the file. | ||
+ | |||
+ | ===== Generate Private/ | ||
+ | |||
+ | We need to generate two keys: | ||
+ | |||
+ | * **private key** for signing outgoing emails. | ||
+ | * **public key** for receiving SMTP server to verify the DKIM signature. It will be published in your DNS zone, so the receiving SMTP server can find it. | ||
+ | |||
+ | Create a separate folder for the domain. | ||
+ | |||
+ | <code bash> | ||
+ | Generate keys using '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | The above command will create 2048 bits keys. '' | ||
+ | |||
+ | Make '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | And change the permission, so only the '' | ||
+ | |||
+ | <code bash> | ||
+ | ===== Publish Your Public Key in DNS Records ===== | ||
+ | |||
+ | Display the public key | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | The string after the '' | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | In your DNS manager, create a TXT record, enter '' | ||
+ | |||
+ | [[..: | ||
+ | ===== Test DKIM Key ===== | ||
+ | |||
+ | Enter the following command on Debian server to test your key. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | If everything is OK, you will see '' | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | Note that your DKIM record may need some time to propagate to the Internet. Depending on the domain registrar you use, your DNS record might be propagated instantly, or it might take up to 24 hours to propagate. You can go to https:// | ||
+ | |||
+ | If you see **Key not secure** in the command output, don’t panic. This is because DNSSEC isn’t enabled on your domain name. DNSSEC is a security standard for secure DNS query. Most domain names haven’t enabled DNSSEC. There’s absolutely no need to worry about **Key not secure**. You can continue to follow this guide. | ||
+ | |||
+ | If you see the **query timed out** error, it means there’s DNS resolution problem on your server. You can run the above command again to see if it will work. If it still doesn’t work, you can comment out the following line in / | ||
+ | |||
+ | < | ||
+ | TrustAnchorFile | ||
+ | </ | ||
+ | ===== Connect Postfix to OpenDKIM ===== | ||
+ | |||
+ | Postfix can talk to OpenDKIM via a Unix socket file. The default socket file used by OpenDKIM is ''/ | ||
+ | |||
+ | Create a directory to hold the OpenDKIM socket file and allow only '' | ||
+ | |||
+ | <code bash> | ||
+ | sudo mkdir / | ||
+ | |||
+ | sudo chown opendkim: | ||
+ | </ | ||
+ | |||
+ | Then edit the OpenDKIM main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | Socket | ||
+ | </ | ||
+ | |||
+ | Replace it with the following line. | ||
+ | |||
+ | < | ||
+ | Socket | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Next, open the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | |||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | SOCKET=local: | ||
+ | </ | ||
+ | |||
+ | Change it to | ||
+ | < | ||
+ | SOCKET=" | ||
+ | </ | ||
+ | |||
+ | [[..: | ||
+ | |||
+ | Save and close the file. | ||
+ | |||
+ | Next, we need to edit the Postfix main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following lines at the end of this file, so Postfix will be able to call OpenDKIM via the milter protocol. | ||
+ | |||
+ | < | ||
+ | # Milter configuration | ||
+ | milter_default_action = accept | ||
+ | milter_protocol = 6 | ||
+ | smtpd_milters = local: | ||
+ | non_smtpd_milters = $smtpd_milters | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then restart opendkim and postfix service. | ||
+ | |||
+ | <code bash> | ||
+ | ===== SPF and DKIM Check ===== | ||
+ | |||
+ | You can now send a test email from your mail server to your Gmail account to see if SPF and DKIM checks are passed. On the right side of an opened email message in Gmail, if you click the '' | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | If your message is not signed and DKIM check failed, you can check postfix log (''/ | ||
+ | |||
+ | If you see the following message in the mail log: | ||
+ | |||
+ | < | ||
+ | warning: connect to Milter service local: | ||
+ | </ | ||
+ | |||
+ | you may want to check if the opendkim systemd service is actually running. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | If opendkim is running and you still see the above error, you might need to edit the /// | ||
+ | |||
+ | < | ||
+ | to | ||
+ | |||
+ | < | ||
+ | |||
+ | Then restart Postfix. | ||
+ | |||
+ | Your email server will also perform SPF and DKIM checks on the sender’s domain. You can see the results in the email headers. The following is SPF and DKIM check on a sender using Gmail. | ||
+ | |||
+ | < | ||
+ | Received-SPF: | ||
+ | Authentication-Results: | ||
+ | dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=" | ||
+ | dkim-atps=neutral | ||
+ | </ | ||
+ | ===== Testing Email Score and Placement ===== | ||
+ | |||
+ | You can also go to https:// | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | |||
+ | ===== Microsoft Mailboxes ===== | ||
+ | |||
+ | If your emails are rejected by Microsoft Outlook or Hotmail, you need to [[https:// | ||
+ | |||
+ | What if Your Emails Are Still Being Marked as Spam? | ||
+ | I have more tips for you in this article: 7 effective tips to stop your emails from being marked as spam. | ||
+ | |||
+ | Next Step | ||
+ | In part 5, we will see how to create DMARC record to protect your domain from email spoofing. As always, if you found this post useful, please subscribe to our free newsletter or follow us on Twitter, or like our Facebook page. | ||
+ | |||
+ | {{page> |