How to edit my 'hosts' file on Windows, Mac, and Linux?


The "hosts" file is a simple text file used to map hostnames to IP addresses on your computer. It can be edited to control how your computer resolves domain names, effectively overriding DNS settings. Here's how to edit the "hosts" file on Windows, Mac, and Linux:

On Windows:

  1. Open Notepad as an administrator:
    • Right-click on the Start button and select "Windows Terminal (Admin)" or "Command Prompt (Admin)" if you're using an older version of Windows.
    • If you're using Windows 10 or later, you can also search for "Notepad" in the Start menu, right-click it, and choose "Run as administrator."
  2. Open the hosts file:
    • In the command prompt or terminal, type notepad C:\Windows\System32\drivers\etc\hosts and press Enter. This will open the "hosts" file in Notepad.
  3. Edit the hosts file:
    • You can add or modify entries in the hosts file. For example, to map the IP address 127.0.0.1 to a domain name "example.com," add the following line:
    • 127.0.0.1 example.com
  4. Save the hosts file:
    • After making your changes, save the file and close Notepad.
  5. Flush DNS cache (optional):
    • To apply changes immediately, you can flush the DNS cache by running ipconfig /flushdns in the command prompt.

On macOS (Mac OS X):

  1. Open Terminal:
    • You can find Terminal in the Utilities folder within the Applications folder or search for it using Spotlight.
  2. Open the hosts file:
    • In the terminal, type sudo nano /etc/hosts and press Enter. You'll be prompted to enter your password.
  3. Edit the hosts file:
    • Add or modify entries as needed. Use the same format as in the Windows example.
  4. Save the hosts file:
    • After editing, press Ctrl + O, then press Enter to save the changes. Press Ctrl + X to exit nano.
  5. Flush DNS cache (optional):
    • You can flush the DNS cache using the command sudo dscacheutil -flushcache.

On Linux (Ubuntu, Debian, CentOS, etc.):

  1. Open Terminal:
    • You can usually find Terminal or a similar command-line tool in your applications menu, or by pressing Ctrl+Alt+t.
  2. Open the hosts file:
    • In the terminal, use a text editor (e.g., nano or vim) to open the hosts file. For example:
      sudo nano /etc/hosts
  3. Edit the hosts file:
    • Make the necessary changes using the same format as in the Windows example.
  4. Save the hosts file:
    • After editing, press Ctrl + O, then press Enter to save the changes. Press Ctrl + X to exit the text editor.
  5. Flush DNS cache (optional):
    • The procedure for flushing the DNS cache can vary depending on your Linux distribution. You may use commands like systemctl, service, or systemd to restart the networking service. Check the documentation for your specific Linux distribution for the appropriate command.

Note: Remember to be cautious when editing the hosts file, as incorrect entries can affect network connectivity. Always back up the original file and make sure you have the necessary permissions to edit the file, especially on Mac and Linux.


Was this answer helpful?

One email a month. Endless business benefits.

Don't miss out on WMTWWFY — the newsletter that keeps your website fast, safe, and visible.

« Back
Spinner
aluminium-anthropoid Security Check