Custom TLD for local network
In this post I will mention the steps to setup a TLD ( top level domain ) which can be used in a local network. Even though I have configured dns zones manually, this was the first time I configured a TLD zone(eventhough local one) of my own and it felt really cool after completing the setup π
ASSUMPTION:
1) TLD used: “.jackal”
2) Bind version: 9
3) OS: Debian 7 (wheezy)
4) DNS/Nameserver ip: 10.111.44.221
SOLUTION:
1) Install bind and required packages,
apt-get install bind9 dnsutils
2) Insert the following into file “/etc/bind/named.conf.default-zones”,
zone "jackal." {
type master;
file "/etc/bind/db.jackal";
allow-transfer { any;};
allow-query { any;};
};
3) Verify configuration,
root@dns01:~# named-checkconf root@dns01:~#
4) Create the zone file for “jackal.” in “/etc/bind/db.jackal”
; ; BIND data file for TLD ".jackal" ; $TTL 604800 @ IN SOA jackal. root.jackal. ( 2 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; @ IN NS ns1.jackal. @ IN NS ns2.jackal. @ IN A 10.111.44.221 dns01 IN A 10.111.44.222 apache01 IN A 10.111.44.223 mysql01 IN A 10.111.44.224 postfix01 IN A 10.111.44.225 dovecot01 IN A 10.111.44.226 ns1 IN A 10.111.44.221 ns2 IN A 10.111.44.221
5) And verify the zone file, bind configuration and after that restart bind service.
root@dns01:/etc/bind# named-checkzone jackal. db.jackal zone jackal/IN: loaded serial 2 OK root@dns01:/etc/bind# named-checkconf root@dns01:/etc/bind# service bind9 restart [....] Stopping domain name service...: bind9waiting for pid 2279 to die . ok [ ok ] Starting domain name service...: bind9. root@dns01:/etc/bind#
6) Create a separate directory for storing zone files of domains,
mkdir /etc/bind/zones/
7) Use the “initdns.sh” script for creating dns zone entries.
NOTE: We are using TLD’s ending with “.jackal”. Also customize the “initdns.sh” for your own use π
root@dns01:/# ./initdns.sh rogerjo.jackal [*] Created zone file for rogerjo.jackal [*] Added zone entry for rogerjo.jackal in bind configuration root@dns01:/# named-checkzone rogerjo.jackal /etc/bind/zones/rogerjo.jackal zone rogerjo.jackal/IN: loaded serial 1378789827 OK root@dns01:/# rndc reload server reload successful root@dns01:/#
initdns.sh
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage: initdns.sh "
exit 1
fi
## Domain name
MYDOMAIN=$1
ZONECONFIG="/etc/bind/named.conf.default-zones"
if [ `sed -n '/^zone "'${MYDOMAIN}'."/p' ${ZONECONFIG}|wc -l` -eq 1 ];then
echo "[ERROR] Entry for ${MYDOMAIN} already exists"
exit 1
fi
## Nameservers
NAMESERVER1="ns1.jackal"
NAMESERVER2="ns2.jackal"
## Apache and ftp service are running on the same host
APACHE_IP="10.111.44.222"
FTP_IP="10.111.44.222"
##Mail server
SMTP_IP="10.111.44.224"
POP_IMAP_IP="10.111.44.225"
## DB Server
MYSQL_IP="10.111.44.223"
## Create zone file
cat > /etc/bind/zones/${MYDOMAIN} << EOF \$TTL 86400 @ IN SOA ns.${MYDOMAIN}. root.${MYDOMAIN}. ( 1378789827 ; Serial 10800 ; Refresh 3600 ; Retry 604800 ; Expire 10800 ) ; Minimum ${MYDOMAIN}. IN NS ${NAMESERVER1}. ${MYDOMAIN}. IN NS ${NAMESERVER2}. ${MYDOMAIN}. IN A ${APACHE_IP} www.${MYDOMAIN}. IN CNAME ${MYDOMAIN}. ${MYDOMAIN}. IN MX 10 mx01.${MYDOMAIN}. ${MYDOMAIN}. IN MX 10 mx02.${MYDOMAIN}. mx01.${MYDOMAIN}. IN A ${SMTP_IP} mx02.${MYDOMAIN}. IN A ${SMTP_IP} pop.${MYDOMAIN}. IN A ${POP_IMAP_IP} imap.${MYDOMAIN}. IN A ${POP_IMAP_IP} mysql.${MYDOMAIN}. IN A ${MYSQL_IP} ftp.${MYDOMAIN}. IN A ${FTP_IP} EOF echo "[*] Created zone file for ${MYDOMAIN}" ## Create zone entry in bind configuration cat >> ${ZONECONFIG} << EOF
zone "${MYDOMAIN}." {
type master;
file "/etc/bind/zones/${MYDOMAIN}";
};
EOF
echo "[*] Added zone entry for ${MYDOMAIN} in bind configuration"
removedns.sh
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage: removedns.sh <domainname>"
exit 1
fi
## Domain name
DOMAIN=$1
ZONECONFIG="/etc/bind/named.conf.default-zones"
if [ `sed -n '/^zone "'${DOMAIN}'."/p' /etc/bind/named.conf.default-zones|wc -l` -eq 1 ];then
##Remove entries from dns configuration file
sed -i -e '/^zone "'${DOMAIN}'."/,/^};/d' ${ZONECONFIG}
sed -i '$d' ${ZONECONFIG}
echo "[*] Removed zone entries from bind configuration"
else
echo "[ERROR] ${DOMAIN} not present in bind configuration"
exit 1
fi
#Remove zone file if it exists
if [ -f /etc/bind/zones/${DOMAIN} ];then
rm -f /etc/bind/zones/${DOMAIN}
echo "[*] Removed zone db file"
fi