search

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 sitesupport.net (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
conf-dir=/etc/dnsmasq.d/,*.conf


Now create file /etc/dnsmasq.d/vm.conf with the following content:
interface=lo
bind-interfaces
address=/sitesupport.net/192.168.33.10


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 127.0.0.1. You can probably do it with:
listen-address=127.0.0.1


The last line of the configuration file says that all requests to sitesupport.net (including subdomains, like super.sitesupport.net) will be redirected to 192.168.33.10 (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 127.0.0.1;


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:
ping sitesupport.net

The output should show that the request was redirected to 192.168.33.10.

1 comment:

  1. Дмитрий СтепанищевJune 27, 2015 at 12:32 AM

    Thanks!

    ReplyDelete