Once you’ve assigned a static IP address, edit the hostname in the /etc/hosts file so that it resolves to the static IP. For example, if your hostname is dnsmasq, edit the line:
This will allow clients to resolve the static IP of the DNS server.
Step 2: Install Dnsmasq
Run the following command to install Dnsmasq
sudo apt install dnsmasq
Modify the /etc/resolv.conf file so that it uses the local Dnsmasq server to resolve DNS requests.
echo "nameserver 127.0.0.1" | sudo tee /etc/resolv.conf
We’ll add the public DNS servers to the Dnsmaq config file in the next step.
Step 3: Configure DNS
The Dnsmasq configuration file on Ubuntu is located at /etc/dnsmasq.conf. First we will create a backup by running the following command:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
Now create a new file by running:
sudo vim /etc/dnsmasq.conf
Add the following contents to the file:
# Global settings
# Upstream nameservers
# domain name
In the example config above, you might want to change the upstream nameservers from Googles to something different. You’ll also want to change the domain, local and listen-address options to suit your environment.
Save the file and apply the changes by restarting Dnsmasq with the following command:
sudo systemctl restart dnsmasq
Step 4: Configure DHCP
If you want to use the built-in DHCP server that comes with Dnsmaq, add the following to the config file and restart the service to apply the changes.
You’ll want to adjust the IP settings and the range to suit your requirements.
How to assign fixed addresses
If you have any devices on your network that you’d like to fix the IP address of without having to change it to static in the device’s settings, you can do that by obtaining the MAC address of the device and adding an entry to the config like in the example below:
In this example, I’m fixing the device with the MAC address of 00:0C:29:D0:95:5E to the IP of 10.1.1.11 and giving it a hostname of esxi01.
How to view DHCP Leases
If you want to see what DHCP leases have been created you can run the following command:
This is can be a useful way of determining the MAC address of a machine before fixing its IP address.
How to add DNS Hosts
Hosts that are assigned via DHCP can already be resolved by other clients on your LAN, but if you want to add custom hosts to resolve, you can do it by adding them to the /etc/hosts file.
For example, adding the following:
To the hosts file will resolve dev to 10.1.2.100. It will also resolve dev.vsphere.lab because of the domain and expand-hosts options set in the config file.
In this post, we learned how to install and configure Dnsmasq as a local DNS server. Using a local DNS server can speed up web response times when multiple devices on your network access the same domain name.
Tony is the founder and editor of GraspingTech, a blog that helps developers and business owners deploy modern web applications to the cloud. He has written over one hundred tutorials which have been read by more than a million people. Some of the topics he covers are Linux, Virtualization, DevOps and web development.