How to edit the hosts file in MacOS, Windows and Linux

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.

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

Editing the hosts file in MacOS

  1. Open the Terminal.app
    Start typing Terminal on the Spotlight, or by going into Applications -> Utilities -> Terminal.
  2. Open the hosts file
    Open the hosts by typing on the Terminal that you have just opened:

    $ sudo nano /private/etc/hosts

    Type your user password when prompted.

  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. 127.0.0.1 – localhost). Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing! You can navigate the file using the arrow keys.
  4. Save the host 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.
  5. Flush the DNS cache
    You can issue a simple Terminal command to flush the DNS cache, in order to force your host file changes to take immediate effect:

    $ dscacheutil -flushcache

Editing the hosts file in Windows

  1. Locate the hosts file
    Windows 95/98/Me  c:\windows\hosts
    Windows NT/2000/XP Pro   c:\winnt\system32\drivers\etc\hosts
    Windows XP Home/Vista/7  c:\windows\system32\drivers\etc\hosts
    (you may need administrator access for Windows NT/2000/XP/Vista/7)
  2. Open the hosts file with notepad
  3. Edit the hosts file
    Add a new line to the hosts file for the desired mapping, that looks like the following:

    127.0.0.1      www.local-host.com

    You type the IP address you want to map, press the tab key and then the host names that will lead to the specified IP address. You can type multiple host names, separated by a space

  4. Save your changes
  5. Flush the PC’s DNS cache
    Type the following in a command prompt window (Open a command prompt by typing cmd in Start>Run)

    ipconfig /flushdns

    If the changes don’t work, try rebooting your PC

What can I do if Hosts isn’t working under Windows 2000 or XP Pro?
First, you should try using “127.0.0.1 localhost” at the top of your hosts file. If that doesn’t work, try converting the entire Hosts file from 0.0.0.0 to the 127.0.0.1 format.

Those of you who use Windows 2000/XP Pro and use the very large version of the Hosts file may experience system slowdowns and slow connections. Most often, this is noticed by those with dialup connections, but may occur with broadband connections as well.

Windows 2000/XP Pro also includes a service known as the DNS cache. You can locate it in your services listed as “DNS Client”. To use Hosts you may need to stop and disable this service. Some machines seem to run Hosts without disabling this service. It is safe to turn it off and disable it. You will likely never notice it is gone. As far as I can tell, no other Windows operating system offers such a service.

Sometimes extra spaces after the host names may cause the file not to work.

Editing the hosts file in Linux

  1. Locate the hosts file
    The file is usually found in /etc/hosts
  2. Add your entry to the hosts file
  3. Make sure this file is used for host name lookups
    Open the file: /etc/host.conf
    This file should have at least the following line: order hosts,bind
    That has host lookups use the hosts file before doing a DNS query with bind.
  4. Try your entry in the web browser (you may need to reboot your PC for it to work)
Panagiotis

Written By

Panagiotis (pronounced Panayotis) is a passionate G(r)eek with experience in digital analytics projects and website implementation. Fan of clear and effective processes, automation of tasks and problem-solving technical hacks. Hands-on experience with projects ranging from small to enterprise-level companies, starting from the communication with the customers and ending with the transformation of business requirements to the final deliverable.