You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.2 KiB
Markdown

4 years ago
# dnsmasq
4 years ago
###### guide-by-example
4 years ago
![logo](https://i.imgur.com/SOa4kRd.png)
4 years ago
# Purpose & Overview
4 years ago
Lightweight DHCP and DNS server.
* [Official site](http://www.thekelleys.org.uk/dnsmasq/doc.html)
4 years ago
* [Arch wiki](https://wiki.archlinux.org/index.php/dnsmasq)
dnsmasq solves the problem of accessing self hosted stuff when you are inside
4 years ago
your network. As asking google's DNS for `example.com` will return your
4 years ago
very own public IP and most routers/firewalls wont allow this loopback,
where your requests should go out and then right back.</br>
4 years ago
Usual quick way to solve this issue is
[editing the `hosts` file](
https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2#--editing-hosts-file)
4 years ago
on your machine, adding `192.168.1.222 example.com` IP-hostname pair.
This tells your machine to fuck asking google's DNS, the rule is right there,
`example.com` goes directly to the local server ip `192.168.1.222`.</br>
But if more devices should "just work" it is a no-go, since this just works
4 years ago
one the machine which `hosts` file was edited.
4 years ago
So the answer is running a DNS server that does this
4 years ago
paring of IPs with hostnames, and a DHCP server that tells the devices
on the network to use this DNS.
4 years ago
4 years ago
*extra info*</br>
DNS servers run on port 53.
4 years ago
# Prerequisites
4 years ago
* the machine that will be running it should have set static IP
4 years ago
4 years ago
# Files and directory structure
```
/etc/
4 years ago
├── dnsmasq.conf
├── hosts
└── resolve.conf
4 years ago
```
4 years ago
* `dnsmasq.conf` - the main config file for dnsmasq where DNS and DHCP functionality is set
4 years ago
* `resolve.conf` - a file containing ip addresses of DNS nameservers to be used
4 years ago
by the machine it resides on
* `hosts` - a file that can provide additional hostname-ip mapping
`hosts` and `resolve.conf` are just normal system files always in use on any linux
4 years ago
system.</br>
`dnsmasq.conf` comes with the dnsmasq installation.
4 years ago
4 years ago
# Installation
4 years ago
Install dnsmasq from your linux official repos.
4 years ago
# Configuration
`dnsmasq.conf`
```bash
4 years ago
# DNS --------------------------------------------------------------------------
4 years ago
4 years ago
# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
4 years ago
4 years ago
# If you don't want dnsmasq to read /etc/resolv.conf
no-resolv
no-poll
4 years ago
4 years ago
cache-size=1000
# interface and address
4 years ago
interface=enp0s25
listen-address=::1,127.0.0.1
4 years ago
4 years ago
# Upstream Google and Cloudflare nameservers
server=8.8.8.8
server=1.1.1.1
4 years ago
4 years ago
# DNS entries ------------------------------------------------------------------
4 years ago
4 years ago
# wildcard DNS entry sending domain and all its subdomains to an ip
4 years ago
address=/example.com/192.168.1.2
4 years ago
# subdomain override
4 years ago
address=/plex.example.com/192.168.1.3
4 years ago
4 years ago
# DHCP -------------------------------------------------------------------------
4 years ago
dhcp-authoritative
dhcp-range=192.168.1.50,192.168.1.200,255.255.255.0,480h
4 years ago
# gateway
4 years ago
dhcp-option=option:router,192.168.1.1
4 years ago
4 years ago
# DHCP static IPs --------------------------------------------------------------
# mac address : ip address
dhcp-host=08:00:27:68:f9:bf,192.168.1.150
4 years ago
#dhcp-leasefile=/var/lib/misc/dnsmasq.leases
4 years ago
```
4 years ago
*extra info*
* `dnsmasq --test` - validates the config
* `dnsmasq --help dhcp` - lists all the DHCP options
4 years ago
You can also run **just DNS server**, by deleting the DHCP section
4 years ago
in the `dnsmasq.conf` to the end.</br>
4 years ago
Then on your router, in the DHCP>DNS settings, you just put in the ip address
4 years ago
of the dnsmasq host as the DNS server.
4 years ago
4 years ago
# resolv.conf
4 years ago
A file that contains DNS nameservers to be used by the linux machine it sits on.</br>
Since dnsmasq, a DNS server, is running right on this machine,
the entries just point to localhost.</br>
4 years ago
`resolv.conf`
```
nameserver ::1
nameserver 127.0.0.1
```
4 years ago
Bit of an issue is that `resolv.conf` belongs to glibc, a core linux library.
But there are other network related services that like to fuck with it.
Like dhcpcd, networkmanager, systemd-resolved,...</br>
Ideally you know what is running on your host linux system, but just in case
`resolv.conf` will be flagged as immutable.
This prevents all possible changes to it unless the attribute is removed.
4 years ago
4 years ago
Edit `/etc/resolv.conf` and set localhost as the DNS nameserver, as shown above.
4 years ago
4 years ago
* Make it immutable to prevent any changes to it.</br>
`sudo chattr +i /etc/resolv.conf`
* Check if the content is what was set.</br>
`cat /etc/resolv.conf`
4 years ago
4 years ago
# /etc/hosts
4 years ago
`hosts`
```
192.168.1.2 docker-host
192.168.1.1 gateway
4 years ago
192.168.1.2 example.com
192.168.1.2 nextcloud.example.com
192.168.1.2 book.example.com
192.168.1.2 passwd.example.com
192.168.1.2 grafana.example.com
4 years ago
```
4 years ago
This is a file present on every system, linux, windows, mac, android,...
where you can assign a hostname to an IP.</br>
dnsmasq reads `/etc/hosts` for IP hostname pairs and adds them to its own
resolve records.
4 years ago
Unfortunately no wildcard support.</br>
But as seen in the `dnsmasq.conf`, when domain is set it acts as a wildcard
4 years ago
rule. So `example.com` stuff here is just for show.
4 years ago
4 years ago
# Start the service
4 years ago
`sudo systemctl enable --now dnsmasq`
4 years ago
* Check if it started without errors</br>
`journalctl -u dnsmasq.service`
4 years ago
* If you get "port already in use" error, check which service is using port 53</br>
4 years ago
`sudo ss -tulwnp`</br>
4 years ago
stop and disable that service, for example if it is `systemd-resolved`</br>
4 years ago
`sudo systemctl disable --now systemd-resolved`
4 years ago
* Make sure you **disable other DHCP servers** on the network,
4 years ago
usually a router is running one.
4 years ago
4 years ago
# Test it
4 years ago
#### DHCP
4 years ago
4 years ago
Set some machine on the network to use DHCP for its network setting.</br>
4 years ago
Network connection should just work with full connectivity.
4 years ago
4 years ago
You can check on the dnsmasq host, file `/var/lib/misc/dnsmasq.leases`
4 years ago
for the active leases. Location of the file can vary base on your linux distro.
4 years ago
4 years ago
#### DNS
4 years ago
4 years ago
nslookup is a utility that checks DNS mapping,
4 years ago
part of `bind-utils` or `bind-tools` packages, again depending on the distro,
4 years ago
but also available on windows.
4 years ago
4 years ago
* `nslookup google.com`
* `nslookup docker-host`
4 years ago
* `nslookup example.com`
* `nslookup whateverandom.example.com`
* `nslookup plex.example.com`
4 years ago
4 years ago
### Troubleshooting
4 years ago
* **ping fails from windows when using hostname**</br>
windows ping does not do dns lookup when just plain hostname is used</br>
`ping meh-pc`</br>
it's a [quirk](https://superuser.com/questions/495759/why-is-ping-unable-to-resolve-a-name-when-nslookup-works-fine/1257512#1257512)
4 years ago
of windows ping utility.
Can be solved by adding dot, which makes it look like domain name and this
forces the dns lookup before pinging</br>
4 years ago
`ping meh-pc.`</br>
* **slow ping of a hostname, but fast nslookup on a linux machine**</br>
for me it was `systemd-resolved` running on the machine I was doing ping from.</br>
It can be stopped and disabled.</br>
`sudo systemctl disable --now systemd-resolved`
4 years ago
4 years ago
# Update
4 years ago
During host linux packages update.
# Backup and restore
4 years ago
#### Backup
4 years ago
4 years ago
Using [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup)
that makes daily snapshot of the /etc directory which contains the config files.
4 years ago
4 years ago
#### restore
4 years ago
4 years ago
Replace the content of the config files with the one from the backup.