KNOWNHOST BLOG

How to View a Hosts File Location & Edit

Hosts files are an essential tool in cataloging the many servers and other devices your computer connects to. It’s commonly used by those involved in IT and web development, for privacy, security, and greater control over online activity.

But what exactly is a hosts file? And how can it be edited to access these benefits?

This guide is an essential tool for those looking to understand how to find a computer’s hosts file location, and how to edit it.

Business professionals are managing files and folders that store corporate data digitally,Document and database management concepts,record keeping database technology file access document sharing

What is a Hosts File? 

A hosts file is a system file on a computer that maps the hostnames of all connected devices (e.g., a web server) to corresponding IP addresses. The operating system uses the file to resolve domain names before sending requests to a Domain Name System (DNS) server.

These files have several important uses, including local DNS resolution – mapping domain names to specific IP addresses at the individual computer level – troubleshooting network issues, and local development testing.

Hosts files have been used since the days of ARPANET – the precursor to the modern internet – to map hostnames to IP addresses on computers. However, the development of DNS allowed for more scalable and efficient mapping across the entire internet.

While DNS has since become the main method for domain name resolution on the internet, hosts files still provide many potential uses today, like:

  • Local Development & Testing: Developers can point a live domain (e.g., mywebsite.com) to their local machine (127.0.0.1) to test sites before making changes public.
  • Website Migration Testing: Temporarily point your domain to the IP of a new server to verify everything works before changing DNS for the world.
  • Blocking Unwanted Sites: Map distracting or malicious websites to a non-routing address like 0.0.0.0 or your localhost (127.0.0.1) to block access.
  • Network Troubleshooting: Isolate DNS issues by hard-coding IP addresses for specific servers.

Why Edit the Hosts File? 

Editing the hosts file allows you to manually control the IP address associated with a particular hostname. This can be useful for various purposes, such as blocking access to specific websites, redirecting domains to different IP addresses, or testing website changes before they go live.

In website development, hosts files can also be used to test website updates locally before updating the DNS records. This allows developers to debug or preview websites using custom domain names or specific IP addresses.

In website migration, hosts files can be temporarily modified to point the domain to the new server’s IP address. This allows for testing and verification of website functionality on the new host before updating the DNS records.

Hosts files can be used for domain redirection by mapping a domain name to a specific IP address. This allows an admin to redirect traffic from one domain to another without modifying DNS settings or web server configurations.

Where is the Hosts File Located? 

The hosts file can be found in different locations on macOS, Linux, and Windows devices:

  • macOS: /etc/hosts
  • Linux: /etc/hosts
  • Windows: C:\Windows\System32\drivers\etc\hosts

Note: Administrative privileges may be required to access and edit the hosts file on each operating system.

What Does a Hosts File Contain? 

The typical content and format of entries in the hosts file include:

  • IP Address: The IP address associated with the domain or hostname.
  • Hostname: The domain or hostname to which the IP address is mapped.

The entries are usually formatted as ‘IP_address hostname’ – for example ‘127.0.0.1 localhost’.

Each entry is placed on a separate line. Comments can be added using the ‘#’ symbol at the beginning of the line to provide additional context or explanations.

It’s important to maintain proper spacing. You can use either a single Tab or at least one Space between the IP address and the hostname to ensure the format is picked up by the operating system.

How to Edit the Hosts File in Windows 

Editing a hosts file in Windows is a relatively simple process, but it requires the correct formatting rules to be done correctly:

  • Step 1: Open Notepad as Administrator: Click the Start menu, type “Notepad”, right-click on it, and select “Run as administrator”.
  • Step 2: In Notepad, click File > Open. Navigate to C:\Windows\System32\drivers\etc. In the bottom-right dropdown, change from “Text Documents (.txt)” to **”All Files (.*)”**. Select the hosts file and click Open.
  • Step 3: Make Your Edits:  Add your new entries at the bottom of the file, following this format:
    • IP_address yourdomain.com www.yourdomain.com
    • Make sure to replace “IP_address” with the actual IP address of the website, and “yourdomain.com” with the domain you want to point to that IP.
  • Step 4: Simply click File > Save or press Ctrl+S. You can now close Notepad.

