How to Change your Hosts File?

Hosts file maps the domain names (hostnames) to the IP addresses. It is present in plain-text format and is used by all operating systems such as Linux, Windows, and MacOS. You don’t have to make any changes to DNS settings if you are editing the host’s file.

Let’s understand this with an example-

  1. First, you enter serverguy.com into your browser tab and press enter.
  2. After that, the domain is translated to its corresponding IP address.
  3. The OS first checks the host file related to serverguy.com.
  4. If they found no entry for this domain in the host’s file.
  5. OS will then go to the DNS servers to the specified domain names.

Using this mechanism, you can change the DNS settings for a domain only on the machine you are working on.

Why do you need to edit the host’s file?

Modifying your host’s file enables you to test your website without the need for a DNS record. For example, you are migrating your website or ensuring it is functional before pointing it to the new server.  It can also be used to block certain websites from running on your computer. 

To modify your local host’s file you have to create an entry for the domain to which you want the IP address to point for the given website.

For example, adding the following line, to your host’s file, will point the records www.serverguy.com and serverguy.com to the IP address 1.2.3.4:

1.2.3.4 www.serverguy.com serverguy.com

Let’s learn how to change your host’s file on the following operating systems:

  • Windows® 10, Windows 8,
  • Windows 7, and older versions
  • Linux®
  • MacOS® X versions 10.0 through 10.1.5
  • MacOS X versions 10.6 through 10.12

When you add the information of the domain and save the changes, your computer will automatically send the domain to the specified IP address. After you have done the migration or testing, remove the changes quickly as it can be difficult to understand why your DNS is not behaving as expected on different computers.

Windows

If you are working on Windows operating system, you have to use Microsoft Notepad as an administrator.

Windows 10 or Windows 8

  • Press the Windows key and type in Notepad in the search bar.
  • After Notepad appears in the search result, right-click on it.
  • Select Run as administrator.
  • Go to File > Open.
  • Choose and open the following file: c:\Windows\System32\Drivers\etc\hosts
  • Make the desired changes to the file.
  • Click on File and then press Save.

Windows 7, and older versions

  • Click Start > All Programs > Accessories.
  • Right-click on the Notepad
  • Select Run as administrator.
  • Go to File > Open.
  • Choose and open the following file: c:\Windows\System32\Drivers\etc\hosts
  • Make the desired changes to the file.
  • Click on File and then press Save.

Linux

  • Open a terminal window.
  • Type in the following command in your favorite text editor:

    sudo sensible-editor /etc/hosts

  • Enter your sudo password.
  • Skip over the existing lines and make your modifications at the bottom of the file. Add your new mappings after the default mappings.
  • Make your changes to the file.
  • Press Control-X and then enter y.

Mac OS X

  • Open a terminal window.
  • Open the /etc/hosts file by issuing the following command in the terminal prompt:

    sudo nano /etc/hosts

  • Type your sudo password when asked.
  • Edit the /etc/hosts file.
  • Skip over the existing lines and make your modifications at the bottom of the file. Add your new mappings after the default mappings.
  • Make your changes to the file.
  • Press Control-X and then enter y.
  • You can get the results of your changes quickly by flushing your MacOS DNS cache:

    dscacheutil -flushcache


    ServerGuy offers customized and dedicated hosting services for eCommerce store owners so that their website loads fast and they don’t lose potential customers. You can contact us directly or leave a comment below. We will get back to you asap.

Was this post helpful?