How to write the hosts file

ヘッダ画像

How to write the hosts file

The domain is linked to the IP address by a mechanism called DNS (Domain Name Server). DNS mutually translates domain (host name) and IP address.
If the hosts file is in the local environment, this setting will take precedence because it will be referenced before querying DNS.
Although it is simple as a mechanism, it seems that there are many cases that do not recognize well unexpectedly, so we summarized the procedures and precautions for changing hosts.

What is the hosts file?

When accessing the URL yahoo.co.jp on the browser etc., I inquire the DNS server and acquire an IP address like 118.151.235.191 and communicate with it.
If the hosts file exists and there is description in the corresponding domain, it is possible to perform name resolution as specified.
In other words, you can let any server access to the request yahoo.co.jp.
The role and meaning of hosts are the same for Windows PC and Mac.

What kind of use is there?

If you are in the position as a client you can think of uses such as site and ad blocking, but one example that you can effectively use will be the site of website creation.
By rewriting the hosts file, it is possible to virtually allocate domains and IP addresses, actually check the web site before publication on the Internet, or refer to a specific server different from the original IP address due to relocation work etc. can do.

As hosts takes precedence over DNS, it can be guided to a specific spam site by tampering with a virus such as malware if you exploit it.

Rewrite format

The format is basically similar and simple in Windows and UNIX.
Add it in the form of "IP address + halfwidth space + domain name" on the last line.

118.151.235.191 example.com

Windows hosts change

hosts file location

Common in Vista, Windows 7, Windows 8, and Windows 10 in the active Windows OS, it is in the following location.

C:\Windows\System32\drivers\etc\hosts

You can also display it even if you copy and paste the following in "Search for programs and files" or Explorer's address bar.

%WinDir%\System32\Drivers\Etc

Windows 10 hosts file

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
118.151.235.191 example.com

In the above example, we associate yahoo's IP address with example.com domain.
When changing it, opening the corresponding file as it is with a text editor etc. can not save it due to privilege problem.
Please execute the application with administrator privileges or copy it to the desktop etc and then edit it.
Because copyrights may require authorization, please do it on a timely basis.

* If the saved file has an extension such as. Txt, it will not be recognized as a hosts file, so please delete it.

If the changed hosts File does not work

If you can not access the server you want in the specified domain, let's first try pinging the domain you set up.

ping example.com

Try pinging to the set domain

If hosts were successfully recognized, if you ping example.com, a response will be returned from 118.151.235.191.

Although the hosts file was originally applied in the browser as well, if the ping is returning normally, try super reloading.

If the ping does not return normally, you may try refreshing the DNS at the command prompt, but probably will not solve it.

ipconfig /flushdns

In conclusion, Many reasons hosts do not recognize on Windows are character codes.
If it is UFT-8 (line feed code: CR + LF), it will work in most cases if you check the character code with a text editor.
This will almost certainly work.

Confirm text code

Confirm text code

Change host on Macintosh

Host file location

Although famous Hoster seems to be commonly used on Mac, it will eventually override to the original, so here we will try to edit directly with a regular text editor.
The hosts file is located in /private/etc/.
Private is an anomalous folder, but you can open a folder by moving it to /etc/ in the "move" -> "move to folder" desktop pull down menu.

/private/etc/hosts

/private/etc/hosts

Machintosh's hosts file

#
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
118.151.235.191 example.com

* The last line, like the Windows example, is linked to a domain called example.com.

Editing with a text editor

The default application to open with double click is genuine text edit.
However, it is impossible to overwrite and save it as it is because of authority problem.
For this reason, copy it to a desktop or the like before editing it or save it as a different name.

As in Windows, rewriting is added as "IP address + halfwidth space + domain name" on the last line.

* When moving the hosts file to the etc folder, you will be prompted to enter the account password.

Editing at Terminal

It can also be rewritten with the "vim command" of the terminal.
Because root privilege is required, execute with sudo.

sudo vim /etc/hosts
Execute vim editor

Execute vim editor

Enter the password and insert with a key.

Add and save on last line

Add and save on last line

Exit editing mode with esc and save it with ": wq".

Supplement: Are you not going through a proxy?

In any OS using a proxy, it is not a client but a proxy server to actually access.
In such a case, you need to change the hosts file of the proxy server, not the local hosts file.
Please be careful because it is easy to misunderstand.

Leave a Reply

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