Linux Host File: How to Edit and Use it?



To understand what a Linux host file is, we will need to undertake this tutorial through a visual aid approach. So fire up your Ubuntu terminal but first ensure you are a root user or have root user privileges.
ubuntu terminal
Having the root user privileges allows you to prefix the commands you type on the Linux terminal with ‘sudo.’ For example, let’s assume you find yourself in front of your friend’s or college’s Linux terminal. You might want to check for updates or access a specific file system configuration. A simple command of checking for updates as a non-root user or sudoer user is depicted below.
check-update as the non-sudoer user
You would get a ‘permission denied’ error as displayed if the Linux OS has sudoer privileges. This authentication aspect of the Linux operating system is important for the prevention of unwarranted security breaches. Since this tutorial article focuses on understanding the Linux host file, such authentication levels are important.
The Linux operating system ensures that access to such file systems is privileged or authenticated. They serve an important role in the functional and design hierarchy of the Linux operating system. To test this theory, let us now update our Ubuntu Linux system using the ‘sudo’ command as a prefix.
update Linux system as sudoer user
As you can see, we were prompted for an authentication password, and we were able to run an update on our system successfully. It is because the user had sudoer privileges. To understand more about sudoer users, check out our already prepared tutorial module on the same.
What is a Linux Host File?
The term ‘Host File’ defines itself. It is applicable in all operating systems, including Windows and macOS. Consider a scenario of a website administrator. Part of the responsibilities of such an individual is managing different websites through their domain name. The web administrator must know the domain names of the websites he or she is managing. It makes it easier to query the website through its name and troubleshoot a potential problem.
To further understand this concept, consider the access you have right now to this Fosslinux tutorials website. You, as an end-user, only see this site as a simple interface. However, the web administrator managing this site knows that it exists in two portions. They are namely the IP address and the domain them. These two aspects of the Linux host file are interlinked, and one cannot exist without the other.
Therefore, even the Linux operating system you are using now has a Linux host file module. You can, therefore, comfortably host a site locally. To gaze at the Linux host file structure on your operating system, type the following command on your terminal.
Linux host file structure
The first two lines are important. We will, however, concentrate on the first line. In simple terms, the first line directly depicts the local IP address ‘127.0.0.1’ links to the domain name ‘localhost.’ The IP address comes before the domain name because it supersedes it. In simple terms, an IP address must exist before a domain name. Once an IP address is assigned, it cannot be changed. However, a domain name can be edited and still point to the same IP address.
Consider the scenario of accessing a website through your browser. Your browser’s first action will be to look for the IP address linked with the domain name. If the IP address exists, you will be able to access the website. However, if the IP address is not active, you will get a 404 error, which implies the site cannot be reached.
Editing and Using the Linux Host File
Since a picture’s worth remains quantified by a thousand words, we need a real-world approach to memorize the Linux host file’s importance completely. Since the file holds the local IP ‘127.0.0.1’ and the hostname ‘localhost’ to your Linux machine, think of yourself as an offline web admin. Our demo on editing and using the Linux host file will require a web server installed on your Linux machine. For our case, we are going to use the popular and award-winning web server Apache. Lucky for us, there is an in-depth tutorial on setting up Apache on your Linux OS. So go ahead and set it up before we resume the tutorial.
Editing your Linux Host File Access
Now that you have your Apache web server up and running, it, by default, assigns itself the local IP address of your machine. Therefore, it implies that if you open a new tab on your browser and punch in the address ‘127.0.0.1’, you should view a page like the one below.
Apache webserver default home page through 127.0.0.1
Apache told you that it could find the IP address on your machine and display its default ‘Welcome to the Web’ page. So Apache is telling us that we can locally host a website on our machine in an offline mode. If you are an aspiring web designer and developer, then this is some exciting news. However, our access to the default Apache page was through the localhost IP address ‘127.0.0.1’. We can now attempt the same access using the ‘localhost’ hostname as viewed in the Linux host file we accessed earlier.
On the same browser, remove the IP address ‘127.0.0.1’ and replace it with ‘localhost.’ The default Apache webpage view should remain the same. The only difference should be the URL view on our browser, which now reads ‘localhost’ instead of ‘127.0.0.1’.
Apache webserver access through hostname localhost
The Dynamic Aspect of the Linux Host File
As mentioned, this IP address limits our access to only one machine, which we are using. However, if you engage yourself with some networking knowledge, you should configure a TP-Link Router. It will enable you to configure a dynamic IP address different from 127.0.0.1. The advantage of a dynamic IP address is that it enables your machine to be accessible by other machines. It, therefore, implies that merging the knowledge you have on Linux host file, web servers, and TP-Link Routers brings you closer to being an undisputed web admin.
Since we are dealing with one machine alone, we will embrace the basic knowledge of the Linux host file it has to offer. We agreed that editing the hostname is warranted since we can only deal with a single IP address per machine. So go ahead and edit the name ‘localhost’ on the Linux host file you previously opened. You can change it to something different, like ‘webadmin.com.’ Now, try to access it through the web browser and see if you will still land on the default Apache home page.
Editing hostname on Linux host file
Accessing edited hostname on Linux host file
As predicted, the access to our default Apache webpage through the edited ‘webadmin.com’ hostname was a success.To be confident with access to the default Apache webpage through the edited hostname ‘webadmin.com,’ we need to perform some verification. Open another terminal and perform the following ping request.
Pinging edited Linux host file domain name
A ping request is a way of reaching out to a hostname or domain name to determine if it is active. A successful pin request will respond as depicted above. We pinged the domain name ‘webadmin.com,’ and it returned a response on our terminal associated with the IP address assigned to the hostname. So this is evidence that editing our Linux host file was a success.
Importance and Applications of the Linux Host File
Since we now have a solid foundation for working with the Linux Host File, we need to know how far this knowledge can take us. We have already touched on some of its importance, but we need to bundle all these advantages.
If you are a web developer or aspiring to be one, you might be skeptical about the development server. However, by using a production server like Apache and Nginx, you can turn your machine into a production server. You will be able to see how your web app will behave in a production environment. It, therefore, offers you a stable testing ground for your web applications. Moreover, you won’t have to be restarting your development server each time you want to work on your web app. A production server is always in a continuously running or active state upon its installation and configuration. You will need to be changing your code and then going to the browser and input the hostname or IP address on your Linux host file.
Another advantage of a Linux host file is its capability to let you host and share your files with other remote machines. You could create a file-sharing system that enables you to warrant access to certain file formats on your end.
Conclusion
The knowledge you gained from this article piece is essential in building your confidence in understanding how domain names and IP addresses connect in the real world. Moreover, you will connect more with information and configurations related to advanced concepts like Domain Name Servers. From here, it will be easy to understand the walkthroughs of their configurations and how to set them up.
A big cheer to the path this Linux host file knowledge will take you.



Source link

Leave a Comment