dnsmasq Command in Linux



The Linux dnsmasq is a lightweight DNS, DHCP, PXE, router advertisement, and TFTP server. It is a versatile server that provides all options without installing a full DNS server. It is designed to provide DHCP and DNS services to a LAN.

The Linux dnsmasq accepts queries and attempts to answer them using a local cache or forwards them to an upstream DNS server (e.g., Googles public DNS server). It also reads the systems host file to resolve the local hosts to answer the DHCP-configured hosts.

Table of Contents

This tutorial will cover the following topics −

Prerequisites for Using dnsmasq Command

Before using dnsmasq, ensure it is installed using the command given below −

dnsmasq --version
Prerequisites for Using dnsmasq Command

If the version is displayed, the dnsmasq is installed. Otherwise, install it using the instructions below.

To install dnsmasq on Ubuntu, Debian, and Debian-based distributions, use −

sudo apt install dnsmasq

To get it on Fedora, use −

sudo dnf install dnsmasq

Syntax of dnsmasq Command

The general syntax of using the Linux dnsmasq command is as follows −

dnsmasq [options]

The [options] field specifies various configuration options, such as the custom configuration file, DHCP allocation range, DNS cache size, and other settings.

dnsmasq Command Options

The options used with the dnsmasq command are listed in the table below −

Flags Options Description
-a ip --listen-address= ip To specify the local IP address or addresses to listen on
-c size --cache-size= size To specify the cache size in entries (default is 150)
-C file --conf-file= file To specify the configuration file
-d --no-daemon To run in the debug mode instead of the daemon
-E --expand-hosts To expand hosts in the /etc/host file with domain suffix
--filter-A To avoid including the IPv4 record in the DNS answer
--filter-AAAA To avoid including the IPv6 record in the DNS answer
-F ip --dhcp-range= ip To enable DHCP in the given range of addresses with lease duration
--dhcp-hostsfile= path To read DHCP host specs from the file
--dhcp-optsfile= path To read DHCP option specs from the file
--dhcp-hostsdir= path To read DHCP host specs from a directory
--dhcp-optsdir= path To read DHCP options from a directory
-h --no-hosts To avoid loading the /etc/hosts file
-H --addn-hosts= path To specify the host file in addition to default /etc/hosts
--hostsdir= path To specify the host to read the hosts
-i --interface= interface To specify the interface name to listen
-I --except-interface To specify the interface name not to listen
-k --keep-in-foreground To keep the dnsmasq running in the foreground instead of running as a daemon
-K --dhcp-authoritative To specify the current server a DHCP server in the local network
-M opts --dhcp-boot= opts To specify the BOOTP options for the DHCP server
-N --no-negcache To avoid caching the failed results
-o --strict-order To use nameserver strictly in the order given in /etc/resolve.conf
-p port --port= port To specify the port to listen for DNS requests (default is 53)
-q --log-queries To log DNS queries
--quite-dhcp To not log DHCP
-R --no-resolve To avoid reading the resolve.conf file
-r file --resolve-file=file To specify the resolve file (default is /etc/resolve.conf)
-s domain --domain=domain To specify the domain to be assigned in DHCP leases
--test To test the configuration file syntax
-v --version To display the version of the dnsmasq command
--help To display brief help related to the command
-8 file --log-facility= file To specify the file to store the log entries instead of Syslog

Examples of dnsmasq Command in Linux

This section demonstrates the usage of the Linux dnsmasq command with examples −

  • Starting dnsmasq Server with Default Settings
  • Starting dnsmasq Server and Enabling Log
  • Starting dnsmasq Server with DHCP Range and Lease Time
  • Starting dnsmasq Server with Specific Interface
  • Starting dnsmasq Server with Multiple Options
  • Starting dnsmasq Server with Custom Settings

Starting dnsmasq Server with Default Settings

To start the dnsmasq server with the default options, execute the dnsmasq command −

dnsmasq

To run the above command, the dnsmasq server must be properly configured.

Starting dnsmasq Server and Enabling Log

Log queries help in troubleshooting the errors. To enable the log before starting the dnsmasq, use the -q or --log-queries option.

dnsmasq -q

To store the log queries in a different file, use the -8 or --log-facility option.

dnsmasq -q --log-facility=/home/user/dnsmasq.log

Starting dnsmasq Server with DHCP Range and Lease Time

The dnsmasq can be used as a DHCP server for local networks. To start the dnsmasq server with DHCP address ranges and lease time, use the -F or --dhcp-range options −

dnsmasq -F 192.168.10.80,192.168.10.160,6h

Here the IP address range starts from 192.168.1.80 and ends on 192.168.10.160. While the 6h (6 hours) is the lease time.

Starting dnsmasq Server with Specific Interface

To listen to a specific interface, you can specify it with the dnsmasq command. Use the -i or --interface option followed by the interface name.

dnsmasq -i enp0s0

Starting dnsmasq Server with Multiple Options

In the above examples, only one option is specified; however, multiple options can also be mentioned according to requirement. For example, in the following command, the interface name is mentioned, along with the DHCP IP range and lease time. The -h option prevents the server from reading the default host file, while the -q option enables the logging of DNS queries.

dnsmasq -i enp0s0 -F 192.168.10.80,192.168.10.160,6h -h -q 

Or −

dnsmasq --interface=enp0s0 --dhcp-range=192.168.10.80,192.168.10.160,6h --no-hosts --log-queries

Starting dnsmasq Server with Custom Settings

In the above examples, the options and settings are mentioned with the dnsmasq command. Mentioning many options in the terminal can make it a bit complex. All the options and settings can also be specified in a separate file. This file can be mentioned using the -C or --conf-file options.

To create a custom dnsmasq configuration file, use any text editor.

sudo nano /etc/custom.conf

The above command creates a file named custom.conf in the /etc directory. However, it can be created in any directory with any name. Now, add the setting as mentioned in the following image −

Starting dnsmasq Server with Custom Settings 1

To verify the dnsmasq configuration file syntax, use the --test option −

dnsmasq -C /etc/custom.conf --test
Starting dnsmasq Server with Custom Settings 2

Now, execute the following command to start the server with the custom configuration file.

dnsmasq  -C /etc/custom.conf

Conclusion

The dnsmasq is a lightweight DNS, DHCP, PXE, and TFTP server primarily designed for small local area networks. This tool can be used for DNS caching, DNS forwarding, and DNS filtering. It is also used as a DHCP server for the local network and supports network booting through PXE.

This tutorial explained the dnsmasq command, its installation, syntax, options, and usage through different examples.

Advertisements