Shell Script To Create BIND Zone Files

by on March 24, 2009 · 7 comments

First, you need to customize configuration file as follows.

Sample ns.profile.nixcraft.net configuration file

Define your default TTL, EMAIL ID and other stuff. Also, set your mail server and nameserver IPs using bash array. Save file as follows ns.profile.nixcraft.net:

  1. # defaults profile for nameserver ns1.nixcraft.net
  2. #
  3. TTL="3h" # Default TTL
  4. ATTL="3600" # Default TTL for each DNS rec
  5. EMAILID="vivek.nixcraft.in." # hostmaster email
  6. REFRESH="3h" # Refresh After 3 hours
  7. RETRY="1h" # Retry Retry after 1 hour
  8. EXPIER="1w" # Expire after 1 week
  9. MAXNEGTIVE="1h" # Minimum negative caching of 1 hour
  10.  
  11. # name server names FQDN
  12. NAMESERVERS=("ns1.nixcraft.net." "ns2.nixcraft.net." "ns3.nixcraft.net.")
  13.  
  14. # name server IPs,
  15. # leave it blank if you don't need them as follows
  16. NAMESERVERSIP=()
  17. #NAMESERVERSIP=("202.54.1.10" "203.54.1.10" "204.54.1.40")
  18.  
  19. # mail server names
  20. # leave it blank if you don't need them
  21. MAILSERVERS=("mail.nixcraft.net.")
  22. #MAILSERVERS=("smtp1.nixcraft.net." "smtp2.nixcraft.net.")
  23.  
  24. ################# add your own A recored here ##########################
  25. # You can add additonal A recs using following function
  26. function LoadCutomeARecords(){
  27. echo >/dev/null # keep this line
  28. # Uncomment or add A recoreds as per your requirments
  29. # echo "ftp $ATTL IN A 202.54.2.2"
  30. # echo "webmail $ATTL IN A 202.54.2.5"
  31. # echo "ipv6host $ATTL IN AAAA 2001:470:1f0e:c2::1"
  32. }

Add additional records using LoadCutomeARecords(). You can create multiple nameserver configuration file and call it from mkzone.sh.

