What is a HOSTS file and How do you modify the HOSTS file in the different operating systems?
The HOSTS file is found inside an operating system, that helps to resolve hostname with an IP address before checking in DNS.
In your system, you want to access www.yourdomain.com hostname your system first actually resolves the hostname with the IP address of the server.
To resolve the IP address it will first check inside the HOSTS file which is a simple text file that store entries of hostname with the IP address, and if not found then it goes to your Internet Service Provider(ISP) or DNS server
It is helpful in case of troubleshooting or publishing a website for testing purposes by giving entry in the HOSTS file which resolves hostname with IP address locally before checking in DNS.
To modify your HOSTS file in Linux
In Linux, the HOSTS file is found inside the /etc directory in the form of a simple text file that can be modified by using text editors like:
Open file using the nano editor:
# nano /etc/hosts
Open file using the vi editor:
# vi /etc/hosts
Note: Use sudo before the following command if you don’t have root privileges:
After the file is open, give entries of IP address and hostname in the desired manner:
And after that safe file with:
1) Press ctrl + o to save file and ctrl + x to exit from a file if you are using a nano editor.
2) Press Esc button and write :wq if you are using vi editor.
To modify your HOSTS file in Windows 10
In windows operating system the hosts file is found inside the c:\\windows\System32\drivers\etc folder to configure it follow the steps:
1) Press Win + r button and search for notepad and should be open with run as administrator
the other entry will not get saved .
2) Click on File and select the open option, after that goes inside the above-specified folder.
3) Select All files and now select the hosts file.
4) After file is open give entry in the last block i.e. localhost name resolution:
# IP_Address Hostname
# 18.104.22.168 www.yourdomain.com
5) Now save the file with the ctrl + s button.
To modify your HOSTS file in Mac os
In Mac os HOSTS file is found under the following path i.e
/private/etc/hosts, you can easily edit it with nano editor by follows the steps:
- On your system, select Application > Utilities > Terminal to open a terminal window.
- Now open the HOSTS file by using the following command:
# sudo vi /private/etc/hosts
Note: use sudo if you don’t have root privileges.
- Now edit the hosts file.
Add your new mapping after the default mapping i.e. (22.214.171.124 – localhost).
- To save and exits the HOSTS file, press :wq.
- Now ping with the same domain name to check the connection is established or not.
CONCLUSION- The above configuration shows how you can troubleshoot and publish websites for testing purposes by modifying the HOSTS file.