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:
- Open the file with the nano text editor:
- Add the following line to the end of the file:
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:
- Open the Start menu and type “Notepad”. This will show search results which include the Notepad program built into Windows.
- Right-click on “Notepad” and then click “Run as administrator” from the popup menu.
- Once Notepad is loaded, click “File” and “Open” from the menu.
- In the “Open” dialog, type the entire path and filename as follows and then click open:
- Once the file is loaded, add this line to the end:
- 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.