skip to content
Skesov.com

How to Edit the hosts File in Windows, macOS, and Linux

/ 2 min read

Table of Contents

The hosts file maps domain names to IP addresses locally on your computer. Entries in this file take precedence over external DNS servers. This is useful for testing websites before updating domain DNS records or for blocking access to specific hosts.

Before You Begin

Antivirus software and Windows Defender may block changes to the hosts file. If you encounter an “access denied” error while saving, temporarily disable protection or add the file to your antivirus exclusions.

Editing in Windows (10, 11)

Method 1: Via Search and Notepad

  1. Press the Win key and type “Notepad”.
  2. Right-click the icon and select “Run as administrator”.
  3. In the program menu, go to File → Open.
  4. Navigate to: C:\Windows\System32\drivers\etc.
  5. Change the file filter in the bottom-right corner from “Text Documents” to “All Files (.)”.
  6. Open the hosts file.

Method 2: Via Terminal

  1. Press Win + X and select Terminal (Admin) or PowerShell (Admin).
  2. Run the following command:
    Terminal window
    notepad C:\Windows\System32\drivers\etc\hosts

Editing in macOS and Linux

On UNIX-like systems, the file is located at /etc/hosts. Modifying it requires superuser (sudo) privileges.

  1. Open the “Terminal” application.
  2. Run the command to edit using the nano console editor:
    Terminal window
    sudo nano /etc/hosts
  3. Enter your administrator password.
  4. Use Ctrl+O to save and Ctrl+X to exit the editor.

Entry Format

Add new entries at the end of the file. Each entry should be on a new line.

IP AddressDomain Name
127.0.0.1local.test
0.0.0.0telemetry.service.com

Example:

127.0.0.1 mysite.local

Verifying Changes

To ensure the changes have taken effect, run the ping command in your terminal:

Terminal window
ping mysite.local

The response should come from the IP address specified in the hosts file.

Resetting the hosts File

If you experience network access issues, clear the file and leave only the standard entries:

127.0.0.1 localhost
::1 localhost