/etc/hosts

The /etc/hosts file is a system file in Unix-like operating systems that maps hostnames to IP addresses. It is used by the system to resolve hostnames to IP addresses before querying DNS servers. This file can be used for local hostname resolution and is particularly useful in network configurations and for troubleshooting.

Format of /etc/hosts

The /etc/hosts file consists of lines of text, each specifying an IP address followed by one or more hostnames. Comments can be added using the # character. Here is the general format:

<IP Address>    <Hostname>    [Alias...]

Example of /etc/hosts

127.0.0.1       localhost
127.0.1.1       myhostname
192.168.1.1     router.local  router
8.8.8.8         google-dns

In this example:

  • 127.0.0.1 localhost maps the IP address 127.0.0.1 to the hostname localhost.

  • 127.0.1.1 myhostname maps the IP address 127.0.1.1 to the hostname myhostname.

  • 192.168.1.1 router.local router maps the IP address 192.168.1.1 to the hostnames router.local and router.

  • 8.8.8.8 google-dns maps the IP address 8.8.8.8 to the hostname google-dns.

Editing /etc/hosts

  1. Open the File: To edit the /etc/hosts file, you need superuser privileges. You can use a text editor such as nano, vim, or gedit. For example:

    sudo nano /etc/hosts
  2. Modify the Entries: Add, remove, or modify entries as needed.

  3. Save and Exit: Save your changes and exit the text editor.

Practical Uses

  1. Local Development: Developers often use the /etc/hosts file to map development domain names to localhost or to specific IP addresses for testing purposes.

    127.0.0.1       myapp.local
  2. Blocking Unwanted Websites: By mapping unwanted websites to 127.0.0.1, users can effectively block access to these sites.

    127.0.0.1       unwantedwebsite.com
  3. Host Resolution in Small Networks: In small networks without a DNS server, the /etc/hosts file can be used to define hostname to IP address mappings for devices.

    192.168.1.100   server.local
    192.168.1.101   printer.local
  4. Overriding DNS: If you need to temporarily override DNS resolution for a specific hostname, you can add an entry to /etc/hosts.

Conclusion

The /etc/hosts file is a simple yet powerful tool for hostname resolution in Unix-like operating systems. It allows for local overrides of DNS queries, facilitating tasks such as development, troubleshooting, and network configuration. Understanding its format and usage can significantly enhance your ability to manage and configure network settings effectively.

Last updated