7 Ways to Use Hosts File on Your Computer

The web is a giant network. There are many things happen in the backend when you type a domain name (website address) on the browser’s address bar and hit enter. One of the first things is that your browser will check the mapping on your computer’s hosts file. In this article, let us explore more on hosts file, where you can find, how to edit and how to use it properly for different reasons.

Related: What is IP address and formats of IP addresses?

What is Hosts File?

A hosts file is a computer system file that maps human-friendly hostnames (domain names) to their IP address. It uses IP address in IPv4 or IPv6 format to resolve the hostname and the browser can quickly connect to the hosting server.

  • The hosts file is a text file and you can edit it using a text editor such as Notepad.
  • Generally hosts file is hidden and does not have any extension.
  • You can define the mapping of websites to particular IP addresses.
  • You need to have administrator access to edit hosts file on your computer.
  • On hosts file, you need to maintain all variations of website addresses like http, https, www and without www versions.
  • Between IP and hostname, you can use either space or tab.
  • Use # at the start to enter comment or to make an entry invalid.

Where to Find Hosts File in Windows 10?

By default, you can find Windows 10 hosts file using the path C:\Windows\System32\drivers\etc\hosts.

Hosts File Location
Hosts File Location

To open the file, right-click “hosts” and open with a text editor like Notepad or Notepad++.

Example Hosts File
Example Hosts File

Where to Find Hosts File in Mac?

Follow the below instructions on macOS to find hosts file.

  • Go to Finder and press “Shift + Command + G” keys to open “Go to Folder…” option.
  • Type “/private/etc/hosts” and click “Go” button.
  • You will see the hosts file’s location.
Hosts File in Mac
Hosts File in Mac

How to Modify Hosts File in Windows 10?

To modify the hosts file;

  • Search for “Notepad” on the Windows search box. Right click “Notepad from the results and select the “Run as administrator”.
  • Browse through the above path to locate the hosts file, select “hosts” and click the “Open” button.
Open File in Notepad
Open File in Notepad
  • You can make changes at the end of the text and save the file. The changes will take effect immediately.
  • You can modify the Windows hosts file for different reasons as listed below.

How to Use Modify File in Mac?

In order to edit hosts file on Mac, you need to open Terminal app.

  • Type “sudo nano /private/etc/hosts” and hit enter.
  • Enter the password and hit enter.
  • You will see the hosts file opens within Terminal app.
  • Use keyboard keys to move the cursor to the last line and start entering your details.
Modify Hosts File in Mac
Modify Hosts File in Mac
  • When done, press “Command + X” keys to quit the editor.
  • Press Y to save your changes and then hit enter to return to Terminal app.

7 Ways to Use Hosts File on Your Computer

While the DNS remains the standard domain name resolution service over the internet, the hosts file overrides the DNS servers. Therefore, you can use the hosts file for various reasons including redirecting or blocking websites, creating local domains and sites shortcuts among other purposes.

1. Editing Hosts File to Block a website

  • To block any site from hosts file, you only need to map the hostname to the localhost IP (127.0.0.1) or a full-zero’s IP address (0.0.0.0) followed by the sites domain name.
  • For instance, to block users from access Twitter, add one of the following entries at the end of the hosts file:

127.0.0.1 twitter.com www.twitter.com
0.0.0.0 twitter.com www.twitter.com

Block Websites in Hosts File
Block Websites in Hosts File

Note: ensure there is a space between the IP address and the hostname and no ‘#’ before the IP as this would deactivate the entry.

2. Re-directing a Website Using Hosts File

You can also redirect the website to a particular domain. For example, you may edit the hosts file such that whenever a user tries to access Twitter, they are redirected to the company’s site or any other website.

  • First, you need to know the IP for your target website.
  • Then use the hosts file to map the IP to Twitter.com. To redirect Twitter to Google.com IP address 216.58.223.110, enter “216.58.223.110 www.twitter.com twitter.com” at the end of the hosts file and save.
Redirect Site in Hosts File
Redirect Site in Hosts File

3. Create Shortcuts for Websites or Intranet Services

You can also modify Windows hosts file to create shortcuts for public or internal sites or web services.

  • For instance, if you have a server with an IP, 192.168.1.10 on your network, it will be easier to remember a descriptive name than the IP address.
  • To create a shortcut for the device, you only need to map its IP to your preferred name with a .com extension.
  • Add “192.168.1.10 mydevice.com” and save the file.
Create Shortcuts in Hosts File
Create Shortcuts in Hosts File
  • When you access “mydevice.com” from the browser, the server interface will open.
  • You can create a unique shortcut for any site depending on your preference.

4. Testing Network / Web Servers

When you are running a web development server on your local network, it will be safe to test its functionality before publishing it live.

  • For instance, if you are running a web server (IP: 192.168.0.11), hosting two sites mysite1.com and mysite2.com, you can add the following entries to the hosts file:

192.168.0.11 mysite1.com
192.168.0.11 mysite2.com

  • Save the file and try to access the two sites from the browser.
Testing Sites in Hosts File
Testing Sites in Hosts File
  • If you can access the two sites, then the server is working properly. Thus, you can now deploy the server online.

5. Content Filtering and Ads Blocking

  • You can block Ad networks or unwanted sites by mapping the site to the localhost IP (127.0.0.1).
  • This will point back to your own PC blocking access to known malicious or Ads sites.

6. Adding Websites to Hosts File to Improve Browsing Speed

Add a site to the hosts file can increase the browsing speed. This is simply because the computer doesn’t need to query DNS server for IP and waste time waiting for a response.

  • Just enter the sites IP address and the domain name separated by a space and save the file.
  • However, this is not advisable as site owners may change the IP from time to time, making it impossible for you to connect to the site.
  • Additionally, blocking adds speeds up the browsing speed as you don’t have to wait for Ads to load.

7. Preventing Malicious Attacks

The hosts file can be a target for malicious attack. Attackers can use viruses, PUPs and malware to modify the hosts file, redirecting you to malicious sites or hijack your sites.

  • You can prevent such unauthorized edits by changing the properties for the “hosts” file.
  • Go to “C:\Windows\System32\drivers\etc\”, right-click “hosts” and select “Properties”.
File Properties
File Properties
  • Under the “General” tab, tick the “Read-only” checkbox to disable editing.
  • Click “Apply” and then “Ok”

Conclusion

A hosts file is one of the powerful and useful tools for Windows users, allowing you to improve your online experience through the above uses. When editing the hosts file ensure that you have administrative rights.

Editorial Staff at WebNots are team members who love to build websites and share the learning with webmasters community. Follow us on Facebook and Twitter.

4 thoughts on “7 Ways to Use Hosts File on Your Computer”

Leave your response.

  1. Another question; Is there a similar ‘hosts’ concept under Apple iOS—iPhone, iPad—and can that be similarly modified by the user/owner? (without jailbreak, app download or similar machination)
    Thank you.

    Reply
  2. Very helpful thanks. Didn’t know you could include multiple domain variations on the same line.

    Q: Possible to redirect to a local file something like this?
    ‘hosts’;
    # this is my desired ‘hosts’ file entry
    # my host-name is “myhost” and my local IP is 192.168.6.9, fixed on my router
    192.168.6.9\C:\test\mywebpage.htm web-site.com
    # OR
    \\myhost\C:\test\mywebpage.htm web-site.com
    # OR
    file:///C:\test\test\mywebpage.htm web-site.com
    #

    Reply
    • There should be an association with the two addresses you type in the hosts file like IP and host name. You can’t redirect local HTML file to a public website hosted on a web server.

      Reply

Leave a Comment