Ubuntu – Bash script to configure BIND9

binddomain-name-systemUbuntu

I have a webserver (running Ubuntu 12.04 server & Nginx) And I am setting up a number of websites on it.

I'm quite new to managing my own webserver, and I'm getting on pretty well except for when it comes to configuring the DNS.

I'd like to have the one server act as both the webserver and the DNS server (unless somebody can convince me otherwise..)

I've read through a couple BIND9 guides, but just don't understand why it has to be so complicated!

I would like one script, which I can run, something like: configure-dns.sh mydomain.com which would create the zones and entries for 'mydomain' and set the current server as the host, and authorative server.

Ideally I would like my workflow for a new domain simply to be:

  • Register the domain at a registrar
  • At the registrar, set my DNS to be my server
  • On my server, run 1 script which sets up the relevant DNS entries
  • On my server, run 1 script which sets up the relevant webserver configs (I already have this)

So,
Can you add a DNS entry with a simple bash script?

Thanks,
Dean.

Best Answer

add to /etc/bind/named.conf:

include "/etc/bind/domain-enabled.conf";

make dir:

# mkdir /etc/bind/domain-enabled/

scripts:

cat <<EOF >>/etc/bind/domain-enabled.conf
zone "$1" {
        type master;
        file "/etc/bind/domain-enabled/$1.db";
};
EOF
cat <<EOF >/etc/bind/domain-enabled/$1.db
\$TTL   604800
@       IN      SOA     ns1.$1. root.localhost. (
                              2         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns1.$1.
@       IN      A       $2
ns1     IN      A       $2
EOF
rndc reload

run:

add-domain examle.com 127.0.0.1
Related Topic