Skip to main content

How to Bind IP Locally with a Domain Using the Hosts File

When you purchase a domain or update DNS records, it may take some time for the changes to propagate globally. However, you can bind the domain to your server’s IP address locally using the hosts file on your computer. This allows you to access your site even before DNS propagation completes.

What is the Hosts File?

The hosts file maps domain names to IP addresses. Modifying this file allows you to override DNS settings locally and direct your domain to the IP address of your choice.

Hosts File Format

The format of the hosts file is simple: each entry contains an IP address followed by one or more domain names, separated by spaces or tabs. Comments can be added by starting a line with #.

# Example of a hosts file entry
# IP Address    Domain Name(s)
192.168.1.100   example.com www.example.com
127.0.0.1       localhost
  

In the example above:

  • 192.168.1.100 is mapped to both example.com and www.example.com.
  • 127.0.0.1 is the local loopback address, mapped to localhost.

How to Use the Hosts File on Different Platforms

Windows

  1. Open Notepad as Administrator:
    • Search for Notepad in the Start Menu.
    • Right-click Notepad and select Run as Administrator.
  2. Open the Hosts File:

    Navigate to the file path:

    C:\Windows\System32\drivers\etc\hosts

    In Notepad, go to File > Open, and set the file type to "All Files" to see the hosts file.

  3. Add Your Entry:

    At the bottom of the file, add:

    192.168.1.100 example.com www.example.com
  4. Save the File: Press Ctrl + S or go to File > Save.
  5. Flush DNS Cache (Optional):

    Open Command Prompt as Administrator and run:

    ipconfig /flushdns

Mac

  1. Open Terminal: Navigate to Applications > Utilities > Terminal.
  2. Edit the Hosts File:
    sudo nano /etc/hosts
  3. Add Your Entry:

    Add the following at the bottom:

    192.168.1.100 example.com www.example.com
  4. Save the Changes:

    Press Ctrl + O to save, Enter to confirm, and Ctrl + X to exit.

  5. Flush DNS Cache (Optional):
    sudo dscacheutil -flushcache

Linux

  1. Open Terminal.
  2. Edit the Hosts File:
    sudo nano /etc/hosts
  3. Add Your Entry:

    Add this line at the bottom:

    192.168.1.100 example.com www.example.com
  4. Save the File:

    Press Ctrl + O, Enter to confirm, and Ctrl + X to exit nano.

  5. Flush DNS Cache (Optional):
    sudo systemctl restart nscd

    If nscd is not installed, use:

    sudo systemd-resolve --flush-caches

Verify the Change

To confirm the domain is resolving to the IP you set, run the following commands:

  • Windows:
    nslookup example.com
  • Mac/Linux:
    dig example.com

Troubleshooting Tips

  • Ensure you run the editor with Administrator/root privileges.
  • Double-check for typos in the IP address or domain name.
  • If changes don't take effect, try restarting your browser or computer.

By following this guide, you can locally bind your domain to an IP address to test configurations or access your website before DNS propagation completes. If you have further questions, contact the Middlehost support team.