Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I just got a Mac computer (OSx v10.6) to test my web site design. Web site is running on the local network, and in windows computers I use "hosts" file to map its network IP address to domain name, like

192.168.1.7 www.sitename.com

How can I do that on Mac?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

OSX does have a /etc/hosts file but it first uses directory services to find hosts. These are maintained via the dscl command

This web page provides examples of use to maintain the lookups.

Essentially, you just need to run the following command in Terminal:

sudo dscl localhost -create /Local/Default/Hosts/sitename.com IPAddress 127.0.0.1
share|improve this answer
1  
Added the most important information from the web page to your answer so it will still work even if that web site goes down or changed. Hope you don't mind. –  Daniel Beck Feb 13 '11 at 19:27
1  
Actually, directory services reads both /var/db/dslocal/nodes/Default/computers/* (which DS calls /Local/Default/Hosts, and is where these instructions put host entries) and /etc/hosts (which DS calls /BSD/local/Hosts), so doing this is equivalent to making an entry in /etc/hosts. (Although it checks dslocal before /etc/hosts, so if there's a conflict, that'll take precedence.) –  Gordon Davisson Feb 14 '11 at 2:46

There's a hosts file in /etc/, a path you can access in Finder using the Go » Go to Folder menu item.

Right-click it and Get Info to change its permissions at the bottom, so you can edit it. You might also need to give yourself permissions for the parent folder /etc so saving works. It's a bit tricky with some GUI editors.


Alternatively, sudo vim /etc/hosts in Terminal, if you're comfortable with vim (or any other CLI editor).

enter image description here

The file itself works like you'd expect it to.

share|improve this answer
1  
Ugh. Those colors. Sorry about that. –  Daniel Beck Feb 13 '11 at 18:44
1  
You're fired :-) –  user3463 Feb 13 '11 at 19:11
    
You might also need to give yourself permissions for the parent folder /etc so saving works. No, just no. That's the worst advice you can ever give to anyone. –  Lie Ryan Dec 13 at 11:41
    
@LieRyan I'd have expected it's obvious that any permissions users give themselves here should be taken away after everything works. Isn't it? –  Daniel Beck Dec 13 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.