I have written this procedure to help you on importing DNS zone in an existing Bind server. In the following procedure, I suppose the file to import have the “.dns” extension. To import these DNS zone files, you have to follow these steps :
- change the content of the zone files (SOA and NS records)
- update the named.conf file on both master and slave dns servers
- load the zones
All the following sed command can be run without the option “-i” for testing without writing the file.
To update both SOA and NS records, you can use the sed command :
- create a temporary folder : mkdir /tmp/zone_files
- copy the zone files to the folder created above
- change the SOA : sed -i ‘s/ns1.domain.old. hostmaster.domain.old./ns1.domain.new. hostmaster.domain.new./g’ *.dns
- delete the old NS entries : sed -i ‘/NS/d’ *.dns
- add the first new NS record : sed -i ‘s/\t\t\t\t)/\t\t\t\t)\n\t\t\tNS ns2.domain.new./’ *.dns
- add another NS record if applied : sed -i ‘s/\t\t\t\t)/\t\t\t\t)\n\t\t\tNS ns3.domain.new.’ *.dns
- backup your named.conf file : cp /etc/bind/named.conf /etc/bind/named.conf.bkp
- create the import_zone.py with this content :
#!/usr/bin/python import os, sys import re path = "/tmp/zone_files/" dirs = os.listdir( path ) with open("/etc/bind/named.conf", "a") as myfile: for file in dirs: zone = re.sub('\.dns$', '', file) zone2add = 'zone "' + zone + "\" {\n\ttype master;\n\tfile \"/var/lib/bind/" + file + "\";\n};\n\n" myfile.write(zone2add)
- run the script and check the content of the named.conf : python import_zone.py
- reload the dns zones : rndc reload
- You can now change the named.conf on the secondary dns servers with the following script (change the ip address with your own master dns server ip address) :
#!/usr/bin/python import os, sys import re path = "/tmp/zone_files/" dirs = os.listdir( path ) with open("/etc/bind/named.conf", "a") as myfile: for file in dirs: zone = re.sub('\.dns$', '', file) zone2add = 'zone "' + zone + "\" {\n\ttype slave;\n\tmasters { 10.1.2.3; };\n\tfile \"/var/lib/bind/" + file + "\";\n};\n\n" myfile.write(zone2add)
- reload the dns zones : rndc reload
Do not hesitate to leave a comment if you have any problem or questions with this procedure.
Import DNS zone file in Bind with Python