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 bothexample.com
andwww.example.com
.127.0.0.1
is the local loopback address, mapped tolocalhost
.
How to Use the Hosts File on Different Platforms
Windows
- Open Notepad as Administrator:
- Search for Notepad in the Start Menu.
- Right-click Notepad and select Run as Administrator.
- 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. - Add Your Entry:
At the bottom of the file, add:
192.168.1.100 example.com www.example.com
- Save the File: Press
Ctrl + S
or go to File > Save. - Flush DNS Cache (Optional):
Open Command Prompt as Administrator and run:
ipconfig /flushdns
Mac
Linux
- Open Terminal.
- Edit the Hosts File:
sudo nano /etc/hosts
- Add Your Entry:
Add this line at the bottom:
192.168.1.100 example.com www.example.com
- Save the File:
Press
Ctrl + O
,Enter
to confirm, andCtrl + X
to exit nano. - 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.