How to edit the hosts file in Mac OS X 10.7 – Lion

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address in the browser, the system first checks if there is a relevant entry in the hosts file and if exists gets the corresponding IP address. If no entries exists it resolves the IP via the active connection’s DNS servers.

The hosts file can be edited to block certain hostsnames, like ad-serving/malicious hosts, or used for web development purposes, i.e. to redirect domains to local addresses.

Step 1 – Open the

Either start typing Terminal in the Spotlight search, or goto Applications > Utilities > Terminal.

Step 2 – Open the host file

Open the hosts file by typing the following in the Terminal window:

$ sudo nano /private/etc/hosts

Type your user password when prompted.

Step 3 – Edit the hosts file

The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. – local host). Simply append your new mappings underneath the default ones.

Step 4 – Save the hosts file

When done editing the hosts file, press Control+o to save the file.

Press Enter on the filename prompt, and

Control-x to exit the editor.

Step 5 – Flush the DNS cache

You can use a simple Terminal command to flush the DNS cache, and have your host file changes take immediate effect. Using the open Terminal window, then the following command:

$ dscacheutil -flushcache

Your new mappings should now take effect.

Other Mac OS X operating systems:
How to edit the hosts file in Mac OS X 10.8 – Mountain Lion
How to edit the hosts file in Mac OS X 10.6 – Snow Leopard


  1. When I try to edit the hosts file I get a blank window with no file on the bottom instead of the number of lines read. I do not see the text of the hosts file even though I can read it with Text Edit.

  2. Hi sir,
    I want t say that after entered in the terminal as type commade
    :sudo nano /Private/etc/hosts
    But it shows To proceed ,enter your password,or type ctrl -c to abort -> but i was typing in the note pass i have to paste it here but here courser not moved any where it is not accept it ..which pass word i hav to type i mean system administator or company passwor……………….will you suggest me plezzzzzzzzzzzz

  3. i try this $ sudo nano /private/etc/hosts in my terminal but it not work it give me this msg
    WARNING: Improper use of the sudo command could lead to data loss
    or the deletion of important system files. Please double-check your
    typing when using sudo. Type “man sudo” for more information.

    To proceed, enter your password, or type Ctrl-C to abort.

    aby is not in the sudoers file. This incident will be reported.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>