22

In order to do web development on my local machine, I use the /etc/hosts file to define hosts that point back to 127.0.0.1 to which I have Apache serve up via VirtualHosts. This works great, except that now it seems that Lion resets my /etc/hosts file every so often.

Has anyone else experienced this? Is there any way to keep this from happening?

I never had this problem in Snow Leopard.

JdeBP
  • 27,556
  • 1
  • 77
  • 106
Jimmy Z
  • 691

4 Answers4

37

I just discovered what is resetting my /etc/hosts file. It the Cisco AnyConnect VPN Client. Any time I would connect to my organization's VPN, it was resetting the hosts file to a default. I so rarely connect to the VPN, that it made the problem seem intermittent.

If you use Cisco's AnyConnect VPN Client, please be aware that it will reset your hosts file to the contents of the following: /etc/hosts.ac

To solve the problem, make your hosts changes in both the /etc/hosts and the /etc/hosts.ac file.

I got the hint from MacWorld.

Jimmy Z
  • 691
2

Directory Services (NetInfo in Tiger and earlier) has its own static hosts map, which will be used by the address resolver before /etc/hosts. These can be manipulated through Directory Utility or dscl.

dscl localhost -create /Local/Default/Hosts/www.myhost1.com IPAddress 127.0.0.1
dscl localhost -create /Local/Default/Hosts/www.myhost2.com IPAddress 127.0.0.1
dscl localhost -create /Local/Default/Hosts/www.myhost3.com IPAddress 127.0.0.1
dscl localhost -create /Local/Default/Hosts/www.myhost4.com IPAddress 127.0.0.1
ephemient
  • 25,622
1

I haven't seen this behavior on Lion and I have a custom /etc/hosts.

Is the whole file reverting, or the top few lines for common entries like 127.0.0.1?

It might be possible that certain operating system actions re-apply the top few "standard" /etc/host lines, so I might try adding lines, instead of editing those...

Mine looks something like this and has never magically changed:

##
# 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

# route some web requests locally
127.0.0.1       www.myhost1.com
127.0.0.1       www.myhost2.com
127.0.0.1       www.myhost3.com
127.0.0.1       www.myhost4.com
spkane
  • 143
1

I witnessed the same problem as Jimmy Z. Really frustrating, especially since I have numerous custom hosts. It wasn't until the 3rd or 4th occurrence that I realized it was always happening after a reboot.

I finally stumbled upon this blog post which suggested to delete and recreate the hosts file. I followed the suggestions, rebooted and was relieved to find /etc/hosts in the pre-boot state. Here's a recap of what I did.

cat /etc/hosts | pbcopy 
sudo rm /etc/hosts{,.ac}
sudo touch /etc/hosts
sudo su root -c 'pbpaste > /etc/hosts'