How do you add an entry to a Mac's host file?

Posted:
in Genius Bar edited January 2014
Okay, this is something that's ridiculously easy to do in Windows that I'm sure can be accomplished easily in OS X if I can just find the documentation. Here's what I'm trying to do:



I'm working with a Web developer whose test Web server is similar to our production Web server, except that new Web pages can be previewed on the test system while the "old" pages are still being served on the production system. (This is all on remote servers, we're not hosting anything on our local computers.) When the developer wants to show me a new page design, he tells me to change my hosts file to include the IP address of his test system on our local network, for example, 10.50.1.33.



On my Windows box at work, I open a "hosts" file in a text editor, and just add "10.50.1.33 test system" to the file. Save it, relaunch any browser, and voila: I type in www.domain.com/test_page (whatever the URL is) and the test system page appears. When I want to see the production page, I just open the "hosts" file, comment-out the IP address for the test system, save the file, relaunch a browser and I'm back to seeing the production environment.



Unfortunately, I can't figure out how to achieve this on my Mac. It doesn't seem that OS X has this capability documented for use by humans. NetInfo Manager might do it, but there's no help available via Mac help and the interface leaves a lot to be desired. I've searched Google and Apple's support forums with no luck. I did find a "hosts" file in /etc, but it's locked under normal circumstances.



So, does NetInfo Manager handle this? Terminal? Do I need to be root? With the Mac's UNIX underpinnings, this has *got* to be a simple procedure. But how?

Comments

  • Reply 1 of 1
    MarvinMarvin Posts: 15,326moderator
    You can do it a number of ways. You can change permissions on your hosts file to give you write access. You can do this in the Finder by using the go menu and doing go to folder /etc and change the permissions of hosts in the getinfo window. Sometimes you have to be careful because OS X likes certain permissions set correctly and I'm not sure if this is one of them.



    You can also edit the file by launching Textedit using sudo. Unfortunately, when you do something like sudo open -e /etc/hosts, the sudo only applies to the open command and not to Textedit so you have to do it the long way, which is:



    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts



    You can copy and paste that into the terminal. For tedious commands, you can set up an alias in your terminal and assign a word.



    To do this, you would open your bash profile using:



    open -e ~/.profile



    and type something like:



    alias edithosts='sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts'



    and save the file. Then whenever you type edithosts into the terminal, it will run the command. You need to make a new terminal window for the alias to come into effect.
Sign In or Register to comment.