Go Back   The macosxhints Forums > OS X Help Requests > Networking



Reply
 
Thread Tools Rating: Thread Rating: 8 votes, 5.00 average. Display Modes
Old 01-01-2012, 08:26 PM   #1
isthmus
Prospect
 
Join Date: Jan 2012
Posts: 3
Hosts File Not Working in OS X 10.6.8

This problem started when I began trying to use localhost. I kept getting errors to the effect of "Oops! Google Chrome could not find localhost".

But everything worked fine when I manually pointed my browser to 127.0.0.1.

Thinking that was odd, I pulled up my /etc/hosts file and had a look. Here's what it said:

Code:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
I found a couple bits of advice saying that the double-listing of localhost could confuse some operating systems, so I deleted it, saved, and flushed the cache with

Code:
dscacheutil -flushcache
But that didn't work. I also tried adding entries to the hosts file such as the following:

Code:
207.46.232.182 foo
And then flushing the cache. Again that didn't work. When I type

Code:
ping foo
I get

Code:
ping: cannot resolve foo: Unknown host
So actually the problem seems to be deeper than just localhost, but rather a complete ineffectiveness of the hosts file. I searched around to see if another hosts file, e.g., in my username directory. No luck.

What in the heck is going on here, and how do I fix it? Thanks!
isthmus is offline   Reply With Quote
Old 01-01-2012, 10:50 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,388
1) Did you mean 10.6.8 (not 1.6.8) ? If so, I'll change the thread title for you.

2) You aren't dealing with "some operating systems", you are dealing with one particular version of OS X. And it is extremely unlikely that Apple would ship a copy of /etc/hosts that is incorrect for the version of OS X that it came with.

3) You need to be careful to save the /etc/hosts file with Unix line endings. (This is true of all Unix config files).

4) Try also the 'host' command - e.g:
host localhost
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 01-02-2012, 12:28 AM   #3
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,299
Quote:
Originally Posted by hayne
Did you mean 10.6.8 (not 1.6.8)

I'm pretty sure he meant 10.6.8. System 1 is a bit past it's "use by" date.


Quote:
Originally Posted by isthmus
I found a couple bits of advice saying that the double-listing of localhost could confuse some operating systems, so I deleted it, saved, and flushed the cache with

localhost isn't double-listed. It's listed once for IPV4 and once for IPV6. Deleting the wrong line can cause serious problems. Notice where it says "Do not change this entry".

I use /etc/hosts on 10.6.8 and it works correctly. You first need to restore it to the proper initial state:

Code:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost 
fe80::1%lo0	localhost
Make sure you save it with unix line endings (automatic if you use a terminal editor like pico) and I recommend doing this before you reboot.

Then make additions properly:

Code:
123.45.67.89  specialdomain.org
127.0.0.1 domainonmycomputer.net
127.0.0.1 www.domainonmycomputer.net
You don't need to run dscacheutil, just count to 3 after you save the file. The system watches it for changes.

And just to make it more complex, certain network utilities (ping, host ...) query DNS directly instead of using the system resolver. So just because a unix utility doesn't work doesn't mean that the hosts file doesn't work.

Last edited by acme.mail.order; 01-02-2012 at 12:39 AM.
acme.mail.order is online now   Reply With Quote
Old 01-02-2012, 06:11 PM   #4
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,161
Be sure you include a newline at the end of the last line. When people edit the /etc/hosts file by hand, a common mistake is to leave off the terminating linefeed on the last line, and then they are puzzled when the last line is ignored.
ganbustein is offline   Reply With Quote
Old 01-02-2012, 06:47 PM   #5
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,299
You mean the trailing newlines in the code blocks isn't obvious? Dang.
acme.mail.order is online now   Reply With Quote
Old 01-03-2012, 05:42 PM   #6
isthmus
Prospect
 
Join Date: Jan 2012
Posts: 3
Partial Progress

hayne:

1) Yep, I did mean 10.6.8, thanks!

2) Two good points, yet localhost was whack as soon as I ever started doing anything, so my gut is a glitch in the Tiger -> Leopard -> Snow Leopard path...or something. Anyone's guess.

