Code Snippets: Edit hosts file in Mac OS X 10.5 Leopard
I do a lot of development in Leopard. Typically for most projects there is a sandbox set up to mirror the production server. I’ve found the easiest way and best practice to work on the sandbox is to change the IP to forward to the production domain.
What you do is open Terminal, which is found in Applications > Utilities. Then type the following:
sudo nano /etc/hosts
Terminal will then ask you for your password, because you have invoked the security privileges of the superuser. Type your password in. Nano will then open. I know you can use VI but for such a simple task, I prefer nano.
Next you will be presented with:
##
# 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
Just a few things to note. A hash, #, is a comment. Don’t mess with those first 4, unless you know what you are doing.
Move the cursor down in nano using the arrow keys. Now you can type in an IP address of the staging server and have it appear to be on the production server domain name. Our theoretical staging server is on IP address 111.111.111.111 and our production server is on IP 11.111.111.112, with the domain www.production.com
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
111.111.111.111 www.production.com
Now save. This is done by hitting ctrl+o or ^o. Hit enter to name it hosts, then hit ^x to exit nano. In order for this to work the cache needs to be flushed. There is a new command for this in 10.5 Leopard.
dscacheutil -flushcache
Now you can go to your staging server via your production domain name. Handy. Just be careful though. Don’t accidentally make edits to production, or think you’re making edits on production but you’re making them to staging.
Tags: 10.5, dscacheutil, hosts, leopard, os x
Comments (13 comments)
this is the clearest explanation of this process i have seen – thank you!
JW / July 16th, 2008, 1:51 pm / #
Agreed, thx for a straightforward, un-bloated explanantion!
Josh / July 30th, 2008, 11:43 am / #
Thank you!
First explanation that worked for me. Clear and to the point without fluff!
Andreas / August 12th, 2008, 11:54 am / #
Excellent explanation, thanks very much!
Ian Barnwell / August 15th, 2008, 6:56 am / #
Excellent work… many thanks for your help.
Edgar G. / August 16th, 2008, 11:04 am / #
Great explanation. Thanks You MUCH
Marcin Michalek / September 24th, 2008, 8:13 pm / #
Okay, I`ve got to the nano hosts file but everytime I try to move to the bottom of the page, the cursor wont move with the arrow keys.
You`re talking about the arrow keys on my keyboard, right?
The cursor just stays at the beginning of the file and wont let me click or move anywhere else.
Tara R. / November 21st, 2008, 9:25 am / #
@Tara R. Yeah you have to use the arrow keys on the keyboard.
Dave / November 24th, 2008, 12:15 am / #
not only perfect. It works!
M. Leon / December 5th, 2008, 1:38 pm / #
Thx so much! You saved me from having to dual boot into win every time
Luke / January 12th, 2009, 6:27 pm / #
[...] – bookmarked by 2 members originally found by wfryer on 2009-01-09 Code Snippets: Edit hosts file in Mac OS X 10.5 Leopard http://rag.nu/2008/07/06/edit-hosts-file-in-mac-os-x-105-leopard/ – bookmarked by 2 members [...]
Bookmarks about Leopard / February 1st, 2009, 11:15 pm / #
Hi!
You can also try hosts file manager to simplify the task.
http://clockwise.ee/gasmask/
Siim / February 5th, 2009, 12:00 pm / #
This worked perfectly!
Thanks.
Steve Dossett / March 4th, 2010, 10:07 pm / #
Post a comment