Watch this video for a step by step process on how to edit hosts file in Windows and Linux operating system
The hosts file is used to map domain hostnames to IP addresses. Using the hosts file you can point any domain name OR hostname to the desired IP on your local machine.
The hosts file is a massive help when an individual/webmaster/developer wants to test website design or code on domains that are not registered OR do not point to the desired server IP. Don’t need to wait for DNS to propagate when pointed to a different web server.
Steps To Add New Entries To The Windows Host File
Follow these steps to add new entries in the host file for the Windows operating system:
- Click on the Start button and type notepad.
- Right-click on Notepad and select the option “Run as administrator” to open Notepad.
- Now, go to File and click on Open.
- Click on This PC.
- Select Local Disk C drive
- Go to the Windows folder.
- Click on the System32 folder.
- Choose the drivers folder.
- Now, select the etc folder.
- As you’ll see, no files are showing in this folder.
- To change the view from text document to all files, simply navigate to this drop-down and select the all files option.
- Select the hosts file and click on Open.
- In the same file, enter the IP address and domain name as per your choice.
- You can use the external IP address provided by your new hosting provider if you wish to test how your website is loading on that IP address before updating its DNS of your website.
- Here, we have entered the localhost IP address, i.e., 127.0.0.1, and www.milesweb.com as a domain name.
- Navigate to File and click on Save to save the file.
- Now close this window.
- To test this hosts file, again go to the Search Box, type cmd, and hit enter.
- The command prompt window appears on the screen.
- Type the command ping space website name, i.e., www.milesweb.com in our case, and press enter.
- Now you can see milesweb resolving to the localhost IP address rather than MilesWeb’s external IP address.
Steps To Add New Entries To The Linux Host File
Follow these steps to add new entries in the host file for the Linux operating system:
- To open the terminal on ubuntu press the shortcut keys Ctrl + ALT + T simultaneously.
- In the terminal window, type the below command and press enter.
sudo nano /etc/hosts
- Type the password of your PC.
- The file will directly get opened.
- Enter the IP address and domain name as per your choice.
- Here, we have entered the localhost IP address, i.e., 127.0.0.1, and www.milesweb.com as a domain name.
- Press CTRL + X simultaneously to save this file.
- It will ask for the confirmation; in that, press y and hit enter.
- Now, type this command: ping space website name and press enter.
As you can see, milesweb is resolving to the localhost IP address rather than MilesWeb’s external IP address.
Steps To Add New Entries To The Mac Host File
Follow these steps to add new entries in the host file for Mac users (Apple macOS):
- Open the Terminal
To open the Terminal, press Command (⌘) (Command key symbol) + Space and use Search for Finder.
- Open the Hosts File
Now, input the command:
sudo nano /etc/hosts
When prompted, provide your Mac password.
- Edit the Hosts File
Look for the arrow keys to navigate to the bottom of the file. Add the following entry:
123.123.123.123 example.com
(Make sure to replace 123.123.123.123 with the actual server IP and example.com with your domain.)
- Save and Exit
In the step before this, you pressed Control + X.
Now, you have to press Y and then press Enter to save.
- Flush DNS Cache
Run the command below to apply changes:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
There are no explicit messages; the changes will be effective immediately.
That’s how you can edit the hosts file in the Windows, Linux, and macOS operating systems.