26

I want to do some tests on linux. I want to put a small web server on a machine with IP: 1.2.3.4 (assumption) and when a DNS query is made for a specific URL : kitty.myweb.com I want to return this IP address.

Scenarios:

  1. When I open a browser(firefox or whatever), I input the URL kitty.myweb.com and the browser will do the DNS query
  2. I want to write a program which does the DNS query for kitty.myweb.com

I know that each computer has a local DNS resolver which caches some entries; a call to a URL will query the local DNS resolver first, and if there is no corresponding entries it will send the query to an external DNS resolver on the LAN or Internet.

What I want to is to add an entry(kitty.myweb.com : 1.2.3.4) to the local DNS resolver, so that for the 2 scenarios above, the DNS response will be 1.2.3.4 without having to make an external query.

Is it possible to do this on linux, and if so, how? Are there perhaps any C code snippets that would do this?

2 Answers 2

37

You might be able to use /etc/hosts to add your entry. This is not really using DNS but it affects the resolver library in Linux, which is used by most applications. Add:

1.2.3.4 kitty.myweb.com

Another option would be to install a simple DNS server on your system. The one I often use is called dnsmasq. On some distros it is installed by default as a caching resolver, in which case you would only need to modify the configuration to include:

address=/kitty.myweb.com/1.2.3.4

If you are running your own DNS server, you will probably need to play with /etc/resolv.conf which is used by the Linux resolver library. To make the resolver library look at a local DNS server, you will need an entry in resolv.conf:

nameserver 127.0.0.1

12

The easiest way to add a single DNS entry on your local host is to add it to the hosts file. The location of that file may vary per distribution, but its traditional location is /etc/hosts and it should look roughly like this:

::1                     localhost localhost.yourdomain.tld
127.0.0.1               localhost localhost.yourdomain.tld
1.2.3.4                 kitty.myweb.com

Note that this will only effect your own computer and that is does not scale well. In other words, it is fine for simple testing, just do not try to edits hosts files on lots of computers. For that you want proper DNS entries.

Not the answer you're looking for? Browse other questions tagged .