Submind YouTube summaries
Thumbnail for /etc/hosts for Local Domains in Linux

/etc/hosts for Local Domains in Linux

Watch on YouTube

Video summary

The video demonstrates how to configure local domain names for testing purposes on a Linux system using the `/etc/hosts` file within a VirtualBox environment. The setup involves three machines: a client, a router, and a server running an Apache web server with the default Ubuntu website. Initially, the presenter accesses this server from the client machine using its IP address, 192.168.2.22, via a text-based browser called Lynx. This process reveals the standard Apache page that appears when no custom domain is configured, establishing a baseline for understanding how local network resources are accessed directly by IP without relying on external DNS services. To simplify access and facilitate testing, the presenter explains the function of the `/etc/hosts` file, which maps specific domain names to IP addresses locally on the machine. By adding an entry that associates a custom domain like `example.com` with the server's IP address, the client can navigate to this domain name instead of typing the raw numbers. The system resolves these requests by first checking the local hosts file; if a matching entry is found, it bypasses external DNS lookups entirely and directs the browser to the specified local IP. If no match exists in the hosts file, the system proceeds to query configured real DNS servers, such as those listed in the `/etc/resolv.conf` file, to resolve public domain names like `google.com`. The tutorial further illustrates how editing the hosts file can redirect traffic for testing or development scenarios, such as pointing a popular site like `google.com` to a local server. When this entry is added, accessing the domain name immediately loads the local Apache page rather than the actual Google website, effectively simulating a different environment on the client machine. It is crucial to note that these changes are strictly local; modifying the hosts file on one machine does not affect others on the network, meaning each Linux machine maintains its own independent mapping table. This isolation ensures that while one user can test their local setup by overriding public domains, other machines on the same network will continue to resolve those domains correctly through the standard DNS infrastructure. Finally, the video concludes by emphasizing the practical utility of the hosts file for developers and testers who need to verify local services or simulate redirections without altering global DNS records. The presenter demonstrates reversing the test by removing the custom entry from the hosts file, which restores normal behavior where accessing `google.com` successfully loads the real Google website after consulting the external DNS servers. This workflow highlights a simple yet powerful method for managing local network resources and testing web applications in isolation before deploying them to a production environment with proper domain registration and DNS configuration.
Read the full video transcript
we have three Linux machines inside VirtualBox the client the router and server and they set up in a small Internet and the server is set up to be running the Apache web server so has it or a website on it the default Apache website we're going to use the client as a web browser and explore how we can modify the hosts file to give some faith domain names just for testing purposes so I'll go to the client and in terms of web browsing on the command line I'm going to use links as my web browser which is a text-based web browser and access the server which has IP address 192 168 2.22 so this should take my client to the website of the server in this case and more just press ENTER and we see the top this is the Apache to Ubuntu a default page so this is provided by default when a patch is installed on the server so this is the website and I'm gonna acute quit yes I'll quit now we don't have a domain name for our web server and we're not going to set one up this stage because he now internal network we're not going to go to the complexities of having a DNS server or registering a real domain name but sometimes for testing purposes I would like to give it a domain name so instead of having to type 1 9 2 1 6 8 2 dot 22 I'd like to type in a domain name and in the hosts file to show that the host file inside a Linux machine it contains local mappings of names including domain names to IP addresses and in my host file I have a mapping which are put in there before the demonstrates that if I want to go to if I type in the domain name WWE example calm it will take me to the address 192 168 2.22 which is that of the server let's just try that so the idea is if I now type in by my browser links I want to go to example.com then it takes me to that same Apache to a bun to a default page it takes me to the server web site to you to click that because what happens is that I supply this domain name and what my DNS client does is it looks in this host file to see if there is such a domain name and it finds on this third line there is a domain name that matches that and therefore it says contact the IP address 192 168 2.22 if there wasn't a value in there then for example is not in the host file then it uses the real DNS system so if it's not in the host file and that's just some cookies that lynx is asking me about then it does a normal DNS lookup goes out to a real DNS server and finds the IP address for wwo comm from from the real DNS server and eventually redirects me to the real Google web page compute equipment so by default what happens the DNS or the DNS lookup first consults that etc' host file if there's no match in there then it consults the real DNS server how do I know which rule DNS server there's a resolve Kampf file which keeps track of the DNS servers or the name servers listed here so my real DNS servers I actually have three configured 138 77 176 top ten and these other two noting that this file is generated by some other software in the background we normally would not modify that that's the resolver software so again when I supply a domain name to a client such as a web browser the host file is consulted if there's no answer there then the DNS server one of the DNS servers is consulted and we get the the real-life address that allows us to put any domain names in the host file so once again if I access Google I get eventually accept the cookies and I get the Google Website and our cue to quit now if I edit the host file and we need administrator privileges I'll open it up and by so we sue over I and if we insert a new line saying read this backwards the domain-name wwo comm maps to the IP address 192 168 2.22 that is my local server then when I try to visit google.com be redirected to my local server we'll try that escape and say now I'll do the same access the Google website and it takes me directly to my local server showing the Apache to Ubuntu a default page this is because the first check is done inside hosts so now my client thinks that wwo comm is actually my local server and it's a way for me to use that domain for testing purposes and do some simple redirection noting that the hosts file is local to my machine right so whatever I put in here is only relevant to my client if I then go to let's say my router another Linux machine I have which has a different host file if I try to access a gripe in Google then it will go to the real Google website so the host file is only for making changes local to the machine so it's no good for setting up domain names across a network it's just internally to that machine and just the fear I'll go back and edit my hosts file and delete that line and save and now when I access Google comm since there's no entry in the hosts file it should take me to the real Google web site because my real DNS server is consulted to get the answer so use your hosts file to test different domains when you have a local server and maybe to do redirection if you don't want to go to the real web site