17

On my Mac (running OS 10.6.8), I can edit my hosts file with sudo vi /etc/hosts, but changes I make don't seem to persist through restarts or possibly awaking from sleep mode.

Is there a way to make these changes persist?

Here's a sample change adding an override for www.example.com:

$ cat /etc/hosts
##
# 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

1.2.3.4 www.example.com

Update: I am also using Cisco AnyConnect VPN software, and that turned out to be the clue. See Daniel's answer below.

Update 2013-05-20: This behavior was fixed/changed by Cisco in AnyConnect v3.0.5080 (or higher), released in Spring 2013. Release Notes - search for hosts.ac.

4
  • 1
    possible duplicate of /etc/hosts getting reset in Lion
    – Daniel Beck
    Jan 26, 2012 at 18:18
  • 1
    @user606723 /etc is a symbolic link to /private/etc.
    – Daniel Beck
    Jan 26, 2012 at 18:18
  • Brian, are you using Cisco VPN software? Do you have the file /etc/hosts.ac?
    – Daniel Beck
    Jan 26, 2012 at 19:42
  • Bingo! I am in face using Cisco AnyConnect VPN. I updated /etc/hosts.ac file and it now persists the changes. Please put this as an answer and I'll accept it.
    – BrianC
    Jan 26, 2012 at 19:54

5 Answers 5

17

You are using Cisco AnyConnect software, which will overwrite /private/etc/hosts with /private/etc/hosts.ac. Just edit that file instead.

21

I've had the same issue. In my case I'm using F5 VPN client. You shouldn't edit /etc/hosts while VPN client is running, because these changes will be erased when you shut down the VPN client. To make your changes permanent, edit /etc/hosts file before you start your VPN client.

2
  • For me, as F5 VPN this was very useful.
    – robert
    Sep 11, 2018 at 14:58
  • Is there any other way without disconnecting VPN? Sep 24, 2020 at 7:26
4

I also had this problem with the Pulse Secure VPN client, which makes a file /etc/pulse-hosts.bak that you should edit too. Editing /etc/hosts before launching Pulse also works.

3
  • 1
    What good does it do to edit a backup file? Aug 5, 2018 at 22:13
  • would never guess that - worked for me !
    – Ricky Levi
    May 18, 2020 at 7:12
  • 1
    Basically, the flow goes like this, when Pulse opens its connection it goes: cp /etc/hosts /etc/pulse-hosts.bak Then it adds whatever edits it needs, to set up the Pulse connection. Then when it's all done, it goes: mv /etc/pulse-hosts.bak /etc/hosts So any changes you make to /etc/hosts will get wiped out, if Pulse was on when you made the edits.
    – turiyag
    May 22, 2020 at 21:03
1

Thanks to @Danial's explanation. Using a wrapper script to edit hosts rather than edit it directly would ease your pain a lot.

#!/bin/sh
ORIG=/etc/hosts
SUCKER=/etc/hosts.ac
sudo vim $ORIG
pid=$!
wait $pid
sudo cp $ORIG $SUCKER
echo "$ORIG copied to $SUCKER"
0
0

Most of the VPN client creates a backup file of the /etc/host when you start the VPN.

To keep your changes permanently, simply change the host file without a VPN connection.

Once you start the VPN, the backup file will be created with your changes in, and any VPN client overriding the host file would still keep your changes in.

Common backup file name by client:

  • Pulse Secure Client - pulse-hosts.bak
  • Cisco Any Connect - hosts.ac
2
  • This appears to be basically a more verbose version of the accepted answer — and you misspelled “Any”. Apr 30, 2020 at 22:48
  • Thanks... The accepted answer is suggesting a WAR to update the backup file. This answer is to update the host file while the VPN is disconnected. This solution will work for any VPN Client
    – xxnations
    May 1, 2020 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .