How to Edit the Local Hosts File on Ubuntu Linux

|

|

This tutorial instructs on how to modify Ubuntu local host files. The host file, a plain-text document, maps IP addresses to hostnames on a local computer, with priority over DNS. Editing is done via commands, with any changes taking effect immediately. To add an entry, users must input the domain and corresponding IP address.

This brief tutorial shows students and new users how to edit or modify Ubuntu local host files.

When you think of a host file, think of it as a plain-text file used by the operating systems to map IP addresses to the hostname on the local computer. The host’s file has priority over DNS.

Editing the local host file on Ubuntu Linux is a useful way to override the default DNS settings. This can be helpful in situations where you need to access a website or server by its domain name, but the DNS server is not resolving it correctly.

You can manually map the domain name to a specific IP address by editing the hosts file. This can also be useful for testing websites or applications on a local network, where you may not have access to a DNS server.

Overall, editing the local hosts file on Ubuntu Linux can be a useful tool for customizing and troubleshooting network settings on your computer.

When you’re ready to edit the Ubuntu local host file, follow the steps below:

Open Ubuntu Hosts File

The process of editing Ubuntu host files is simple. To add an entry or modify the host’s file, simply run the commands below to open the host’s file.

sudo nano /etc/hosts

Similar lines below should appear in the file.

127.0.0.1	localhost
127.0.1.1	ubuntu1804

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
..

Changes you make to the host’s file take effect immediately. If you want to undo changes to the host’s file, simply open the file and remove the lines you added or precede the line with a # to ignore it.

Add Host Records

For example, to add an entry to translate 127.0.0.1 to example.com, open the host’s file and add its entry by scrolling down to the end of the file and add your new entries:

127.0.0.1	localhost
127.0.1.1	ubuntu1804
127.0.0.1       example.com
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback

Save the file and exit.

As we said above, the changes take effect immediately. You can use example.com in your browser when you save the file.

Conclusion:

This is how one adds or modifies Ubuntu hosts file entry.

You may also like the post below:

Like this:



6 responses to “How to Edit the Local Hosts File on Ubuntu Linux”

  1. Chief Avatar
    Chief

    A stupid question: how to save the /etc/hosts file?

    1. A Avatar
      A

      You just save it, just make sure you opened your text editor as root user or it won’t let you save it.

    2. Charlie Avatar
      Charlie

      Run this command in your terminal. It will open the host file in the nano text editor as super user so it can be saved.

      sudo nano /etc/hosts

    3. Mai Dat Avatar
      Mai Dat

      sudo nautilus 🙂

    4. Param Avatar
      Param

      Ctrl+s or Ctrl+o
      then to exit Ctrl+X

  2. Qamar Abbas Avatar
    Qamar Abbas

    great solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.