Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Strange behavior of /etc/hosts

We had to redo the IPs of some of the static machines in the house here after I went to 10.6, and I had set some static entries in /etc/hosts on my MBP so I could just type "ssh user@machine" on the command prompt, rather than an IP. It worked great in Leopard, but now I'm seeing a weird phenomenon. When I edit the file it works fine now, but when I reboot it erases the change and I have to go and swap the IP again for that host (from 192... to 10...). Very annoying -- what is possibly causing this?

13" MacBook Pro, Mac OS X (10.6.2)

Posted on Jan 8, 2010 7:57 PM

Reply
23 replies

Jan 9, 2010 7:26 AM in response to mcs37

If you give your Macs easy to type Computer names

System Preferences -> Sharing -> Computer Name: xyz

Then Specify "local" as one of your "Search Domains"

System Preferences -> Network -> Advanced -> [+] Search Domain: local

You will be able to access the Mac using its Computer Name

ssh user@xyz

This takes advantage of Bonjour which takes each Mac's Computer name and makes it available as xyz.local, but since you told the network that "local" was one of the domains to search, you get to omit the ".local" and just say xyz

Having said that, it would still be nice to know why /etc/hosts is geting overwritten, and how to build a list of static system names and IP addresses.

Jan 9, 2010 11:48 AM in response to etresoft

You shouldn't even have to do that much.

By default, the computer name is often something like:

First Last's iMac

Which is difficult to type on the command line. This is why I suggested changing the Sharing Computer name.

The adding of "local" to the search domains is just icing on the cake.
Nor should anyone be using anything other than DHCP.

No one? Ever?

At home a fixed IP address can make life easier if you are port forwarding on your router, depending on make and model of router (some have the ability to lock in on the MAC address, and others do not).

At work, a fixed IP address allows me to access my Mac from a different subnet (like one 2 or 3 thousand miles away). The company also associates a nice name with the fixed IP address.

But for most home users, DHCP is a perfect solution.

Jan 9, 2010 3:09 PM in response to mcs37

mcs37 wrote:
Hey guys, the remote machine is a Linux box so no Bonjour.


Are you sure about that?

I am editing the file using emacs:

sudo emacs /etc/hosts

When I save the file it works fine. Everything restarts when I reboot though!


MacOS X is designed so that things "just work" the way you would expect them to. Hack that are standard procedure for Linux (although not even appropriate there) fail on MacOS X.

Hacking /etc/hosts is easy for a human but not very robust or scriptable. On MacOS X, look at the scutil program. You can easily write a script to do the same thing as your /etc/hosts hack, but automate it on startup, or anytime your network changes. I wrote just such a script to automatically add one of my favorite search domains every time I connect to my work VPN.

In your case, the best solution is (probably) DHCP and (especially) Bonjour. But if you really want to hack things up, try scutil.

Jan 9, 2010 3:13 PM in response to mcs37

I'll try both those but I don't consider editing /etc/hosts to be a "hack" -- any Unix user knows exactly how to use this file. It worked fine under Leopard, it just seem to keep refreshing the old file when I reboot. What I suspect is there is another master file somewhere I'm not editing and then /etc/hosts is being overwritten, but I can't find it.

Jan 9, 2010 3:28 PM in response to BobHarris

BobHarris wrote:
By default, the computer name is often something like:

First Last's iMac

Which is difficult to type on the command line.


That is a good idea. Hip network administrators usually choose a clever theme for network node names. But then, sometimes government bureaucrats make them use some totally boring and lame system.

The adding of "local" to the search domains is just icing on the cake.


That isn't such a good idea anymore. Apple designs the OS to work in a modern world of mobile devices where networks are always changing. This can make some of the standard UNIX hacks inappropriate. This is one example. I used to use just such a hack and it worked fine with that other big hack known as Cisco VPN. When we switched to Juniper, it failed. Juniper is more modern and installed it own DNS settings which you can't muck with. Well, you can muck with them to an extent - with scutil.

Nor should anyone be using anything other than DHCP.

No one? Ever?


Sorry, I'm not going to waste time qualifying answers for the 1% of outliers who truly need to do something differently. Such people usually already know who they are and and how to do it. In this day and age, no one should be using anything other than DHCP.

At home a fixed IP address can make life easier if you are port forwarding on your router, depending on make and model of router (some have the ability to lock in on the MAC address, and others do not).


