Everything You Need to Know About the Linux Hosts File
Sometimes, you want to block websites from your Linux machine. For example, your IT department might want to block certain sites at work or you may want to take the time to add in personal sites as well. In either case, there are plenty of ways to accomplish this task, and one of the most effective methods is by editing the hosts file. If you’re not already familiar with it, here’s everything you need to know about the Linux hosts file and how to use it.
What Is The Linux Hosts File?
The Linux hosts file is a config file that allows system administrators and programmers to map custom hostnames onto a specific IP address. The file includes a list of IP addresses, one per line, that are considered valid for name resolution by each node on the network. These can be IPv4 or IPv6 addresses depending on the type of network being configured. For example, an IPv4-only node has 127.0.0.1 as its only localhost entry in the file whereas an IPv6-enabled node would have ::1 in addition to 127.0.0.1 in this field. How Do I Use It?: The hosts file typically resides at /etc/hosts.
Working With The Hosts File
The hosts file is a text file that lists all of the IP addresses that your computer recognizes. To access it, open up Terminal and type in sudo nano /etc/hosts. This will take you directly to the file, where you can edit it by pressing Ctrl + O then navigating with arrow keys and hitting Enter. Type 127.0.0.1 localhost into the blank space at the bottom of the document (after any comments). Press Ctrl + X to exit and save changes when prompted. Now go back to the folder in Finder and rename the hosts file from hosts to localhost. In terminal, type rm -i hostname. Next time you open Terminal, it should automatically have a new hosts file called localhost as an editing option so you can use this instead of opening Terminal just for this purpose every time.
It’s important to note that editing your hosts file can result in a variety of different results and is not always intended for correcting local site issues. Editing this file can also open up new attack vectors against your system, so be sure you know what you’re doing before diving in! The best way to learn about your host file is to experiment with it on a test system or VM.
Let us know if you have any questions in the comments below!
Feel free to share your thoughts and tips for using a hosts file in the comments below. Also, be sure to check out part two of this guide, covering everything you need to know about editing your hosts file on Windows 10, macOS and Chrome OS. It’s all right here at hostinger!