How to Edit the hosts File in Windows and linux to Load an Example Domain from a Localhost Proxy

Abstract windows drawing

Editing /etc/hosts in linux

In a previous post, we wrote about configuring putty ssh port forwarding. However, for this to be useful, you do need to edit /etc/hosts (in linux) or the “hosts” file (in windows) to point your target domain to 127.0.0.1. So to help you get that going, this article will show how to edit those files appropriately. This will tell your computer to connect to 127.0.0.1 when connecting to www.example.com.

To start, we need to edit the /etc/hosts file. In linux, this requires root permissions or sudo access, so make sure to login to an account with appropriate permissions.

Once you’re logged in to a command prompt, do the following:

  1. Open the file with the nano text editor: nano /etc/hosts
  2. Add the following line to the end of the file: 127.0.0.1 www.example.com
  3. Press CTRL+X and when prompted, type “Y” to save your changes and exit nano

That’s it! The /etc/hosts is now updated to tell your computer to connect to 127.0.0.1 when trying to load www.example.com. Because this updates the DNS resolution for the domain, you might need to flush your dns cache or close and reopen your browser in order for the changes to take effect.

Editing “hosts” file in windows:

The following process was tested with Windows 10, but will work identically, or nearly so, in many versions of Windows:

  1. Open the Start menu and type “Notepad”. This will show search results which include the Notepad program built into Windows.
  2. Right-click on “Notepad” and then click “Run as administrator” from the popup menu.
  3. Once Notepad is loaded, click “File” and “Open” from the menu.
  4. In the “Open” dialog, type the entire path and filename as follows and then click open: C:\Windows\System32\drivers\etc\hosts
  5. Once the file is loaded, add this line to the end: 127.0.0.1 example.com
  6. click “File” and “Save” from the menu.

As with the linux instructions, your hosts file is now updated to tell your computer to connect to 127.0.0.1 when trying to load www.example.com. Again, because this updates the DNS resolution for the domain, you might need to flush your dns cache or close and reopen your browser in order for the changes to take effect.

Why do this?

We’ve shown how to editing the “hosts” file to point a domain to localhost. This is useful in conjunction with SSH port forwarding for a couple of reasons.

The first benefit is that you can access www.example.com as if it were hosted locally. This can be helpful for accessing services that are only offered on your local network as well as for testing and development needs.

Second, it enables you to safely access www.example.com via an unsafe network using SSH port forwarding, which we covered in our earlier post. Your network traffic can be encrypted to prevent interception and tampering by using the SSH tunnel.

Third, you can visit www.example.com using SSH port forwarding without having to individually configure each program. Any application that can access the localhost will instantly be able to use the tunnel since you can use the SSH tunnel as a transparent proxy by forwarding the domain to the localhost.

Overall, using this method to update the hosts file can offer a quick and safe way to connect to www.example.com using the local SSH tunnel created with PuTTY.

Do you love servers?

We do! If you need help optimizing your server’s performance and security, IOFLOOD can help. Our dedicated servers offer a range of solutions to fit your needs, and our team of experts can help you configure your server to meet your specific requirements.

To learn more about our dedicated servers and how we can help you with your server needs, contact us at sales[at]ioflood.com or visit our website at https://ioflood.com. Our team is always available to answer your questions and help you find the perfect server for your needs.