Objective
Create Local DNS Server in CentOS 8 using dnsmasq so app can identify all subdomain of puliyo.lan
which points to 127.0.0.1.
How To
1. Install dnsmasq
yum install dnsmasq
2. Modify /etc/dnsmasq.conf
Following is my /etc/dnsmasq.conf setting:
# don't forward to upstream queries for plain names
domain-needed
# don't forward to upstream private addresses
bogus-priv
# use upstream servers defined here
resolv-file=/etc/resolv.dnsmasq
user=dnsmasq
group=dnsmasq
# Will be used only in this machine. Should change to work in your env
listen-address=127.0.0.1
bind-interfaces
# don't use /etc/hosts
no-hosts
# how many names to cache
cache-size=150
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig
Detailed explanation of each option can be found here
3. Create necessary files
Create conf file under /etc/dnsmasq.d/ to tell dnsmasq to resolve *.puliyo.lan
as 127.0.0.1
# cat /etc/dnsmasq.d/puliyo.lan.conf
address=/puliyo.lan/127.0.0.1
/etc/resolv.dnsmasq which will be used by dnsmasq to contact upstream dns server if domain name cannot be resolved locally
cp /etc/resolv.conf /etc/resolv.dnsmasq
4. Modify /etc/resolv.conf
Modify the /etc/resolv.conf file so it uses local dns server (dnsmasq)
# cat /etc/resolv.conf
nameserver 127.0.0.1
5. (Optional) Prevent NetworkManager overwriting /etc/resolv.conf
NetworkManager will overwrite /etc/resolv.conf whenever its service has restarts.
You can take either of following method to prevent this:
Add
dns=none
under[main]
section in /etc/NetworkManager/NetworkManager.confAdd
PEERDNS=no
in your interface under /etc/sysconfig/network-scripts/Make resolve.conf immutable
chattr +i /etc/resolv.conf
6. Start the server and test
Start the server
systemctl start dnsmasq
Test if the local domain and public domain returns correct IP
# dig +short hello.puliyo.lan
127.0.0.1
# dig +short puliyo.lan
127.0.0.1
# dig +short www.google.com
74.125.142.99
74.125.142.106
74.125.142.105
74.125.142.104
74.125.142.147
74.125.142.103