Note: In Windows 7 onwards, you have to run Notepad as an administrator. If you’re using Windows XP or earlier, it’s not necessary.

How to Edit the Hosts File in Windows in MacOS and Linux

You’ll use the Terminal and a command-line text editor.

  • Step 1: Open Terminal: (macOS: Finder > Applications > Utilities. Linux: Ctrl+Alt+T).
  • Step 2: Open the Hosts File with sudo: Type the following command and press Enter. This uses nano, a common beginner-friendly editor.
    • Command: sudo nano /etc/hosts
  • Step 3: Authenticate: Enter your administrator password when prompted (no characters will appear; this is normal).
  • Step 4: Make Your Edits: Use the arrow keys to navigate. Add your entries.
  • Step 5: Save and Exit: Press Ctrl+X to exit. It will ask if you want to save. Press Y for yes, then Enter to confirm the filename.

Reversing Changes Made to the Hosts File 

To reverse changes made to the hosts file in Windows, follow these steps:

  • Open File Explorer.
  • Go to C:\Windows\System32\drivers\etc.
  • Copy the original hosts file (if available).
  • Paste and replace the edited hosts file with the original file.
  • Confirm the replacement if prompted.
  • Restart the computer, so the changes can take effect.

Reversing Changes Made to the Hosts File on macOS and Linux

The core principle is the same for both operating systems: you can either perform a full reset to default or a selective rollback of specific entries. Most steps require terminal commands with sudo for administrator privileges.

Key Preliminary Step (Recommended):

  • Before making changes, always create a backup of your current hosts file. This allows you to restore the previous state instantly.
    • On macOS: sudo cp /etc/hosts /etc/hosts.backup
    • On Linux: sudo cp /etc/hosts /etc/hosts.backup

For macOS Users

Option A: Full Reset to Default

  1. Open the Terminal application.
  2. Edit the hosts file with a text editor (using nano here):
    • Run: sudo nano /private/etc/hosts
  3. Replace the entire file content with the standard default:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
  1. Save and Exit the editor:
    • Press Ctrl+O, then Enter to save.
    • Press Ctrl+X to exit.
  2. Flush the DNS Cache to apply changes immediately (no reboot needed):
    • Run: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Option B: Selective Rollback of Specific Entries

  1. Open the Terminal.
  2. Edit the hosts file:
    • Run: sudo nano /private/etc/hosts
  3. Find and Modify the specific lines you want to reverse:
    • Either delete the entire line containing the unwanted entry.
    • Or comment it out by adding a # at the line’s beginning (e.g., # 192.168.1.100 mysite.test).
  4. Save and Exit (Ctrl+O, Enter, Ctrl+X).
  5. Flush the DNS Cache using the same command as in Option A.

For Linux Users

Option A: Full Reset to Default

  1. Open a Terminal window.
  2. Edit the hosts file:
    • Run: sudo nano /etc/hosts
  3. Replace the content with a common default template:
127.0.0.1   localhost
127.0.1.1   your-computer-hostname
::1         localhost ip6-localhost ip6-loopback

Note: Additional IPv6 lines may exist on some distributions and are optional.

  • Pro Tip: Replace your-computer-hostname with your actual hostname. Find it by running the hostname command in another terminal tab.

  1. Save and Exit (Ctrl+O, Enter, Ctrl+X).
  2. Flush DNS Cache (if applicable): The command depends on your system’s service.
    • For systemd-resolved (common on Ubuntu, Fedora): sudo systemd-resolve –flush-caches or sudo resolvectl flush-caches (for newer systems)
    • For nscd (Name Service Cache Daemon): sudo service nscd restart
    • Note: Many Linux setups don’t cache DNS locally; if a flush command fails, it’s often normal.

