As it becomes increasingly easy to deploy servers in a cloud, we often find ourselves wanting to test changes to servers on a running instance. Prudence advices against experimenting on a production server, but there are times when it is necessary to test on a fully functioning instance.
For instance, if one wishes to switch to a new web server hosting multiple (existing) domains, it is easy enough to start a new instance of the server under AWS, however, the resulting server is only accessible via an IP address. Typically, accessing a site on the server via a browser will be ineffective as apache VirtualHost blocks (ServerName directives) will not function without the Host header bring passed by the browser.
While it may be possible to navigate to an IP address, but independently set the Host header, this approach is typically tedious. One way to test the new server would be to change the DNS entries, however, that would negate the point of a ‘testing’ server. On the other hand, remapping DNS for just your computer would allow you to navigate to the site in question on the testing server, via a browser, using the normal domain name, while everyone else goes to the production server.
To implement the above, all that is necessary is a single entry in one’s hosts file, which exists under both Windows (
C:\Windows\System32\drivers\etc\hosts) and Linux (
/etc/hosts), albeit with slight differences.
Domain names entered in this file are mapped to corresponding IP address provided. This file is a plain text file, with a single IP/domain entry per line. The IP address is listed first followed by the domain name (and separated with whitespace).
126.96.36.199 example1.com 188.8.131.52 example2.com
By mapping the IP of your newly launched instance to your domain name, it is possible to achieve what is outlined above, and once the changes to the hosts file are reverted, DNS resolution goes back to using the proper nameservers.
In order to edit the hosts file:
Under Windows, open a text editor as an administrator (Run as Administrator), and from the program, open the hosts file.
Under Linux, root privileges are needed to edit the hosts file.