cPanel & WHM

How To Mass Update SPF Records On cPanel Servers

Usually, when you migrate accounts using the Transfer Tool in WHM the DNS records are updated automatically. Records like SPF values are updated to the new server’s IP so no manual configuration is required. But there are cases where you will need to mass update some records like SPF Records in cPanel.

If you are connecting your cPanel Server to a SmartHost you will need to update the SPF records on all domain names or it’s likely your SmartHost’s IP will get listed in RBLs like SpamCop. In our Exim series of articles, we created an Exim mail server and used that as a SmartHost. We connected our development cPanel server to our SmartHost relay. Now we will bulk update the SPF record in our development server using SSH. You can use this guide to update one domain names SPF or 1000.

Mass Update SPF Records

First, let’s check our current SPF record. The IP of our SmartHost was 148.251.220.14 so this needs to be reflected in the SPF record.

dig txt first2host.org
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> txt first2host.org
 ;; global options: +cmd
 ;; Got answer:
 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63051
 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
 ;; OPT PSEUDOSECTION:
 ; EDNS: version: 0, flags:; udp: 512
 ;; QUESTION SECTION:
 ;first2host.org.                        IN      TXT
 ;; ANSWER SECTION:
 first2host.org.         14399   IN      TXT     <strong>"v=spf1 +a +mx +ip4:54.36.0.57 ~all"</strong>
 ;; Query time: 29 msec
 ;; SERVER: 8.8.8.8#53(8.8.8.8)
 ;; WHEN: Thu Feb 25 11:18:35 GMT 2021
 ;; MSG SIZE  rcvd: 90

We can see from the report that the current SPF record only reflects our development WHM server IP. So we need to update this. Let’s first copy the named folder so we can restore it should we need to.

cp -a /var/named /var/named-backup

Then move into the named folder so we can run the command to update the SPF

cd /var/named

Now we can run the command to bulk update the SPF records on all the zone files in this folder. This will update the record for ALL domain names on the server. If you just wanted to edit one. Open up the file using a text editor and edit the value manually.

find /var/named -type f -exec sed -i 's/+ip4:<strong>54.36.0.57</strong>/+ip4:<strong>148.251.220.14</strong>/gI' {} \;

systemctl restart named

The command above will search for the string +ip4:54.36.0.57 and replace it with +ip4:148.251.220.14. The command searches all files in the /var/named folder allowing you to mass update the SPF for all domain names.

Updating A Single SPF Record

If you just wanted to update a single SPF record then you could use the following command

sed -i 's/<strong>+ip4:54.36.0.57</strong>/<strong>+ip4:148.251.220.14</strong>/g' /var/named/first2host.org.db

systemctl restart named

This would change the SPF record for the first2host.org domain name from 54.36.0.57 to our SmartHost’s IP of 148.251.220.14.

Verify SPF Update

Finally to double-check the SPF record is now correct you cag dig the txt record like we did at the start.

dig txt first2host.org
dig txt first2host.org
 ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.3 <<>> txt first2host.org
 ;; global options: +cmd
 ;; Got answer:
 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42459
 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
 ;; OPT PSEUDOSECTION:
 ; EDNS: version: 0, flags:; udp: 512
 ;; QUESTION SECTION:
 ;first2host.org.                        IN      TXT
 ;; ANSWER SECTION:
 first2host.org.         14399   IN      TXT     <strong>"v=spf1 +a +mx +ip4:148.251.220.14 ~all"</strong>
 ;; Query time: 28 msec
 ;; SERVER: 8.8.8.8#53(8.8.8.8)
 ;; WHEN: Thu Feb 25 12:41:26 GMT 2021
 ;; MSG SIZE  rcvd: 94

That’s it. You have Bulk Updated SPF Records On a cPanel/WHM Server. This guide will work on any server that utilizes named for DNS services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button