When a developer is testing web applications, it is common to spoof your computer into thinking the company’s domain name is linked to the local machine. The easiest way to accomplish this feat is to edit the / etc / hosts file and map the computer’s loopback address to your company’s domain name.
Let’s see why developers are changing hosts and the importance of Ubuntu hosts file.
Why change the hosts?
When a developer modifies the hosts file, it prevents your local computer from looking up domain names through a Domain name system. If a domain name is found in the hosts file, a computer will use the associated IP address specified there. Since the loopback IP address of 127.0.0.1 points to the local machine, mapping any domain name in the hosts file to that IP address will allow software programs that use your corporate domain name to be locally testable.
On a Windows machine, the hosts etc file (pronounced “etsy”) can be easily opened, edited and saved with any standard text editor. However, elevated user rights are required to edit the Ubuntu hosts file. As such, the easiest way to initiate an Ubuntu hosts file edit is to issue a “sudo” command and request that the hosts file be opened with a registered text editor such as Nano.
The hosts file edit
A developer should use Windows File System Explorer to easily locate the hosts file. With changes from Ubuntu hosts, it’s easier to just reference the file through the terminal.
A developer can modify the Ubuntu hosts file by running the following command from the terminal:
[email protected]:~$ sudo nano /etc/hosts
This command opens the Ubuntu hosts file in the system’s default text editor. Add a new line that maps 127.0.0.1 to the domain name of your choice. In this example I have mapped gitlab.mcnz.com and www.mcnz.com at localhost in the Ubuntu hosts file.
Developers can save changes to the Ubuntu hosts file with the CTRL + O command. The command to quit Nano is CTRL + X, or a developer can simply click the close button in the upper right corner of the terminal window.
No restart is necessary to test the changes. Just ping the domain name you added to the Ubuntu hosts file and the repeated responses from the server will be visible.
[email protected]:~$ ping gitlab.mcnz.com
Ubuntu harbors the dangers of file editing
Keep in mind that once a developer adds a domain name to the hosts file, that domain name will be mapped to the local machine forever. For example, if your Jenkins or Tomcat server uses the company domain name or if one of your DevOps tools uses it, and you have mapped that domain name to 127.0.0.1, you will never be able to access to these resources as long as the Ubuntu hosts do not have the file changes are deleted.
The corollary of this danger is the fact that you can also prevent the browser from accessing unwanted domain names by mapping them to the loopback address. For example, if a developer maps the Instagram domain name to the loopback address on a desktop or laptop computer, it would be impossible for anyone to open Instagram on your computer.