Quick DNS install

by Mark Nielsen
Copyright May 2023



Quick DNS install

The purpose of this document is to setup your own DNS server, have your servers or local server point it to for resolution, required for some services, like LDAP, and to use in our TestEnv.

Links



Setup

This is for Ubuntu.

For this document, replace 10.0.2.15 with the ip address of your computer.


   # log in as root, this assumes sudo is setup from your account. 
sudo bash
   # if you can't sudo : su -l root
   
    # make sure your systems is up to date
apt-get update 
apt-get upgrade 
apt-get dist-upgrade

    # install the server and utilities

apt-get -y install bind9 dnsutils

   # Save the original copy and make a new one. 
mv /etc/bind/named.conf.options /etc/bind/named.conf.options_ORIG

   # Replace W.X.Y.Z with theip address found at
   # grep ^nameserver /var/run/systemd/resolve/resolv.conf | grep -v 127
   # in the next echo command. 
   # or edit the file after and change it. 
   
echo 'options {
   directory "/var/cache/bind";
   forwarders {
   ' > /etc/bind/named.conf.options
  
   for i in `grep ^nameserver /var/run/systemd/resolve/resolv.conf | grep -v 127 | cut -d' ' -f2` ; do
echo "$i; " >> /etc/bind/named.conf.options
   done
   
echo "   
   8.8.8.8;
   };
   	dnssec-validation auto;
	listen-on-v6 { any; };
};
" >> /etc/bind/named.conf.options

   # Make the files for local zone.
   # The first adds the zone and what config file to use.
   # The second is the config file with the DNS entries for our zone. 
   # The third command adds to /etc/hosts just in case DNS does not work.

   
echo '
   zone "local" {
type master;
file "/etc/bind/db.local2";
};
   ' >> /etc/bind/named.conf.local

echo '
$TTL    604800

@       IN      SOA     ldap.local. root.local. (
                              3         ; Serial
                         604820         ; Refresh
                          86600         ; Retry
                        2419600         ; Expire
                         604600 )       ; Negative Cache TTL

; name servers - NS records
    IN  NS  ldap.local.

; name servers - A records
ldap.local.          IN      A       10.0.2.15

   ' > /etc/bind/db.local2

   # Let us add to /etc/hosts just in case
   echo "10.0.2.15 ldap.local" > /etc/hosts

   # lets make sure bind starts at bootup
   
    systemctl enable named.service
   #Now test it
nslookup ldap.local

   # Lets add DNS to any ethernet ports. 

for i in `ifconfig | egrep ^[a-z] | cut -d ":" -f1 | grep -v "^lo"`; do
  systemd-resolve --interface $i --set-dns 127.0.0.1
done

   # Now check name resolution
   systemd-resolve --status
   # You should see 127.0.0.1 as the DNS for all ethernet ports. 

   # Check is DNS is running right.
   nslookup ldap.local
   nslookup google.com

The test should look like


root@linux4:/home/mark# nslookup ldap.local
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	ldap.local
Address: 10.0.2.15

root@linux4:/home/mark# nslookup google.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	google.com
Address: 172.217.12.14
Name:	google.com
Address: 2607:f8b0:4025:815::200e

  1. Is ldap.local is working, but google.com is not, then there is something wrong with the forwarding.
  2. If google.com is working, and ldap.local is not, it is not hitting the local server for DNS lookups or DNS configuration is wrong.

Now, you might have to create a script at start.

Reboot your system, hopefully named comes up and DNS works!