Install, configure and administer BIND 9 on Debian Whezzy


BINDBIND (Berkeley Internet Name Domain) is the most used DNS software over the Internet. The BIND package is available for all Linux distributions, which makes the installation simple and straightforward. In today’s article we will show you how to install, configure and administer BIND 9 on a Debian VPS.

 

Installation

The installation is very simple and easy, login to your server via SSH and run:

apt-get install bind9

Configuration

Open the /etc/bind/named.conf.options file and add the following lines on the top of the file.

acl trusted {
        127.0.0.1;
};

and in the options block add :

allow-recursion    {trusted; };

Restart the service

service bind9 restart

Administration

The following script can be used to create new DNS zone files.

#!/usr/bin/env bash
#
# Create Zone file

# Variables
BIND_LOCAL='/etc/bind/named.conf.local'
ZONE_DIR='/etc/bind/zones/'
BIND_USER='bind'
NAME_SERVER_1="ns1.your_nameserver.com"
NAME_SERVER_2="ns2.your_nameserver.com"
SERIAL=$(date +"%Y%m%d")01

# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green

error() { echo -e '\e[1;31m'$1'\e[m'; } # Red

usage () {
  ok "[*] Usage: $0 [ -i ip ] [ -d domain ] [ -h ]"
}

# Sanity check
if [[ $EUID -ne 0 ]]
then
    error "[*] Script must be run as root"
fi

# Get arguments
while getopts ":hd:i:" option; do
  case "$option" in
    d)  DOMAIN="$OPTARG" ;;
    i)  IP="$OPTARG" ;;
    h)  usage
        exit 0 
        ;;
    :)  error "[*] Error: -$OPTARG requires an argument" 
        usage
        exit 1
        ;;
    ?)  error "[*] Error: unknown option -$OPTARG" 
        usage
        exit 1
        ;;
  esac
done   

if [[ -z "$DOMAIN" || -z "$IP" ]]; then
  error "[*] Error: you must specify a Domain Name using -d and IP Address using -i"
  usage
  exit 1
fi

# Create zones directory
mkdir -p $ZONE_DIR

# Check if exist
grep "zone \"${DOMAIN}\"" ${BIND_LOCAL} > /dev/null

if [[ 0 -eq $? ]]
then
  error "[*] Error: ${DOMAIN} is already added!" >&2
  exit 1
else

# Create zone file
  cat > ${ZONE_DIR}db.${DOMAIN} << _EOF_
\$ORIGIN ${DOMAIN}.
\$TTL 86400;    expire in 1 day.
@       IN      SOA     ${NAME_SERVER_1}. admin.${DOMAIN}. (
                        ${SERIAL}      ; serial
                        10800           ; Refresh
                        3600            ; Retry
                        604800          ; Expire
                        300             ; Negative Response TTL
                )

; DNS Servers
@               IN      NS      ${NAME_SERVER_1}.
@               IN      NS      ${NAME_SERVER_2}.

; A Records
@               IN      A       ${IP}
localhost       IN      A       127.0.0.1
host            IN      A       ${IP}
mail            IN      A       ${IP}

; MX Records
@               IN      MX 10   ${DOMAIN}.
@               IN      MX 20   mail.${DOMAIN}.

; TXT Records
@               IN      TXT     "v=spf1 a mx -all"

; Aliases
ftp             IN      CNAME   ${DOMAIN}.
_EOF_

# Add record
  cat >> ${BIND_LOCAL} << _EOF_

zone "${DOMAIN}" {
type master;
file "${ZONE_DIR}db.${DOMAIN}";
};
_EOF_
fi

ok "${DOMAIN} has been successfully added."

These instructions should work on any Debian based distribution such as Ubuntu, Mint, Elementary OS and others.

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Tutorials
How to Install Ruby 1.9.3 and Rubygems 1.8.24 on CentOS 6.2
Tutorials
How to Install Dolibarr ERP CRM on a CentOS 7 VPS
Tutorials
Install MDwiki on an Ubuntu 14.04 VPS
  • AdrianoLeal

    I really enjoyed the article. Recommended. I wonder if you can do an article on reverse zone, in this way, with administration script.
    Now, appreciate the attention.