Are you looking to take control of your domain name resolution? You’ve come to the right place! In this guide, we’ll walk you through the simple steps of adding static entries to the hosts file on Linux.
Mastering the manipulation of the hosts file is a valuable skill. This seemingly small file holds the power to redirect network traffic, block unwanted sites, or even create shortcuts to your favorite servers. But before we go any further, we need to introduce some theory.
What Is the Linux Hosts File?
The hosts file is a plain text file that almost every operating system, including all flavors of Linux, uses to map hostnames (like example.com) to IP addresses. This file serves as a local override for DNS lookups.
On Linux systems, the hosts file is typically located at “/etc/hosts“. This path makes it easy for system administrators and users to find and edit it when necessary. Accessing it requires root privileges, ensuring only authorized users can make changes.
How Does the Hosts File Work?
When you enter a URL into your browser, try connecting to an SSH remote server using a hostname, etc., your system uses the Domain Name System (DNS) to translate the web address or hostname into an IP address that computers can understand.
However, if an entry exists in the hosts file for that address, the system uses the specified IP address from the hosts file instead of performing a DNS lookup. This process speeds up address resolution and allows for custom domain mappings.
In simpler terms, when you try to visit a website, your computer will check the hosts file first to see if there’s a corresponding IP address there before asking the wider internet.
Top Use Cases
Adding static entries to your host’s file on Linux can be super handy for a few reasons: all about controlling how your computer finds and connects to websites or networked devices:
- Testing and Development: Developers working on websites that are not yet live can map the domain name of their project to the IP address of a local or staging server. This allows you to see how the website will look on the real internet without it actually being there.
- Override DNS information: In cases where DNS information is incorrect or has not propagated yet, adding static entries to your host’s file can temporarily override DNS settings to allow you to access the correct server.
- Block Unwanted Sites: If you want to keep certain websites off-limits, redirect their domain names to a โdead endโ like 0.0.0.0. This way, when you try to visit them, your browser just canโt find the way.
- Speed Up Access: Your computer can find the website faster because it doesn’t have to ask for the address; it’s already written down in your local “/etc/hosts” file.
Add Static Entries to the Hosts File on Linux
To add entries to the hosts file, you need to edit it. This process requires administrative (sudo) privileges on Linux, as the hosts file is a system file protected from regular user changes.
Open the “/etc/hosts” file using a terminal text editor you’re comfortable with. Once the file is open, you’ll see existing entries. These typically include references to localhost.
sudo nano /etc/hosts
To add a new entry, navigate to the end of the file and enter the IP address, followed by at least one space or tab, and then the hostname you wish to map to that IP. For example, to map the “example.com” domain to the IP address “10.0.1.69,” the final version should look like this:
After adding your entries, save the changes and exit the editor. Now, it’s time to test your changes. You can ping the hostname you added to ensure your new mapping works. For example:
ping -c 3 example.com
Code language: CSS (css)
If everything is set up correctly, the ping command should resolve the hostname to the IP address you specified in the “/etc/hosts” file, proving that your system is now using the local hosts file entry for this hostname.
Conclusion
Adding entries to the hosts file in Linux is straightforward and invaluable for testing purposes. Following the steps outlined above, you can add static entries to the hosts file, allowing for precise control over hostname resolution on your system.
Thanks for your time! For more in-depth information, consult hosts file manual page.