Option B: Selective Rollback of Specific Entries

  1. Open a Terminal.
  2. Edit the hosts file: sudo nano /etc/hosts
  3. Find and Modify the lines you wish to undo by deleting or commenting them out.
  4. Save and Exit.
  5. Flush DNS Cache using the appropriate command for your system, if needed.

Restoring from a Backup (Universal)

If you created a .backup file (or .old), restore it with this single command:

  • On macOS: sudo mv /etc/hosts.backup /etc/hosts
  • On Linux: sudo mv /etc/hosts.backup /etc/hosts
  • Then, flush the DNS cache using your OS-specific command above.

Special Considerations for Browser Behaviour 

Special considerations need to be made for browsers like Firefox, that use DNS format over HTTPS (DoH) for hosts files.

DoH encrypts DNS queries and sends them over HTTPS, bypassing the traditional DNS resolution process.

In Firefox, changes made to the hosts file may not affect browsing if DoH is enabled. This is because DoH routes DNS queries through trusted DNS resolvers, ignoring the local hosts file.

To make changes effective, the DoH in Firefox will need to be disabled, or configured with a specific DNS resolver that recognizes the hosts file modifications.

To disable DoH in Firefox:

  • Open Firefox and type ‘about : config’ in the address bar.
  • Accept the warning prompt.
  • Search for ‘network.trr.mode’ and set its value to 5.

Flushing the DNS Cache in Windows
Sometimes, a user may be required to flush their DNS cache to resolve related problems, such as incorrect or outdated DNS records.

It’s also essential to flush a DNS cache shortly after updating DNS settings, to ensure a computer retrieves the latest DNS information.

A DNS cache can be flushed by:

  • Opening the command prompt as an administrator.
  • Typing the command ‘ipconfig /flushdns’.
  • Pressing enter.

Security and Permissions 

Administrative permissions are essential to editing the hosts file, as it is a critical system file that controls the mapping of hostnames to IP addresses. Modifying this file can have significant implications for network connectivity and security.

Administrative access helps prevent the risk of unauthorized or accidental changes that could compromise the overall security and functionality of network operations by making sure only authorized users can edit the host file.

An additional layer of security would be a Secure Socket Layer (SSL), which is a cryptographic protocol that establishes an encrypted connection between a client and a server – keeping data shared between them protected from unauthorized access.

An SSL Certificate is an additional form of digital verification over the authenticity of a website or server – allowing an SSL to transmit or receive encrypted communications.

Support and Assistance 

If support is needed for a specific query related to hosts files, solutions can commonly be found in three key areas:

  • Online forums & communities
  • Official operating system documentation
  • Online tutorials or guides

Alternatively, Knownhost offers Shared, VPS, Dedicated, and Cloud Web Hosting services, with 24/7/365 support available from our hosting experts.

Looking to take your development website live? Need support managing server configurations, DNS settings, and more?

Maybe you need support with troubleshooting on live servers, DNS management, or simply enhancing security on your live website. Get expert support today!

GET STARTED NOW

Frequently Asked Questions (FAQs)


Q: Where Is the Hosts File Shortcut?

A: The hosts file shortcut is a system file that plays a critical role in resolving domain names to IP addresses. On Windows operating systems, the hosts file is usually found at ‘C:\Windows\System32\drivers\etc\hosts’, while on macOS and Linux, it is located at ‘/etc/hosts’.

Q: How Do I Open a Hosts File?

A: To open the hosts file, you can use a text editor with administrative privileges. On Windows, navigate to ‘C:\Windows\System32\drivers\etc\hosts’. On macOS and Linux, access ‘/etc/hosts’. Ensure you run the text editor as an administrator/root to make changes, then save the file after editing.

Q: Where Do I Put Hosts Files in Windows?

A: In Windows, hosts files are typically located at ‘C:\Windows\System32\drivers\etc\hosts’. This is the designated folder where you should place and edit the hosts file. Ensure you have administrative privileges to modify the file.