There may be occasions that you need to update and edit the host files on your computer. Reasons that come up may range from simply controlling access to network traffic and websites all the way to fixing an attack on your system. Whatever the reason may be, having these simple tips will speed up the process. However, there is more. Designers and developers can make use of the local hosts file to save a lot of time in the development process.
How to use the hosts file as a web designer or web developer
When developing a website, editing your hosts file is the perfect way to test websites. Basically what you do is tell your browser to act as if you are using the website address you typed in, but requesting the traffic from another IP address. You basically tell only your browser to get the traffic from a different IP address than the IP address that is connected to the domain when typed in, in the browser.
When to use a hosts file
At Hoasted we use the hosts file in multiple occasions. In the following situations, using a hosts file change can be very convenient:
1. When creating a hosting account on a new server, but the domain is not yet transferred
When a domain is moved to Hoasted and in pending transfer status, we often have to wait for the domain to be properly indexed/cached by the ISP (internet service provider). This process can take up to 48 hours. By changing the hosts file we can act as if it was already fully transferred. While your ISP thinks that it still needs to get the data from the old server, you told your browser to already get the data from the new server.
2. When copying a VPS/server for staging purposes
Every now and then, as a developer you need to clone a server or VPS account to develop new code or additional code for a customer. In these cases you can copy the server and put the domain name and ip address of the staging server in the hosts file. In that waye we let our browser get the data from the staging server. For the outside world, nothing changes. Only your browser will get the domain connected to the different server, because the hosts file overrides it. Now you can test your application (like WordPress for example), without making changes in URL’s or settings. The moment you are done testing, you can migrate the data without worrying about potential bugs that can occur due to url changes.
How To Edit Your Hosts File: Using Hosts File to Test Website
Hosts files have been in use for quite a long time. They were used to resolve hosts names before DNS and since ARPANET. These files, which were huge, were used to solve the network name resolution problems. Since Microsoft decided to keep the hosts files around in Windows networking, the syntax of hosts files stays the almost the same across all platforms (Windows, OS X or Linux).
Hosts files tend to have several loopback entries, so we can use those for a syntax example. The location to redirect to will be what we see first, second includes the address to redirect and finally is the comment. While you can separate the entries with spaces, typically you will find them in one or two tabs instead.
Different operating systems
Windows 8 or 8.1
The most challenging part of working with Windows 8 is setting up apps to be run as administrator. Notepad is the key. Search for the program, right-click and then choose run as administrator. After that is completed find the following file and open it up by choosing File – Open Feature.
In the Run Line use the following command:
This will open the file in notepad. Once the program is opened you can just edit the file and save the progress.
In these operating systems hosts files can directly be edited in the terminal. Any editor can be used whether it is GUI text or any other that you feel comfortable with. Though sometimes found in the root drive, Ubuntu hosts files are also in the /etc/folder. To edit open at the root. In Ubuntu you will see a section for IP6, though normally this can be ignored in most cases. As with Windows, make sure to save the changes.
Any version of Mac OS X
Run this code in Terminal:
sudo nano /private/etc/hosts
This process follows the same track as Ubuntu. Start with whichever editor you prefer and in the terminal. Do this even if you wish to use GUI.
Things we noted when testing these steps: Chrome does not use hosts files in any OS, but adding the web address needing to be redirected (or blocked) and adding and extra line afterwards corrected the problem.
The information above should help you redirect (or block) websites the way you want. We always enjoy any suggestions or comments on any of the information covered in our articles. Leave us a comment!