Friday, May 22, 2015

How to configure dnsmasq to forward all requests to specific address

For example you have a virtual machine, which you use for local development, and you want all requests to the (name of the website that you are developing) forwarded to the virtual machine. We will use dnsmasq to achieve it.

Install dnsmasq:
sudo yum install dnsmasq

Start it with command:
sudo service dnsmasq start

Check if it was started without any errors:
sudo service dnsmasq status

If there is an error that dnsmasq failed to start because port 53 is already in use, you can check what process listening this post with command:
sudo netstat -tulpn | grep :53

If the process is dnsmasq itself it probably means that you have libvirt installed that has its own dnsmasq copy. To resolve that conflict we need to configure dnsmasq. Configuration file we are going to store in /etc/dnsmasq.d/vm.conf. By default dnsmasq uses file /etc/dnsmasq.conf for configuration. If you open that file, you will be able to see lots of examples and configuration descriptions. Lets left this file intact (almost) and allow dnsmasq to extent configuration with files from dnsmasq.d directory by adding (uncommenting) line:
# Include all files in a directory which end in .conf

Now create file /etc/dnsmasq.d/vm.conf with the following content:

The first line determines what interface dnsmasq will be listening. To check all available interfaces use command ifconfig.

Basicly you need to listen to localhost or You can probably do it with:

The last line of the configuration file says that all requests to (including subdomains, like will be redirected to (that's my virtual machine).

Also you need to add to the file /etc/dhcp/dhclient.conf (or create the file if it does not exist):
prepend domain-name-servers;

You also need to restart your system. Instead of restarting the system you may try to restart following services:
sudo service dnsmasq restart
sudo service NetworkManager restart

To check that everything works correctly, use command:

The output should show that the request was redirected to

1 comment: