In this article we will try to teach you how to edit Linux hosts file. Occasionally you may need to edit the hosts file on your computer. Your hosts file may have been maliciously modified in an attack or prank. Alternatively, you may only want to control access to specific websites or network traffic. Computers have had hosts files since the ARPANET days. The hosts files were used to resolve hostnames for decades before the DNS system was implemented. Because they were used to support network name resolution, hosts files grew into huge documents. Microsoft, Apple, and other OS developers have kept the hosts file, so it varies very little between Windows, macOS, and Linux. The syntax remains largely the same on all platforms. Most hosts files have multiple entries for loopback.
In the /etc/hosts file, your system stores an internal map of IP addresses -> hostnames. This does not require a DNS server as it is stored locally on your computer. This is a great way to manage things like internal servers or other devices on your network that normally require you to enter an IP address. Below we have mention the steps to edit host file in Lonix.
How to Edit Linux Hosts File
Open a Terminal Window (Command Line)
Most Linux distributions have an applications > utilities > terminal feature, or an option to right-click the desktop and click Open Terminal.
Open the Linux Hosts File
To open the Linux hosts file, type the command:
sudo vim /etc/hosts
The system should prompt for your password – enter it, and the Hosts file should open.
Modify the File
The hosts file in Linux is formatted so that the IP address is first, and the server name is second.
0.0.0.0 server.domain.com
Add any entries you wish to the end of the file. If you make a mistake or need to tell your operating system to ignore a line, add the ‘#’ sign at the beginning of that line.
Name Service Switch
The hosts file bypasses the standard Domain Name Server lookup. In Linux, there’s another file that tells the operating system what order to look for the IP address translations. The file is nsswitch.conf, and if it’s configured to look at DNS first, then it’ll skip your hosts file and go straight to DNS lookup.
To check the configuration, in your terminal window type:
cat /etc/nsswitch.conf
About halfway down, there should be an entry labeled “hosts”. Make sure that the right-hand column lists files first. If for some reason DNS is listed first, open the file in your text editor:
sudo vim /etc/nsswitch.conf
It should open the nsswitch.conf file. For the hosts: setting, change the entry so that files is at the beginning of the entry, and dns is at the end.
Final Words
We hope our article on how to Edit Linux Hosts File will help you and resolve all your problems. Linux is an open source operating system (OS). An operating system is the software that directly manages a system’s hardware and resources such as CPU, memory, and storage. The operating system sits between applications and hardware, making the connections between all your software and the physical resources that do the work.
I hope you understand this article, How to Edit Linux Hosts File.