A modern OS (like MacOS X) and a modern router (like one of Apple's) should handle all of that on its own.

At work, a fixed IP address allows me to access my Mac from a different subnet (like one 2 or 3 thousand miles away). The company also associates a nice name with the fixed IP address.

But for most home users, DHCP is a perfect solution.


I suspect you could use DHCP at work too. Modern software and hardware also understands subnets. I haven't had to think about subnets since I ditched my Linux router years ago.

Jan 9, 2010 3:32 PM in response to mcs37

Is ZeroConf available for your Linux Distribution? ZeroConf is the generic term for Bonjour.

Ya' Know, I was going to mention that if a Unix/Linux box was in the mix, that was another good reason for using Fixed IP addresses, but I figured, I would have been pushing my point a little too hard.

Personally I agree with you about it being very natural to just add the fixed IP addresses to /etc/hosts. It has been the traditional way to do this kind of thing for years and years. This worked in Mac Classic, was broke in early versions of Mac OS X, was fixed so it worked, and according your report it is not working again (I have not experimented with this yet, as I'm in the process of tearing my office apart and Snow Leopard system is down).

But then again, you went and mentioned Emacs, now according to the "Vim Religion", I have to shun you 🙂

Jan 9, 2010 3:42 PM in response to etresoft

Since when is editing your hosts file "hacking"? This is standard practice under *nix, even in a DNS environment. I know it's a Mac forum, but c'mon. Bonjour is nice, but outside of the Mac world, it is not standard, and while convenient, doesn't always work well on all networks. Plus, in most cases the purpose of editing your hosts file is to use familiar names not available in DNS or mDNS.

And what exactly does DHCP have to do with anything here?

Jan 10, 2010 7:10 AM in response to LittleSaint

LittleSaint wrote:
Since when is editing your hosts file "hacking"?


When it is not a complete solution to the problem.

This is standard practice under *nix, even in a DNS environment. I know it's a Mac forum, but c'mon.


Yes, it is a Mac forum. And since it doesn't seem to work for the poor original poster on a Mac, what good does it do to continue to laud its merits?

Bonjour is nice, but outside of the Mac world, it is not standard, and while convenient, doesn't always work well on all networks.


Well, the original poster has tried /etc/hosts, and that didn't work.

Plus, in most cases the purpose of editing your hosts file is to use familiar names not available in DNS or mDNS.


There are valid reasons to edit the hosts files on some systems, such as Linux or Windows. But lately I've noticed that most /etc/hosts modifications fall well into the realm of "hacking". If you want to edit /etc/hosts, and it works for you, fine - feel free to ignore me.

And what exactly does DHCP have to do with anything here?


It lets the OS handle the details so you don't have to.

Jan 10, 2010 7:23 AM in response to mcs37

mcs37 wrote:
What I suspect is there is another master file somewhere I'm not editing and then /etc/hosts is being overwritten, but I can't find it.


It is deep within the bowels of Directory Services. I suggest reading up on dscl, scutil, and dscacheutil.

While you don't seem willing to take the safe, easy, and supported route to a solution, I do appreciate your eagerness to learn more about how it all works underneath. Personally, I don't want to deal with these details, so I really don't claim to understand them. I'm happy to let Apple handle them. I don't have the time and they are far more knowledgeable than I am.

But each to their own. I did manage to track down this link that should provide another solution option for you. It still isn't quite what you are asking for, however.

The definitive solution seems to be in the command: "dscacheutil -configuration", where it shows that /BSD/local is secondary to Directory Service's /Local/Default. When you reboot, it is apparently overwriting your secondary settings with the primary settings. You could swap your dscl configuration to use /BSD/local as primary. However, I don't recommend that because you have no idea what other really handy MacOS X features are going to either break or act differently. I suggest you could modify Directory Service using instructions from that link. Everyone is correct - that is not how it is done on other UNIX or Linux systems. But the Mac is different. I encourage you to learn and use those differences for fun and profit.

Jan 10, 2010 7:25 AM in response to etresoft

I suspect you could use DHCP at work too. Modern software and hardware also understands subnets. I haven't had to think about subnets since I ditched my Linux router years ago.

DHCP only works at my company if you do not need to connect to another system outside your subnet. Inside the subnet netbios names (mostly PC or Linux boxes) are visible. Outside the subnet, the DHCP servers create dynamic DNS names you wouldn't want to meet in dark ally. Such as company-country-region-city-facility-nn-nn-nn-nn.company.com. No one wants to type that, and the next time your IP address changes, so does the name.

Fixed IP addresses are easier. We have to access remote systems to write & test our code. The code runs on Linux, Windows, AIX, and Solaris at the moment. Transferring source and binaries between development systems, build systems, and test systems would never work in a DHCP world.

But discussing the merits of fixed vs DHCP addresses does not really help solve the OP's problem.

Jan 10, 2010 7:57 AM in response to etresoft

etresoft wrote:

There are valid reasons to edit the hosts files on some systems, such as Linux or Windows. But lately I've noticed that most /etc/hosts modifications fall well into the realm of "hacking". If you want to edit /etc/hosts, and it works for you, fine - feel free to ignore me.



It's hard to ignore you when you keep giving the OP incorrect information and justifying it with what is quite frankly, ignorant statements. The OP has a legitimate question about something that should and does work just fine under Mac OS X, and so far you've offered absolutely nothing to help solve it. Moreover, you really need to do some research on host files, DNS, and DHCP before commenting anymore. It's only making you look bad.

Message was edited by: LittleSaint

Strange behavior of /etc/hosts

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.