Recently at my job I’ve started doing some work on a mac (yay!) which means I now get to use some of the fun extra mac tools that I don’t have available on linux, but it also means having some things that are more difficult to configure, strangely.
The first anomaly I’ve encountered so far was in adding host entries for sites that are not in our DNS. These are sites that are either not in DNS yet but are pending or sites that will never be added to DNS due to security reasons or because they are not intended to be used by anyone but us programmers.
##The Problem
The first thing I noticed when editing the /etc/hosts
file on OS X Lion for
the first time is that when I rebooted the machine all changes I made would
disappear completely. Sometimes they would even change if I didn’t reboot. When
I opened up /etc
I noticed there was another file called /etc/hosts.ac
which
I had never seen before. Originally I thought this was some sort of system
change that Apple had made, but I later on discovered that this was actually the
work of Cisco’s AnyConnect VPN client. Cisco automatically replaces your
/etc/hosts
with its own /etc/hosts.ac
##The Zinger Here’s the zinger: OS X does not know about hosts.ac, nor does it care. So what do we do in this situation? Well after having to edit both files for a long time I decided it was time to do something about it. Here is my solution.
Create a vim autocmd to save all changes made to /etc/hosts into /etc/hosts.ac
also. Throw the following snippet into your .vimrc
:
Don’t forget to source your vimrc again after. Now when you save your hosts file it will automatically copy it to your hosts.ac, now Cisco and OS X can be happy together!
Note: There is one downside to this approach, unfortunately. I couldn’t
figure out how to hone it specifically on the hosts file in /etc/ so this
autocmd will actually trigger on any file in any location named hosts
.
For more information about manipulating file names in vim check out
:h filename-modifiers