Hosts is a text file and crucial system file used to map hostnames (like domain names) to IP addresses. By editing this file, you can manually control where your computer directs certain website requests; this can be useful for testing websites before they go live or for blocking access to certain sites. This knowledge-based article will guide you in editing the host file on your Mac.
Why edit the Mac host file?
Editing the host file allows the following:
- Test Websites Locally: Preview changes to websites before updating DNS records.
- Block Websites Access: Access to specific websites can blocked by redirecting a domain to a different IP address.
- Override DNS Settings: For troubleshooting purposes, force your Mac to use specific IP addresses for certain domains.
Step-by-Step Guide:
- Open Terminal:
- To edit the Mac host file, you need to launch the Terminal application. Find it under Applications > Utilities.
- Go to the Utilities folder, find the Terminal icon in the Applications, and select it.
- You can also use Spotlight (Cmd + Space) and type “Terminal” to search for it, then run the Terminal application.
- Open the host File:
- Once the Terminal window opens, you can edit the host file. First, ensure that you are in your Mac’s top-level folder by typing the following command:cd /
- The host file’s location on Mac computers is:/private/etc/hosts
- We used the Nano text editor. To modify the host file, administrator access is required.
- Type the following command and press Enter:sudo nano /etc/hosts
- To confirm the command press, Enter, and then you will be prompted to enter your administrator password.
- Edit the host file.
- The host file typically contains comments explaining its purpose.
- To add a new mapping, place your cursor where you want to make changes and enter the IP address followed by the hostname you want to map.
IP_address yourdomain.comFor example 192.168.0.12 example.comNote: You must replace IP_address with the website’s IP, and yourdomain.com with the actual domain name.Use the Tab key to separate the IP address and hostname. As required, you can add as many mappings, each on a new line.
- Save Changes:
- To save your changes to nano, press Ctrl + O and enter to confirm the filename written.
- Press Ctrl + X to exit Nano.
- Flush the DNS cache:
- After editing the host file to ensure that the changes take effect immediately, the DNS cache may need to be flushed. In Terminal, type:
sudo killall -HUP mDNSResponder - This command restarts the DNS resolver that is responsible for domain name resolution on your Mac.
- After editing the host file to ensure that the changes take effect immediately, the DNS cache may need to be flushed. In Terminal, type:
Editing the host file on your Mac gives you manual control over DNS mappings, which is important for web developers, system administrators, or anyone who needs precise control over how their computer resolves domain names. By following these steps, you can confidently edit your host file and effectively manage network configurations. For further assistance or if you encounter any issues while editing your host file, please contact our support team.
Read also about What is DNS_PROBE_FINISHED_NXDOMAIN, and How to Resolve it.