Ubuntu hosts file update
When a developer tests web-based applications, it’s common practice to spoof your computer to think the corporate domain name links back to the local machine. The easiest way to accomplish this feat is to edit the /etc/hosts file and map the loopback address of the computer to your corporate domain name.
Let’s explore why developers edit hosts and the importance of the Ubuntu hosts file.
Why edit hosts?
When a developer modifies the hosts file it stops 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 back to the local machine, mapping any domain name in the hosts file to this IP address will allow software programs that use your corporate domain name to be testable locally.
On a Windows machine, the etc (pronounced ‘etsy’) hosts file 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 ask for the hosts file to be opened with a registered text editor such as Nano.
Where is the Ubuntu hosts file?
A developer should use the Windows file system explorer to easily locate the hosts file. With Ubuntu hosts edits, it’s easier to simply reference the file through the terminal.
A developer can edit the Ubuntu hosts file by issuing the following command from the terminal:
[email protected]:~$ sudo nano /etc/hosts
This command opens Ubuntu’s 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 choosing. In this example I mapped gitlab.mcnz.com and www.mcnz.com to the localhost address in the Ubuntu hosts file.
Developers can save the Ubuntu hosts file changes with the CTRL+O command. The command to exit Nano is CTRL+X, or a developer can simply click on the close button in the top right-hand corner of the terminal window.
No reboot is needed to test the changes. Simply 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 hosts file edit dangers
Keep in mind that after a developer adds a domain name to the hosts file, that domain name will be forever mapped to the local machine. For example, if your Jenkins or Tomcat server uses the corporate domain name or one of your DevOps tools uses it, and you’ve mapped that domain name to 127.0.0.1, you’ll never be able to access those resources until the Ubuntu hosts file edits are removed.
The corollary to this danger is the fact that you can also stop the browser from accessing any undesirable domain names by mapping them to the loopback address. For example, if a developer mapped the Instagram domain name to the loopback address on a desktop or laptop, it would be impossible for anyone to ever bring Instagram up on your computer.