3) Good call, and I'm being careful about adding extra blank lines at the end (and editing it in pico)

4) Here's where it gets weird. I type

Code:
hosts localhost
and get

Code:
localhost has address 127.0.0.1
localhost has IPv6 address ::1
Host localhost not found: 3(NXDOMAIN)
So it does seem to know that it's 127.0.0.1

I also tried putting in the etc/hosts file a reference to the microsoft.com IP as "soopermsoft", and when I type

Code:
hosts soopermsoft
I get

Code:
soopermsoft has address 207.223.0.140
Host soopermsoft not found: 3(NXDOMAIN)
So far so good. But using the ping command I get:

Code:
ping: cannot resolve localhost: Unknown host
and

Code:
ping: cannot resolve soopermsoft: Unknown host
That's weird. More to the point, Chrome doesn't resolve either. Putting localhost in the address bar gives

Code:
Oops! Google Chrome could not find localhost
Safari gives

Code:
Safari can't open the page "http://localhost/" because Safari can't find the server "localhost".
Firefox tries to resolve it as www.localhost.com, and then gives

Code:
"The connection to the server was reset while the page was loading."
So what gives? The hosts command is working fine but all else is giving me nada. Any ideas for what to try?

Thanks so much for any thoughts you have.
isthmus is offline   Reply With Quote
Old 01-04-2012, 11:55 AM   #7
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,388
1) I think you were using the 'host' command, not the 'hosts' command. You really need to be more careful about small details if you are going to do things at the Unix level.
And it is usually much preferred to show us a copy/paste of your Terminal contents rather than retyping the commands in your forum reply (which can introduce typos).

2) By default, the 'host' command looks for other things (besides the IP address), like mail server info. Try doing:
host -t A localhost

3) Your browser queries would fail if you don't have a web server running at 'localhost'. Check your Apache config. I vaguely recall that you need to specify 'localhost' in the config if you want that to work.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 01-04-2012, 09:12 PM   #8
isthmus
Prospect
 
Join Date: Jan 2012
Posts: 3
1) You're right. Sorry for the typo from retyping that. Copy-paste from here on out.

2)
Code:
isthmuses-computer:~ isthmus$ host -t A localhost
localhost has address 127.0.0.1
3) I have Web Sharing turned on in the System Preferences panel.

http://192.168.1.125/~isthmus/ gives me a template site that says "Your Website" and looks Apple-y in design.

http://192.168.1.125/ says "It works!" in black-on-white Times.

http://127.0.0.1/ says "It works!" in black-on-white Times.

This leads me to think Apache is definitely running. How do I set the binding for "localhost"? I'm assuming I do it in /etc/apache2/httpd.conf, but that file is massive and over my head. Thoughts?

Again, it boggles me that I have to do so much hardcore coding just to see "It works!" at http://localhost/ . I thought Apple products were made to "just work"... At any rate, anything that gets me closer to figuring this out and fixing it is a huge help.

Here's another bit of intriguing info. This is all being done on my "programming" user on the computer. My default user can't even get http://192.168.1.125/~mainuser/ to come up correctly, despite the fact that there's a hyperlink to it right in the System Preferences window when I turn on Web Sharing for mainuser. That makes me think something is definitely out of whack in some config file somewhere. It's also why I thought I would find things easier if I made a new user. Well, http://192.168.1.125/~isthmus/ works, so that's one step better than mainuser. But still no localhost.
isthmus is offline   Reply With Quote
Old 01-04-2012, 11:24 PM   #9
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,299
Quote:
Originally Posted by isthmus
I thought Apple products were made to "just work"...

They do. Out of the box, localhost will produce the website that lives in /Library/WebServer/Documents. localhost/~username/ produces the contents of /Users/username/Sites/. "localhost" can be replaced with your current IP address at any time.

If system files have been poked at then all bets are off.

If you are trying to get http://mysite.local/ to load some random folder then you have stepped away from what OSX supports. You can either read the (copious) Apache documentation about virtual hosts or get OSX Server, which has a very nice user interface for the web service. I have this on my desktop machine and it works very well.
acme.mail.order is online now   Reply With Quote
Reply

Tags
hosts, localhost, osx, snow leopard

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:09 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.