How to Edit Your Hosts File: A Step-by-Step Guide (Windows, macOS & Linux)

This article explains how to safely locate, open, and modify the hosts file across major operating systems to map domains to IPs.

How to Add a Static Entry to Your Hosts File

This guide will walk you through the steps to add a static entry to your hosts file on Windows, macOS, and Linux. The hosts file is a crucial system file that allows you to map domain names to specific IP addresses, overriding the default DNS resolution.

Steps to Add a Static Entry

1. Access the Hosts File:

  • On Windows

    1. Open Notepad as an administrator. To do this, press Windows Key + S, type "Notepad," right-click on "Notepad" in the search results, and choose "Run as administrator."
    2. In Notepad, go to File > Open.
    3. Navigate to C:\Windows\System32\drivers\etc\ and select the hosts file. You might need to change the file type filter from "Text Documents (*.txt)" to "All Files (*.*)" to see it.
  • On macOS

    1. Open Terminal. The quickest way is to press Command + Space, type "Terminal," and hit Enter.
    2. (Optional but recommended) Backup your hosts file: Enter sudo cp /etc/hosts /etc/hosts.backup and press Enter. You'll need to enter your administrator password.
    3. Edit the hosts file: Type sudo nano /etc/hosts and press Enter. Enter your administrator password when prompted.
  • On Linux

    1. Open your preferred terminal application.
    2. (Optional but recommended) Backup your hosts file: Enter sudo cp /etc/hosts /etc/hosts.backup and press Enter. You'll need to enter your administrator password.
    3. Edit the hosts file: Type sudo nano /etc/hosts (or sudo vim /etc/hosts if you prefer Vim) and press Enter. Enter your administrator password when prompted.

2. Add the Static Entry:

Just pop in a new line using this simple format:

<IP Address> <Domain Name>

For example, if you wanted internalserver.local to point to 192.168.1.10,

you'd just type: 192.168.1.10 internalserver.local

3. Save the Changes:

  • Notepad: Press Ctrl + S or go to File > Save.
  • Nano: Press Ctrl + O, then Enter. Press Ctrl + X to exit.

4. Flush the DNS Cache:

  • Windows: Open Command Prompt as administrator and run: ipconfig /flushdns
  • macOS: Run: sudo dscacheutil -flushcache
  • Linux: Run: sudo systemd-resolve --flush-caches (or) sudo service nscd restart