This guide is for setting up DNS server in a small lab network.
- Configure netplan:
cat << EOF > /etc/netplan/dns_config.yml
network:
version: 2
<wifis/ethernets>:
<interface name>:
addresses:
- 192.168.0.16/24
nameservers:
search: <local domain e.g home.lab>
addresses: [1.1.1.1]
EOF
Open /etc/netplan/dns_config.yml
and replace the contents inside angled brackets with relevant values.
Apply the netplan configuration:
sudo netplan try
- Install
dnsmasq
sudo apt-get update && apt-get -y install dnsmasq
The start process for dnsmasq might fail as port 53 might already be in use. To mitigate this, stop and disable the systemd-resolved
service.
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved
Optionally, also remove the resolv configuration file.
sudo rm -v /etc/resolv.conf
- Configure
dnsmasq
# Create a backup of default configuration
sudo mv /etc/dnsmasq.d/dnsmasq.conf /etc/dnsmasq.d/dnsmasq.conf.bkp
# Create the new config file
cat << EOF > /etc/dnsmasq.d/dnsmasq.conf
domain-needed
bogus-priv
expand-hosts
domain=<local domain e.g home.lab>
local=/<local domain e.g home.lab>/
no-resolv
server=1.1.1.1
cache-size=100
EOF
Open /etc/dnsmasq.d/dnsmasq.conf
and replace the contents inside angled brackets with relevant values.
- Declare hostnames and their IP addresses in
/etc/hosts
. For example:
...
192.168.1.1 host-1
192.168.1.2 host-2
- Start and enable
dnsmasq
service
sudo systemctl start dnsmasq
sudo systemctl enable dnsmasq
Get a list of known ad servers e.g from this repository and put it in /etc/dnsmasq.d/
directory:
wget "https://github.com/notracking/hosts-blocklists/blob/master/dnsmasq/dnsmasq.blacklist.txt?raw=true" -O /etc/dnsmasq.d/adblock.dnsmasq.conf
Restart the dnsmasq service
sudo systemctl restart dnsmasq.service
After running the DNS server on your network, you can configure clients to use the new name server.
- Change
/etc/systemd/resolved.conf
:
[Resolve]
DNS=<IP address of DNS server>
FallbackDNS=1.1.1.1 #Or some valid DNS server
Domains=home.lab
- Restart resolved service
sudo systemctl restart systemd-resolved.service
- Check which DNS server is being used:
resolvectl status
- Open
System Preferences > Network > Click on 'Advanced...' button > Open 'DNS' tab
- In the
DNS Servers
field, add the IP address of the DNS server. - In the
Search Domains
field, add the local domain you configured on the server e.ghome.lab
.