8

I read This manual and I'm a little confused. This is what I don't undetstand:

When your system tries to resolve a hostname to an IP address or determine the
ostname for an IP address, it refers to the /etc/hosts file before using the
name servers

So:

1)When DOES the system tries to resolve a hostname? To my understanding its when a datagrams's header specifies a hostname, am I right?

2)Why does it refer to hosts before resolv.conf?

3)After the hostname is resolved, the system turns to the routing table?

2 Answers 2

15

1) Normally your system would use the DNS server on your 'resolv.conf'. If you visit www.yahoo.com your system will contact the DNS, the DNS returns the IP address of that address and your systems then knows what IP address corresponds the address www.yahoo.com.

2) It uses hosts before trying to resolve an address because hosts file is used to override any address you are trying to resolve. ie: you already have probably an entry there, 127.0.0.1 localhost tells the system that if you are trying to contact the host with the name "localhost" it will do so by using the address 127.0.0.1, in this case its your lopback interface on your eth0.

3) After the system knows an address of a host either looking directly on your hosts or by contacting the DNS servers in 'resolv.conf' it will look at the routing table to see which of the rules explains what to do with the traffic with destination to the IP address obtained.

Ie: Imagine you have 2 computers on your network with hostnames of "Ubuntu-One" and "Ubuntu-Two", each computer will probably have this assigned at the host file:

127.0.0.1 localhost Ubuntu-One for computer Ubuntu-One 127.0.0.1 localhost Ubuntu-Two for computer Ubuntu-Two

This is done automatic and its the reason you can resolve 'localhost' and 'Ubuntu-One" and "Ubuntu-Two" on each respective terminals.

Try to ping Ubuntu-Two from Ubuntu-One and Ubuntu-One will contact the DNS servers at your resolv.conf file, the servers will say "I dont know any IP related to that address" and you PC will reply "Unknown hostname". Add the line <IPADDRESSFROMUBUNTU-TWO Ubuntu-Two to the hosts file of Ubuntu-One and one you try to ping again the system will see that on the hosts file the IP address from Ubuntu-Two is x.x.x.x, will then check your routing table to see which rule applies to that IP or IP range and will contact the host using the specified gateway (or none). Thats is why hosts is ever before resolv.conf.

6
  • Thanks! My resolv.conf file only contains 10.0.0.138 which is my router address. So when I try to connect google.com it first look for it in hosts, and after failing to find it goes to my router which directs the information to my ISP DNS, am I right?
    – yotamoo
    Oct 6, 2011 at 10:48
  • Thats the spirit, correct ;) Your router will translate the hostname (unless specified directly on hosts, if its in hosts there is a chance it wont pass at your router depending on your routing table) by using the DNS servers assigned to your router by your ISP. Oct 6, 2011 at 10:53
  • And after using my ISP server it goes back to my local routing table? Seems like a waste of time, no?
    – yotamoo
    Oct 6, 2011 at 10:55
  • Not really because that way the DNS cache can work out to your advantage (ie: not asking your ISP DNS all the time what is the IP of a hostname). Oct 6, 2011 at 10:58
  • Also, I have to configure hosts every time I boot right? I'm asking because my laptop (for example) might have a different IP after a while...
    – yotamoo
    Oct 6, 2011 at 11:06
1

It needs to at least resolve the hostname to an IP whenever you want to connect to anything. It uses settings from /etc/hosts first because it's local to your system. If you haven't provided it, then it will look it up from your DNS servers, which are defined in resolv.conf.

You must log in to answer this question.

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