« HOWTO Sort out kvmNetwork Time Protocol (NTP) HOWTO »

HOWTO Use dnsmasq

19/07/08

HOWTO Use dnsmasq

Description

This description shamelessly take from the dnsmasq home page.

Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server. It is designed to provide DNS and, optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS. The DHCP server integrates with the DNS server and allows machines with DHCP-allocated addresses to appear in the DNS with names configured either in each host or in a central configuration file. Dnsmasq supports static and dynamic DHCP leases and BOOTP/TFTP for network booting of diskless machines.

...

Installation Using 'apt-get'

Use the command line below . You will need the universe repository in your software sources list.

Install command
sudo apt-get install dnsmasq dnsmasq-base

Initialization and Configuration.

The example system used in this HOWTO

The server where dnsmasq is running its DNS and DHCP services is called linux60 (192.168.0.7), The Router is IP 192.168.0.1. There are a mixture of real and virtual machines all using these services. All machines are in the local domain example.com. The client machines will have names like linux12 or kvmubuntu.

Setting up the server

Make backup copies of 'conf' files.

Start by making a copy of the files we will be changing so you can always go back to a know starting point.

Copy original configuration files:
cd /etc  cp dnsmasq.conf hosts resolv.conf  ~/mybackups

Setting up /etc/dnsmasq.conf

Looking at the file /etc/dnsmasq.conf first. Below is a good starting point to get things up and running. The lines are listed in the same order as they appear in the default file. Just uncomment and amend them as necessary. (Remove the '#' from the beginning of the line)

Example /etc/dnsmasq.conf:

domain-needed  


bogus-priv  
expand-hosts  
domain=example.com  
dhcp-range=192.168.0.20,192.168.0.50,24h

What these lines will do for you.

  1. domain-needed This tells dnsmasq to never pass short names to the upstream DNS servers. If the name is not in the local /etc/hosts file then "not found" will be returned.
  2. bogus-priv All reverse IP (192.168.x.x) lookups that are not found in /etc/hosts will be returned as "no such domain" and not forwarded to the upstream servers.
  3. expand_hostsSo we can see our local hosts via our home domain without having to repeatedly specify the domain in our /etc/hosts file.
  4. domain This is your local domain name. It will tell the DHCP server which host to give out IP addresses for.
  5. dhcp-range This is the range of IPs that DHCP will serve: 192.168.0.20 to 192.168.0.50, with a lease time of 24 hours. The lease time is how long that IP will be linked to a host.

Dnsmasq will, set or find out automatically, lots of common networking and connection values. These do not need to be set unless you are paranoid or like to specifically set these things. Which is rather nice of dnsmasq, don't you agree?

  • broadcast address
  • network mask
  • router parameters
  • interface (eth0) and IP address to listen on

Setting the server /etc/hosts file

The /etc/hosts file on the example server will look like this. Leave the IPv6 stuff as it was.

Example /etc/hosts:

127.0.0.1 localhost  


192.168.0.7 linux60 

Setting the server /etc/resolv.conf file

One last thing to do it set the localhost or loop device on the server as a nameserver so it can use the DNS service that it is running. Add the nameserver line below to the top of the list in /etc/resolv.conf.

Example /etc/resolv.conf:
nameserver  127.0.0.1

Setting up the clients

Make backup copies of 'conf' files.

Start by making a copy of the files we will be changing so you can always go back to a know starting point.

Copy original configuration files:
cd /etc  cp dhcp3/dhclient.conf hosts resolv.conf  ~/mybackups

The /etc/hosts file on the client machines should look similar to this. Leave the IPv6 stuff alone.

Example /etc/hosts:

127.0.0.1 localhost 


127.0.1.1 linux12 
info
There should be no need to change the hosts file from the default one created by Ubuntu.

Clients for DNS.

In the example setup our server is linux60 or 192.168.0.7 it is the only machine with a static IP address. This is necessary as we need to tell the other machine were to look for the DNS service. So we need to tell the other machines were to get their DNS service from to take advantage of our shiny new server. We do this by adding a line to /etc/resolv.conf. I put it as the first nameserver in the file.

Example /etc/resolv.conf:
nameserver  192.168.0.7

Clients for DHCP.

Only two small changes to make here. We need to make sure that when the host requests an IP address it passes its own hostname to the DHCP server. So the other machines can use its name to look up the IP address it was just given. So for example if we are setting up our workstation called linux12 that is in our fictitious domain example.com we would add the following line to /etc/dhcp3/dhclient.conf

Example /etc/dhcp3/dhclient.conf:
send host-name "linux12.example.com"; 

The second is to make sure that the main network interface is set to use DHCP.
Edit the interfaces file and set the NIC to be dhcp as below:

auto eth0
iface eth0 inet dhcp

You can now reboot linux12. During which time it will talk to our new DHCP server request a new IP address. The server will remember which IP address it gave out to linux12 and it and other machines on the network will be able to ping linux12. No more needing to know which machine has which IP address.


Pages: 1· 2· 3

Tags: dhcp, dns, dnsmasq

No feedback yet