Editing Hosts File in Mountain Lion

When doing website development for a domain that is not yet pointed to the Linux/Apache server you are developing on, you can often point to the directory with a URL consisting of the IP address followed by a tilde, which is this squiggly thing ~ and then the FTP user name like:

But if you are working with backend administration directories like CPANEL and wp-admin, you need to set up a DNS reference within your “hosts” file. On OSX this is either accessed using the TERMINAL application (which is a non-graphical interface that makes me feel really computer-geeky, and which is in the Applications/Utilities folder).

In OSX Lion I would simply open the terminal and type: sudo nano /private/etc/hosts, which opens the Hosts file using a program called nano. You’ll probably be looking at a file like this:

##<br /> # Host Database<br /> #<br /> # localhost is used to configure the loopback interface<br /> # when the system is booting. Do not change this entry.<br /> ##<br /> localhost<br /> broadcasthost<br /> ::1 localhost<br /> fe80::1%lo0 localhost

Add your DNS routing like this: www.domainname.com domainname.com

Then Flush the Cache using this command:

2dscacheutil -flushcache

But in Mountain Lion that doesn’t work! It may be because the hosts file has a “sticky” protecting it from being edited. SO…

Here’s the process that finally worked.

1) First you need to log-in as the Root User in Terminal using the following command:

sudo su &#8211;

Enter your OSX password

2) Then you need to remove the “sticky” lock assigned to the hosts file that prevents people from modifying it:

chflags nouchg /private/etc/hosts

3) Edit the hosts file as per usual via the following command:

sudo nano /private/etc/hosts

“Control O” to Save

“Control X” to Exit

File will be similar to this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
## localhost broadcasthost
::1 localhost
fe80::1%lo0 localhost www.domainname.com domainname.com

4) IMPORTANT: LOG OUT of root:


5) Flush your cache to ensure the new hosts file is current:

sudo killall -HUP mDNSResponder

6) Log back in at root user again:

sudo su &#8211;

7) Go ahead and reapply the “sticky” lock so the hosts file doesn’t modify itself:

chflags uchg /private/etc/hosts

8) log out as root:


9) Log Out as user:


You should see message:

[Process completed]

I hope this saves you some time. Have a great day.