Importing large domain list into Bind DNS Server Configuration
How to easily import large list of domains into bind DNS server configuration files
Some time ago I was asked to import few domains into primary and secondary DNS server and create a simple WEB site which would tell that this domains are for sell. Of course I said yes as this task is extremely simple and it shouldn't bother me for more then 1 minute for a domain. However when I received the list I noticed that this few domains where around 50. I had a moment of panic but then I realized that I no longer use "click click" Microsoft DNS server but an extremely well organized Gentoo Linux based chroot Bind configuration. Thanks to this I can create configuration the right way, the Linux way. I spend 5 minutes of my time creating a fairly simple script which did all the job for me. I love Linux for saving me a lot of effort and what most important my time.
Before I will show you the script I will take few moments to explain how Bind chroot configuration is looking like on my Gentoo Linux servers. I want to make sure that others will be able to adopt this script to their distributions DNS configuration.
My Bind servers are running in chroot environment for security reasons. The DNS configuration is located at /chroot/dns catalog. Main Bind server configuration file is located at /chroot/dns/etc/bind/named.conf. This file is used in default Gentoo configuration only for main server configuration. At bottom part of the file you will find two lines:
// Include master domains config file include "/var/bind/conf/master.conf"; // Include slave domains config file include "/var/bind/conf/slave.conf";
This two lines are pointing to master and slave zone configuration files. This files should keep configuration file location for every domain zone. For master zone sample configuration line located in masters.conf should looked like this:
zone "kardasa.pl" IN { type master; file "master/kardasa.pl"; };
As you can see configuration file for my zone should be located in absolute path /chroot/dns/var/bind/master/kardasa.pl. For slave zone sample configuration line located in slave.conf should looked like this:
zone "kardasa.pl" IN { type slave; masters{ master_server_ip_address; }; file "slave/kardasa.pl"; };
I like to keep configuration like this because it's much easier for me to find information I'm looking for.
For master configuration on first server my script should do two things, append a line to /chroot/dns/var/bind/conf/master.conf file and create a new zone file in /chroot/dns/var/bind/master. For slave configuration zones on second server all my script have to do is to append a line to /chroot/dns/var/bind/conf/slave.conf, pointing to right master server. The slave zone files will be created by BIND server. The master zone configuration file should looked like this:
$TTL 600 ; kardasa.pl @ IN SOA server_fqdn. root.kardasa.pl. ( 2010030201 ; serial 12h ; refresh 1h ; retry 2w ; expire 1h ; minimum ) IN NS master_server_name. IN NS slave_server_name. @ IN A server_ip_address ; host records localhost IN A 127.0.0.1 server_name IN A server_ip_address www IN CNAME server_name
This is very simple configuration but for a domains that are for sale I don't need anything else. I surly don't need any MX record, but It doesn't cost me much to add it optionally.
I explained what I need to do, now I can show the script. I named it: bind_zone_config.sh
BIND_DIR=/chroot/dns/var/bind/ MASTER_CONF_FILE=${BIND_DIR}conf/master.conf SLAVE_CONF_FILE=${BIND_DIR}conf/slave.conf SLAVE_DIR=slave MASTER_DIR=master SERIAL=$(date +"%Y%m%y")01 MASTER_SERVER_IP="X.X.X.X" SLAVE_SERVERS_NAMES="nameserver1 nameserver2" HOSTNAME=short_server_name CNAMES="www" MX= MX_IP=X.X.X.X FILE=${2} #Checking the list of parameters if ( [ $# -lt 2 ] ) then echo "To few arguments were passed to script" echo "Usage ./bind_config.sh type zone_list_file" echo "Type can be master | slave" exit 0 fi #Creating the zone files if [ "${1}" = "master" ]; then while read line do echo "zone \"$line\" IN { type master; file \"${MASTER_DIR}/$line\"; };" >> ${MASTER_CONF_FILE} echo "" > ${BIND_DIR}${MASTER_DIR}/$line echo "\$TTL 600" >> ${BIND_DIR}${MASTER_DIR}/$line echo "; $line" >> ${BIND_DIR}${MASTER_DIR}/$line echo "@ IN SOA ${HOSTNAME}.$line. hostmaster.$line. (" >> ${BIND_DIR}${MASTER_DIR}/$line echo " ${SERIAL} ; serial" >> ${BIND_DIR}${MASTER_DIR}/$line echo " 12h ; refresh" >> ${BIND_DIR}${MASTER_DIR}/$line echo " 1h ; retry" >> ${BIND_DIR}${MASTER_DIR}/$line echo " 2w ; expire" >> ${BIND_DIR}${MASTER_DIR}/$line echo " 1h ; minimum" >> ${BIND_DIR}${MASTER_DIR}/$line echo " )" >> ${BIND_DIR}${MASTER_DIR}/$line echo "" >> ${BIND_DIR}${MASTER_DIR}/$line echo " IN NS ${HOSTNAME}.$line." >> ${BIND_DIR}${MASTER_DIR}/$line for server in ${SLAVE_SERVERS_NAMES} do echo " IN NS $server." >> ${BIND_DIR}${MASTER_DIR}/$line done if [ ${MX} ]; then echo " IN MX 10 ${MX}.$line." >> ${BIND_DIR}${MASTER_DIR}/$line fi echo "@ IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line echo "" >> ${BIND_DIR}${MASTER_DIR}/$line echo "; host_records" >> ${BIND_DIR}${MASTER_DIR}/$line echo "localhost IN A 127.0.0.1" >> ${BIND_DIR}${MASTER_DIR}/$line echo "${HOSTNAME} IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line if [ ${MX} ]; then echo "${MX} IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line fi for cname in ${CNAMES} do echo "${cname} IN CNAME ${HOSTNAME}" >> ${BIND_DIR}${MASTER_DIR}/$line done done < ${FILE} elif [ "${1}" = "slave" ]; then while read line do echo "zone \"$line\" IN { type slave; masters{ ${MASTER_SERVER_IP}; }; file \"${SLAVE_DIR}/$line\"; };" >> ${SLAVE_CONF_FILE} done < ${FILE} else echo "Usage ./bind_zone_config.sh type zone_list_file" echo "Type can be master | slave" exit 0 fi
This script is taking a domain list file and reading it line after line appending the configuration files and if necessary creating zone configuration file. Example of usage:
./bind_zone_config.sh master /var/tmp/my_list_of_domains.txt
This script could be better for example it could take the file listing domains and server but as I needed the configuration for same master and same slave server this perfect is perfect for me.
My script is also available on my svn server you can access it by following this link.
GNU Free Documentation License or Creative Commons Share Alike
If you have found something wrong with the information provided above or maybe you just want to speak your mind about it, feel free to leave a comment.
All comments will show up on page after being approved. Sorry for such policy but I want to make sure that my site will be free of abusive or vulgar content. I don't mind being criticized just do it using right words.
Processing a comment.