02 June 2010

Importing large domain list into Bind DNS Server Configuration

Categories:  Server  Gentoo  Linux

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:

  1. $TTL 600
  2. ; kardasa.pl
  3. @ IN SOA server_fqdn. root.kardasa.pl. (
  4. 2010030201 ; serial
  5. 12h ; refresh
  6. 1h ; retry
  7. 2w ; expire
  8. 1h ; minimum
  9. )
  10.  
  11. IN NS master_server_name.
  12. IN NS slave_server_name.
  13. @ IN A server_ip_address
  14.  
  15. ; host records
  16. localhost IN A 127.0.0.1
  17. server_name IN A server_ip_address
  18. 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

View the script source
  1. BIND_DIR=/chroot/dns/var/bind/
  2. MASTER_CONF_FILE=${BIND_DIR}conf/master.conf
  3. SLAVE_CONF_FILE=${BIND_DIR}conf/slave.conf
  4. SLAVE_DIR=slave
  5. MASTER_DIR=master
  6. SERIAL=$(date +"%Y%m%y")01
  7. MASTER_SERVER_IP="X.X.X.X"
  8. SLAVE_SERVERS_NAMES="nameserver1 nameserver2"
  9. HOSTNAME=short_server_name
  10. CNAMES="www"
  11. MX=
  12. MX_IP=X.X.X.X
  13. FILE=${2}
  14.  
  15. #Checking the list of parameters
  16.  
  17. if ( [ $# -lt 2 ] ) then
  18.  
  19. echo "To few arguments were passed to script"
  20. echo "Usage ./bind_config.sh type zone_list_file"
  21. echo "Type can be master | slave"
  22.  
  23. exit 0
  24. fi
  25.  
  26. #Creating the zone files
  27.  
  28. if [ "${1}" = "master" ]; then
  29.  
  30. while read line
  31. do
  32. echo "zone \"$line\" IN { type master; file \"${MASTER_DIR}/$line\"; };" >> ${MASTER_CONF_FILE}
  33. echo "" > ${BIND_DIR}${MASTER_DIR}/$line
  34. echo "\$TTL 600" >> ${BIND_DIR}${MASTER_DIR}/$line
  35. echo "; $line" >> ${BIND_DIR}${MASTER_DIR}/$line
  36. echo "@ IN SOA ${HOSTNAME}.$line. hostmaster.$line. (" >> ${BIND_DIR}${MASTER_DIR}/$line
  37. echo " ${SERIAL} ; serial" >> ${BIND_DIR}${MASTER_DIR}/$line
  38. echo " 12h ; refresh" >> ${BIND_DIR}${MASTER_DIR}/$line
  39. echo " 1h ; retry" >> ${BIND_DIR}${MASTER_DIR}/$line
  40. echo " 2w ; expire" >> ${BIND_DIR}${MASTER_DIR}/$line
  41. echo " 1h ; minimum" >> ${BIND_DIR}${MASTER_DIR}/$line
  42. echo " )" >> ${BIND_DIR}${MASTER_DIR}/$line
  43. echo "" >> ${BIND_DIR}${MASTER_DIR}/$line
  44. echo " IN NS ${HOSTNAME}.$line." >> ${BIND_DIR}${MASTER_DIR}/$line
  45. for server in ${SLAVE_SERVERS_NAMES}
  46. do
  47. echo " IN NS $server." >> ${BIND_DIR}${MASTER_DIR}/$line
  48. done
  49. if [ ${MX} ]; then
  50. echo " IN MX 10 ${MX}.$line." >> ${BIND_DIR}${MASTER_DIR}/$line
  51. fi
  52. echo "@ IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line
  53. echo "" >> ${BIND_DIR}${MASTER_DIR}/$line
  54. echo "; host_records" >> ${BIND_DIR}${MASTER_DIR}/$line
  55. echo "localhost IN A 127.0.0.1" >> ${BIND_DIR}${MASTER_DIR}/$line
  56. echo "${HOSTNAME} IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line
  57. if [ ${MX} ]; then
  58. echo "${MX} IN A ${MASTER_SERVER_IP}" >> ${BIND_DIR}${MASTER_DIR}/$line
  59. fi
  60. for cname in ${CNAMES}
  61. do
  62. echo "${cname} IN CNAME ${HOSTNAME}" >> ${BIND_DIR}${MASTER_DIR}/$line
  63. done
  64. done < ${FILE}
  65.  
  66. elif [ "${1}" = "slave" ]; then
  67.  
  68. while read line
  69. do
  70. echo "zone \"$line\" IN { type slave; masters{ ${MASTER_SERVER_IP}; }; file \"${SLAVE_DIR}/$line\"; };" >> ${SLAVE_CONF_FILE}
  71. done < ${FILE}
  72.  
  73. else
  74. echo "Usage ./bind_zone_config.sh type zone_list_file"
  75. echo "Type can be master | slave"
  76. exit 0
  77. 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.




Comments

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.

Leave a comment