The hosts file on your computer allows you to bypass the DNS. This means, by editing the hosts file on your computer, you can actually map a domain name to any IP address in the Internet. So, in this article, we will discuss how to edit the hosts file to override the DNS for a website.
Why and How to Edit The Hosts File?
This is useful when you want to test your site without a temporary URL ( like
http://IP/~username ), prior to DNS changes, before going live with SSL or for any other DNS-related reasons.
You need to add the following entries to your systems hosts file in order to resolve the website to a particular IP Address:
- The IP Address
- The website name
So, if you want to point the websites domain.com and www.domain.com to your new hosting server IP Address 18.104.22.168, edit the hosts file of your local system with the following entries.
22.214.171.124 domain.com www.domain.com
Let’s have look at accessing the hosts files in the different operating systems:
- Click Start.
- Enter Notepad in the Start menu search text box.
- Right-click Notepad and select Run as Administrator.
- From the Notepad, open the following file:
C:\Windows\System32\Drivers\etc\hostsIt will looks like the screenshot below:
- Now, add the above entry, i.e, the destination server’s IP address and the domain name details at the end of the file.(Do not include the # at the beginning of the entry)
- Save the changes
MacOS and Linux
- Access the terminal/console on your system
- Switch to the root user/sudo user
- Open the /etc/hosts file with a text editor.
It will looks like the screenshot below:
- Now, add the destination server’s IP address and domain name at the end of the file. (Do not include the # at the beginning of the entry)
- Save the changes
Thats all!!. You have done the mapping between domain name and IP locally on your system. You can access the domain on your favorite web browser and it will resolve from the mentioned IP address in the hosts file.