14

This is purely out of curiosity. But if I were to type in the IP of a website, would the request still go through the forward lookup process? If not, at what point and by which step is it realized that this is the location by IP and not a hostname as a string?

Also, how would this be recorded in my hostfile? If it goes through the entire lookup process, will the hostfile contain the string hostname as well as the IP and populate locally?

5
  • 1
    Domain Name System, this is what translates a url (google.com) into the ip address, so if you use an ip address it does not have to do a name lookup, bypassing the DNS system. en.wikipedia.org/wiki/Domain_Name_System
    – Moab
    May 3, 2016 at 0:34
  • The answers below are correct, browsers are smart enough not to do a forward lookup of an IP address. Doing a forward lookup of an IP address is never a good idea, there are 3 ways a DNS server can react to it: 1) Most DNS servers will simply return the same IP address. 2) Other DNS servers cannot resolve the "dns name" to an IP and will tell you so. 3) Some (free) DNS servers cannot resolve the "dns name" and give you an IP which redirects you to their own search page (usually full of ads). May 3, 2016 at 8:04
  • 1
    Domain name is necessary if the web server hosts multiple sites. It has single IP and without domain name it cannot understand which site you want. If there is only one web site, then it will work with IP in the same way.
    – i486
    May 3, 2016 at 8:21
  • 1
    Note that the hostname isn't only sent to DNS for translation, but also to the webserver itself. This means that a webserver which serves multiple websites can't know which of those sites you want. This of course is an IPv4 problem; with IPv6 every website can have its own IP address.
    – MSalters
    May 3, 2016 at 8:50
  • 1
    Now the next question is: How does a your browser know that 2001:feed:face:dead::beef:8080 is an IPv6 address with a specified port and not just a really badly formatted URL... ;) May 3, 2016 at 11:10

2 Answers 2

18

If you use an IP address, the OS will know that it is an IP address immediately, and will not do a forward lookup.

When you do a HTTP request, the first step is to obtain the IP address of the destination, so that the browser can communicate with the server. For communication with the server, everything happens over IP, from IP address to IP address, and so the name of the server is not needed for this communication. However, it is passed in the HTTP headers as a Host: header to let the web server know which website you are after, if it is hosting multiple sites.

In the case of accessing by IP address, this does not happen, and the default site is presented.

Your hosts file is a static file, it does not change by any action you take, except if you directly edit it (or have scripts or applications that modify it).

7
  • 2
    Also, it is worth noting relatedly that if you enter the IP Address in a browser such as Chrome, it may do a Google search before simply loading the site.
    – sventechie
    May 3, 2016 at 0:36
  • 1
    Note that the internal linking of the individual objects that make up the page, is likely done by FQDN, so while the root request will run without the DNS lookup, but every image, stylesheet, script file, etc will almost certainly be resolved by DNS request when your browser attempts to render the root page and downloads all the child resources.. May 3, 2016 at 1:45
  • 2
    @FrankThomas In most cases, where the asset is on the same server, the assets are referenced by relative URLs and so will be accessed via the same method as the original request - via IP. The exception to this would be where base_href is used in the headers, and so dictate the host that relative URLs should use. If an FQDN is used then yes, game over.
    – Paul
    May 3, 2016 at 1:57
  • In the case of accessing by IP address, this does not happen, and the default site is presented. You'll far more likely be given an error page. May 3, 2016 at 8:25
  • @sventechie Really? Any idea why it would do that? Like, what's the value of that? May 3, 2016 at 8:57
3

As others have written, by using an IP address you are indeed skipping the DNS which allows accessing for example development sites without a host name.

Adding an IP - mock host name into /etc/hosts is extremely common again for development sites. Unlike using the IP address directly this will a) still skip DNS b) make the browser add the Host request header to the HTTP request allowing virtual hosts on a local server for example.

2
  • Its worth noting, that if for whatever reason the IP address gets updated, the HOSTS file is not updated along with it, which can lead in unexpected behavior and a site that seems to not be working anymore. Looking at the hosts file is one of the last things people do in troubleshooting an issue and may be overlooked entirely.
    – LPChip
    May 3, 2016 at 8:46
  • @LPChip fair but please note I have emphasized several times how this is more of a developer - local practice than anything else.
    – chx
    May 3, 2016 at 9:30

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