The /etc/hosts file is a plain text file that maps hostnames to IP addresses in the Ubuntu and Unix-like operating systems. It is used to resolve hostnames to their corresponding IP addresses before querying a DNS server.
When a domain name is requested, the hosts file will be consulted first and if there is an entry for it, the IP address associated with that domain will be used.
If you’ve ever wanted to change what a particular domain points to on your own machine, you can do that by editing your hosts file.
In this post, I’ll take a look at the /etc/hosts file on an Ubuntu system. I’ll discuss how the hosts file works and how you can use it to customize your network settings.
Example of /etc/hosts file
The hosts file is made up of two columns: the IP address and the associated hostname.
An example /etc/hosts file entry looks like the following:
127.0.0.1 localhost
192.168.0.100 web-server
The above entries mean that when a request for “localhost” is made, it will resolve to the IP address 127.0.0.1 and when a request for “web-server” is made, it will resolve to the IP address 192.168.0.100.
You can use the ping command to verify if your hosts file is working properly. For example, you can ping that hostname like so:
ping web-server
If it works, you should see something like this:
PING web-server (192.168.0.100) 56(84) bytes of data.
64 bytes from 192.168.0.100: icmp_seq=1 ttl=64 time=0.053 ms
From the command output, we can see that the hostname “web-server” was resolved to the IP address 192.168.0.100, which is what you specified in your hosts file.
Using /etc/hosts file in Ubuntu Linux
You can use your hosts file to manually assign an IP address to a domain or override existing DNS records.
For example, if you wanted to point www.example.com to 192.168.1.10, you could add the following line to your hosts file:
192.168.1.10 www.example.com
Once you’ve added this line, any requests to www.example.com will be directed to 192.168.1.10 instead of querying a DNS server for the IP address.
Editing /etc/hosts File in Ubuntu Linux
You may need to edit your /etc/hosts file for various reasons such as blocking certain websites, redirecting domains to different IP addresses for testing purposes or just creating shortcuts.
The /etc/hosts file is a plaintext file, which means that you can modify it with any text editor such as Nano or Vim. To do this, open a terminal and issue the following command:
sudo nano /etc/hosts
This will open the hosts file in Nano.
For example, we need to add
192.168.1.10 www.example.com
Make the necessary changes and then save it by pressing Ctrl+X, then Y and finally Enter.
Note: It’s important to note that the hosts file is case-sensitive and does not support wildcards, so you’ll need to specify exact domain names for each entry.
Now when you visit www.example.com, your browser will use the IP address that you specified in the hosts file instead of looking it up with DNS.
This can be very useful if you need to override an incorrect or outdated DNS record or if you want to prevent your computer from accessing certain domains.
That’s it!
Conclusion
The /etc/hosts file is a powerful tool for overriding DNS records and customizing your network settings. By understanding how the hosts file works and how to edit it, you can easily customize your network settings without having to rely on a DNS server.
We hope this post has been helpful in understanding the /etc/hosts file in Ubuntu. If you have any questions or comments, please leave them below. Thanks for reading!