Editing your hosts file on Yosemite

Lately, I had to edit my hosts file. I’m not used to do this often but I have done it before. I was surprised when just a small change like this disable my localhost environment. Every time I opened a project in httdocs I had a:

The server at localhost can’t be found, because the DNS lookup failed.

My first reflex was to erase the changes and get back to my original file but nothing happens. After that I thought that there was something wrong with my ISP, so I changed my DNS for google DNS, but nothing. After that, It was like a Saturday night between 4-6am: you have an idea where you were, who you was with, but you can not really remember how everything happens.

After start making a backup (I know, even my girlfriend told me I was being paranoid), I found someone in stackoverflow saying that he have had the same problem once editing this file with Sublime. Then I said, what if I just replace my host file with a backup copy? (you could say that it should have been my first reflex, and I agree, but they look the same!).

Once I did this, my localhost came back. Apparently, there is some encoding conflict, you should edit your hosts file with other software than Sublime or taking any kind of precaution (I don’t know which one).

So, I decided to make it console style:

$ sudo nano /private/etc/hosts

Here, I had access to my host file. I add the lines I needed. Then control-o to save and control-x to close.

After that, don’t forget to flush the DNS cache to have your changes immediately

$ dscacheutil -flushcache