How to edit hosts file

We will demonstrate how to edit the hosts file on Linux, Windows, and Mac. Also, let us practical experiment with the hosts file.

Do we still have hosts?

Yes, most operating systems have a hosts file.

If we are using Linux, we can find the file in the path “/etc/hosts,” and if we are using Windows, we can find the file in the path “c:\windows\system32\drivers\etc\hosts “

Practice with hosts.

Let’s edit our hosts file to demonstrate how we can map a name to an IP. First, we will map the name “dns” to an IP “127.0.0.24”.

Of course, we could map another IP and another name, but we will use this example due to the simplicity of the configuration.

We’ll show you some ways to do this, depending on your operating system.

Editing hosts on Linux or Mac

Let’s edit the /etc/hosts file with some text editor. We will edit inside a Linux machine using the nano edit in this example.

For this, we will use the command below with administrator privileges.

sudo nano /etc/hosts
Linux hosts file

Next let’s edit the file by inserting a loopback IP = “127.0.0.24” and the name “dns”.

Note. We could create a name for any other IP you would like to access for example (8.8.8.8 dnsgoogle).

127.0.0.24 dns
Editing linux hosts file

Now let’s save the change. If you are using the nano editor, as we are using, you can press Ctrl + x . Then confirm with Y for Yes and press ENTER.

Now let us test using the ping command and the name we gave to our IP. In this example, we will use the command below.

ping dns
Testing your hosts file using ping

Editing hosts on Windows

Let’s look for Notepad and right-click on “Run as Administrator”.

Open notepad using administrator

Now let’s click on File and open. Then let’s select the path below.

C:\Windows\System32\drivers\etc

It’s worth checking that the option to display all files is selected. Otherwise, notepad only shows files of type text.

Locate the hosts file

Then let’s select the hosts file.

Select hosts file on windows

Next let’s edit the file by inserting a loopback IP = “127.0.0.24” and the name “dns”.

127.0.0.24 dns

Note. We could create a name for any other IP we would like to access, for example (8.8.8.8 dnsgoogle).

Editing hosts file on windows

After that we will save the changes.

Next, let’s test it by opening the terminal with cmd in the run menu.

Open windows terminal

Now let’s type in the terminal the ping command and the name we gave to our IP. In this example, we will use the command below.

ping dns
Testing hosts file on windows

We can see in the figure above that the name “dns” is linked to the IP that we previously configured “127.0.0.24”.

See more at :

Install OpenVPN Client on Windows

Install Open VPN on Linux

Install Zabbix on Ubuntu

How to install Zabbix on Raspberry Pi

What is NAT ?

Comment on our channel if you liked it: Click here

Juliana Mascarenhas

Data Scientist and Master in Computer Modeling by LNCC.
Computer Engineer