terminal command ref book

My new reference ebook: covering the mac terminal, and with primers on bash, Automator, AppleScript, and other scripting tools. It also features a guide to the /etc/hosts file. Check out the info page here:

Mac Terminal Ebook

If  you find your wi-fi network is getting slow, there may be interference from another network. If another Wi-Fi network nearby uses the same channel, or even a close channel, the two networks will collide, reducing speed for everyone. Here are some tips about how to troubleshoot Wi-fi… (more…)

Did you know that future updates to Minecraft are always previewed in weekly snapshots? Big parts of the ‘pretty scary update’ were available for weeks before it was officially released! Here’s how you can get access to the cool stuff in upcoming versions of Minecraft on your Mac… (more…)

/etc/hosts file on Mac OS X

November 29, 2011 — 7 Comments

The /etc/hosts file on the Mac can be hard to find. Here are some tips for finding and editing it, and what you can do with it. (more…)

I’ve covered the ‘history’ command before, but here are two more useful Mac OS X Terminal commands…

I’ve been using the Ctrl+ R trick (Hit Ctrl + R, start typing part of the command you want. If you don’t like the first suggestion hit Ctrl + R again to show the next one). This is really handy and fast and it will return matches from the middle of the string.

And I just found another great Terminal command here: Discover the last used command beginning with anything without executing it… this one is a bit harder to use:


…where [needle] is the first character(s) of the command you’re looking for. It’s not as flexible because it only shows the most recent match instead of cycling through the results, but could still come in handy.

Web Developers, Administrators of Mac Networks, and anyone who likes to noodle with OS X will probably eventually need to add hosts file entries. I use them when I’m testing a moved and/or changed version of a website at new hosting before I move the DNS.

I used to add hosts file entries manually then wonder why they didn’t seem to work. I tried rebooting, logging out, etc with varying success, until I learned this trick. On the Mac you must flush the DNS cache for the changes to take effect. Mac OS X used to have a friendly utility called NetInfo Manager, but that seems to have been removed in Leopard, so we’re using terminal commands.

Open a terminal sesion and type:
sudo nano /etc/hosts

Hit the down arrow until you get to the end of the file, and add your hosts file entry, for example: myintranetserver.mydomain.com
… I put in an extra line break at the end. I don’t think it’s really necessary but it’s a habit from having to use Windows, which would ignore the last line in a hosts file.

then flush the cache:
sudo dscacheutil -flushcache

…the name should now resolve! Test it:
ping myintranetserver.mydomain.com

Update: As of OS X 10.5 (aka Leopard) you don’t need to flush the DNS: changes to the hosts file take effect immediately. There are other cases where it is necessary to flush DNS; be aware that as of 10.7 (aka Lion) you should use:
sudo killall -HUP mDNSResponder


Other Info

One thing that Windows, for all its annoyances, did get right is that changes to the hosts file (in ‘c:\Windows\system32\drivers\etc’) are reflected immediately. I’d like to see Apple do something about this in OS X, perhaps just flushing the cache automatically when /etc/hosts changes. Update: as of OS X 10.6 this works; changes to the hosts file take effect immediately.

Another handy use for /etc/hosts is to set up mapping of virtual server addresses. For example, I use MAMP on my development Mac, and create new virtual hosts entries in Apache for each site I’m working on. Then for each I just add a new entry in the hosts file, e.g.: devclientsite7.leftcolumn.net
… then I just use devclientsite7.leftcolumn.net:8080 in Safari or Chrome to access the development site. It means my development sites don’t have to be in subdirectories, instead I just make sure the sites use a config file to figure out what their address is, and I make sure that config file is updated when the site is moved to a live system.

On OS X Tiger (10.4) and probably earlier, there was some strange behaviour with hosts file entries. You could add an entry and it would take effect, but if you had no network available, the name would not resolve and could not be used, even if it resolved to! So you have the bizarre situation of putting in addresses for virtual hosts as described in the previous paragraph, all of which resolve to, but still not being able to use them for testing and development when not connected to the Internet. If anyone can enlighten me as to how this was anything other than a bug in OS X I’d be interested…

You can also use hosts file entries to block ads (by resolving common ad server addresses to e.g.: You can download huge lists of ad servers for this purpose. This is a good idea when you’re stuck using Internet Explorer on Windows, especially on dial-up.

terminal command ref book

If you like this page and want to know more about the Mac Terminal, check out my ebook. It contains an improved version of the hosts file info here, plus a reference to commonly-used Terminal commands. Get to grips with the Mac Terminal and scripting tools now. More Info >