mkzone.sh: Shell script to create BIND zone file

  1. #!/bin/bash
  2. # A Bash shell script to create BIND ZONE FILE.
  3. # Tested under BIND 8.x / 9.x, RHEL, DEBIAN, Fedora Linux.
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2002,2009 Vivek Gite <vivek@nixcraft.com>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11. # Examples:
  12. # ./mkzone.sh example.com default-www-IP-address
  13. # ./mkzone.sh cyberciti.biz 74.12.5.1
  14. # -------------------------------------------------------------------------
  15. # Last updated on: Mar/24/2007 - Fixed a few bugs.
  16. # -------------------------------------------------------------------------
  17. DOMAIN="$1"
  18. WWWIP="$2"
  19.  
  20. if [ $# -le 1 ]
  21. then
  22. echo "Syntax: $(basename $0) domainname www.domain.ip.address [profile]"
  23. echo "$(basename $0) example.com 1.2.3.4"
  24. exit 1
  25. fi
  26.  
  27. # get profile
  28. PROFILE="ns.profile.nixcraft.net"
  29. [ "$3" != "" ] && PROFILE="$3"
  30.  
  31. SERIAL=$(date +"%Y%m%d")01 # Serial yyyymmddnn
  32.  
  33. # load profile
  34. source "$PROFILE"
  35.  
  36. # set default ns1
  37. NS1=${NAMESERVERS[0]}
  38.  
  39. ###### start SOA ######
  40. echo "\$ORIGIN ${DOMAIN}."
  41. echo "\$TTL ${TTL}"
  42. echo "@ IN SOA ${NS1} ${EMAILID}("
  43. echo " ${SERIAL} ; Serial yyyymmddnn"
  44. echo " ${REFRESH} ; Refresh After 3 hours"
  45. echo " ${RETRY} ; Retry Retry after 1 hour"
  46. echo " ${EXPIER} ; Expire after 1 week"
  47. echo " ${MAXNEGTIVE}) ; Minimum negative caching of 1 hour"
  48. echo ""
  49.  
  50. ###### start Name servers #######
  51. # Get length of an array
  52. tLen=${#NAMESERVERS[@]}
  53.  
  54. # use for loop read all nameservers
  55. echo "; Name servers for $DOMAIN"
  56. for (( i=0; i<${tLen}; i++ ));
  57. do
  58. echo "@ ${ATTL} IN NS ${NAMESERVERS[$i]}"
  59. done
  60.  
  61. ###### start MX section #######
  62. # get length of an array
  63. tmLen=${#MAILSERVERS[@]}
  64.  
  65. # use for loop read all mailservers
  66. echo "; MX Records"
  67. for (( i=0; i<${tmLen}; i++ ));
  68. do
  69. echo "@ ${ATTL} IN MX $(( 10*${i} + 10 )) ${MAILSERVERS[$i]}"
  70. done
  71.  
  72.  
  73. ###### start A pointers #######
  74. # A Records - Default IP for domain
  75. echo '; A Records'
  76. echo "@ ${ATTL} IN A ${WWWIP}"
  77.  
  78. # Default Nameserver IPs
  79. # get length of an array
  80. ttLen=${#NAMESERVERSIP[@]}
  81.  
  82. # make sure both nameserver and their IP match
  83. if [ $tLen -eq $ttLen ]
  84. then
  85. # use for loop read all nameservers IPs
  86. for (( i=0; i<${ttLen}; i++ ));
  87. do
  88. thisNs="$(echo ${NAMESERVERS[$i]} | cut -d'.' -f1)"
  89.  
  90. echo "${thisNs} ${ATTL} IN A ${NAMESERVERSIP[$i]}"
  91. done
  92. else
  93. # if we are here means, our nameserver IPs are defined else where else... do nothing
  94. :
  95. fi
  96.  
  97. echo "; CNAME Records"
  98. echo "www ${ATTL} IN CNAME @"
  99.  
  100. LoadCutomeARecords

How do I use this script?

Simply type the command as follows to create a zone file for cyberciti.com domain with 202.54.1.2 as default www IP:
# ./mkzone.sh cyberciti.com 202.54.1.2 ns.profile.nixcraft.net
Sample output:

$ORIGIN cyberciti.com.
$TTL 3h
@	IN	SOA	ns1.nixcraft.net.	vivek.nixcraft.in.(
			2009032401	; Serial yyyymmddnn
			3h		; Refresh After 3 hours
			1h		; Retry Retry after 1 hour
			1w		; Expire after 1 week
			1h)		; Minimum negative caching of 1 hour
; Name servers for cyberciti.com
@			3600	IN	NS	ns1.nixcraft.net.
@			3600	IN	NS	ns2.nixcraft.net.
@			3600	IN	NS	ns3.nixcraft.net.
; MX Records
@			3600	IN 	MX	10	mail.nixcraft.net.
; A Records
@ 			3600	IN 	A	202.54.1.2
; CNAME Records
www			3600	IN	CNAME	@

To save output to a zone file called /var/named/chroot/etc/bind/master/c/cyberciti.com, type:
# ./mkzone.sh cyberciti.com 202.54.1.2 ns.profile.nixcraft.net > /var/named/chroot/etc/bind/master/c/cyberciti.com



4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • KING SABRI

    Great Mr.Vivek

    I trying to make complete Bind Script (From A-Z)
    - asking to Adding Static IP
    - asking Adding Hostname of server
    - Installing bind
    - configure /etc/named.conf
    - configure /var/named/myZone.zone
    - configure /var/named/myZone.rr.zone

    % I’m still beginner , but I’ll better %

    thank you sir

  • Xenwater

    Hi very nice script,

    thank you

  • Hello KIng SABRI

    I’m working on a same script i’m onthe satrt i got eveything copied but the editing part is where ibe stuck but i figure it out any way when i need help i ask youhere guys thanks

    nice script any way

    i migh use some of your ideas mr vivek
    thanks

  • Pat

    Hi,

    I realize this post is several years old now, but I came across it looking to simplify my life. This script worked perfectly. In an effort to further simply my life however, I added your script to a simple launcher to make the db file, add the zone to named.conf.local, restart bind and run an nslookup on the newly created domain. If you or anyone else that finds this page are interested, I am pasting it below.

    #!/bin/bash
    echo -n "==> Enter a new domain name (domain.com): "
    read DOMAIN
    echo -n "==> Enter the IP Address (192.168.0.1): "
    read WWWIP
    echo "Setting up files for $DOMAIN at $WWWIP"
    ./mkzone.sh $DOMAIN $WWWIP > /etc/bind/zones/$DOMAIN.db
    echo "Adding zone to named.conf.local"
    echo -e "#Zone for $DOMAIN
    zone 42$DOMAIN42 {
         type master;
         file 42/etc/bind/zones/$DOMAIN.db42;
    };
    " >> /etc/bind/named.conf.local
    echo "Restarting Bind"
    /etc/init.d/bind9 restart
    echo "Running nslookup"
    nslookup $DOMAIN
    
  • Rob

    I’ve been working on a few scripts to enable adding and removal of records.
    Don’t suppose anything exists already?

  • rodel@lsca.edu.ph

    I am receiving error when i type the command
    [root@lsca /]# ./mkzone.sh cyberciti.com 202.54.1.2 ns.profile.lsca.edu.ph
    bash: ./mkzone.sh: Permission denied

  • Germano Pires Ferreira

    sorry but… you try …
    # chmod 755 mkzone.sh before execute this script?

Previous Script:

Next Script: