3

I have Ubuntu 11.10 running on VirtualBox and I've set up a virtual named server.
I'm able to access this site from Windows 7, but I'm not able to access the site from Ubuntu.

My HOSTS file is located in /etc/ folder and looks like this:

127.0.0.1       localhost
127.0.1.1       ubuntu-VirtualBox
192.168.0.97     mysite.com

But whenever I try to access mysite.com from the server, I get redirected to a site saying that this domain is for sale.

What could be the cause of the hosts file not working?

Update

This is my /etc/nsswitch.conf

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

#hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4
hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
2
  • 1
    Is your file really named 'HOSTS'? It should be lower-case: /etc/hosts
    – rvs
    Jan 29 '12 at 10:01
  • No, it's lowercase, I just used uppercase here to make it stand out.
    – Steven
    Jan 29 '12 at 11:44
9

First, check that nsswitch.conf is correct:

$ grep hosts /etc/nsswitch.conf
hosts:          files dns

Make sure that it says "files dns", otherwise it will not look in your hosts file before asking DNS.

If it is correct in nsswitch I have before seen that an invisible character somehow got into my hosts file, causing it to look perfectly normal but the invisible character made it not work.
So try deleting the line and then create it again - by hand, don't paste.

Next thing you can try is to test what it looks it up for by pinging:

$ ping mysite.com
PING mysite.com (192.168.0.97) 56(84) bytes of data.

If you see the correct IP address there, it does resolve correctly and your browser is the one causing trouble. Do not use host, dig or nslookup, they ignore /etc/hosts! Use ping to have the IP address resolved as most applications would see it.

5
  • Thanks. I change it, but I still get the same result. If I ping, I get the following message: ping: unknown host http://mysite.com
    – Steven
    Jan 29 '12 at 11:48
  • 1
    @Steven: Are you trying to ping the full URL http://mysite.com/? Ping only the name mysite.com.
    – Khaled
    Jan 29 '12 at 12:02
  • @Khaled, Ah. Yes, that worked.
    – Steven
    Jan 29 '12 at 12:12
  • @Steven: So, the name should be resolvable now from the browser also.
    – Khaled
    Jan 29 '12 at 12:36
  • Invisible character. Thanks. Been banging my head against the wall for an hour now.
    – Swader
    Jul 22 '13 at 1:54
1

Run curl -Iv http://mysite.com

You should get something like (plus a bunch more lines):

* About to connect() to mysite.com port 80
*   Trying 192.168.0.97... connected
* Connected to mysite.com (192.168.0.97) port 80
> HEAD / HTTP/1.1
> User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: mysite.com
> Accept: */*
>
< HTTP/1.1 200 OK

Actually, here's a thought: are you hitting "mysite.com" or "www.mysite.com"? If it's the latter, you don't have an entry like that in your hosts file, and it will fail. If you're going to "mysite.com" but your web server is redirecting that to "www.mysite.com", that too will fail.

Note the "200 OK" at the end of my curl output snippet. If you're getting, say, a 301, you're getting redirected to someplace else by your web server configuration.

1

I had to log out and back in after I added the entries to /etc/hosts

0

You mentioned named. If you're expecting to use that DNS service, that needs to be in your resolv.conf file.

0

By default your file permissions of file "/etc/hosts" are 600. Try this command: sudo chmod 644 /etc/hosts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.