How to remove DNS Zone via SSH if its Domain doesn't exist?

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
Hi guys,
I need an SSH command to run on a daily basis to check all the DNS Zones with their accounts on the server and if the account does not exist, then I need to remove this DNS Zone from this dir ( /var/named )

Example:
example1.com.cat >> example1.com (If the domain "example1.com" exist, then do nothing)
example2.com.cat >> example2.com (If the domain "example2.com" doesn't exist, then remove the DNS file "example2.com.cat")
 

techAMIGO

Well-Known Member
Nov 6, 2019
59
41
18
Kerala, india
cPanel Access Level
Root Administrator
Hi,
Do not manually remove the zone file from /var/named .. the zone file entry is also in /etc/named.conf
even if the domain is not existing, you can remove the zone from WHM Home >>> DNS Functions >>> Delete a DNS Zone. zone will be listed there and you can delete from there.
 

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
Hi,
Do not manually remove the zone file from /var/named .. the zone file entry is also in /etc/named.conf
even if the domain is not existing, you can remove the zone from WHM Home >>> DNS Functions >>> Delete a DNS Zone. zone will be listed there and you can delete from there.
I can't remove it from WHM, because I'm using DNS Cluster and the file will be deleted from the DNSOnly server.

I need to remove the DNS file using SSH
 

techAMIGO

Well-Known Member
Nov 6, 2019
59
41
18
Kerala, india
cPanel Access Level
Root Administrator
I'm not sure why you need to delete it from server only.. since you mention you're using a DNS cluster that means. zone not related to the server also seeing in the /var/named directory. it is because you have set the DNS cluster mode as synchronize. so you can change to cluster mode to standalone mode to avoid that.
also in standalone mode if you make any changes it will not reflect to other servers.



if you need to remove them manually then you need to perform two actions,

1) remove the entries from /etc/named.conf .. entries will be like


zone "domain_name.com" {
type master;
file "/var/named/domain_name.com.db";
};

2) then delete the zone file from /var/named/domain_name.com.db

which I don't recommend doing manually via SSH. instead please try standalone mode for DNS cluster
 

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
I'm not sure why you need to delete it from server only.. since you mention you're using a DNS cluster that means. zone not related to the server also seeing in the /var/named directory. it is because you have set the DNS cluster mode as synchronize. so you can change to cluster mode to standalone mode to avoid that.
also in standalone mode if you make any changes it will not reflect to other servers.

which I don't recommend doing manually via SSH. instead please try standalone mode for DNS cluster
I know that the DNS zones aren't related to the server, I just need to clean each server from the extra/unused zones after I move the website from one server to another.
Also, I don't want to manually remove the zone via SSH, I need this script to run automatically every day via cron job.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,651
2,636
363
cPanel Access Level
Root Administrator
As mentioned above, there aren't going to be any automated tools to perform this work. I'm still a bit confused about your cluster configuration and why it would be leaving DNS zones on a server - once the account is terminated on the Source machine after the migration is complete, the zone would also be removed.
 

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
As mentioned above, there aren't going to be any automated tools to perform this work. I'm still a bit confused about your cluster configuration and why it would be leaving DNS zones on a server - once the account is terminated on the Source machine after the migration is complete, the zone would also be removed.
That's correct, but my issue is not in the termination of the account.
My issue, that I always move the account from one server to another, after this move, the DNS zone will exist on both servers, I just want to remove it from the old server only using an automated tool or script
 

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
Hi there,

I believe that what you are looking for can be found on this thread: https://forums.cpanel.net/threads/delete-dns-zones-not-in-server.671429/
In case I'm right, read it carefully and make a backup prior.


Using this solution Will Not Delete zones on a dns cluster, only on the running machine.

Hope it helps.
Perfect, it solved 90% of the issue :)

What I got from the thread and replicated on my server, and worked for me is:

To just print the unmanaged zones: /scripts/autorepair unmanaged_zones

You should delete the unmanaged zones from this file manually: cat /etc/unmanaged_zones
Or to automate it, delete the file and create it again: unlink /etc/unmanaged_zones && touch /etc/unmanaged_zones
Run: /scripts/unmanaged_zones to remove the unmanaged files from cat /etc/named.conf

A small issue now happens when you delete the whole file, it deletes the DNS zone for (nameservers and hostname) for the server itself, how can I create these zone again using SSH?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,651
2,636
363
cPanel Access Level
Root Administrator
In general, I don't like to see a separate zone file for the hostname, so it's unlikely you need to recreate that. Usually that data is served elsewhere, as the host A recornd and NS/A records for the nameservers are handled in the main domain.com zone file.
 

ImperialTrader

Well-Known Member
Aug 31, 2014
196
21
68
Egypt
cPanel Access Level
Root Administrator
In general, I don't like to see a separate zone file for the hostname, so it's unlikely you need to recreate that. Usually that data is served elsewhere, as the host A recornd and NS/A records for the nameservers are handled in the main domain.com zone file.
Ok, that's fine then I don't need to re-create them again!

When I run this script command /scripts/unmanaged_zones
it asks me for confirmation [y/n]
how to make it (y) in the command?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,651
2,636
363
cPanel Access Level
Root Administrator
While the command doesn't have any flags that support that, you could likely use the Linux command "yes" to help automate that process. That would look something like this:

Code:
yes | /scripts/unmanaged_zones
which answers "y" to all questions.
 

InterServed

Well-Known Member
Jul 10, 2007
275
18
68
cPanel Access Level
DataCenter Provider
Hi,

Please make backup before trying this method. Executing this will auto answer with y.
1. Create a new file, example expectscript.exp (requires expect package to be installed)
2. The content of the file/script:
Code:
#!/usr/bin/expect -f
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn /scripts/unmanaged_zones
expect -exact "Would you like to remove the zone files and entries in /etc/named.conf for the unauthoritative zones on this server only? (y/n)  "
send -- "y\r"
expect eof
3. chmod +x expectscript.exp
4. The execute the script: ./expectscript.exp

Hopefully this will do the tasks you desire, and don't forget to always make a backup of /etc/named.conf and /var/named.
 
Last edited: