hostsファイルの概要をまとめました。
hostsファイルの概要
hostsファイルはIPアドレスと特定の文字列を紐づけているファイルです。
Windowsでは以下にhostsファイルが格納されています。
C:\Windows\System32\drivers\etc\hosts
Linuxでは以下にhostsファイルが格納されています。
/etc/hosts
Windowsのhostsファイル
Windowsでは、以下のファイルにドメイン名とIPアドレスを対応させて記載することができます。
C:\Windows\System32\drivers\etc\hosts

hostsファイルには以下の内容が記載されています。
# 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
Windowsのhostsファイルの編集
管理者実行せずにメモ帳を起動し、hostsファイルを編集しようとすると編集できないことがあります。
その場合、Windowsのhostsファイルは、メモ帳(notepad.exe)を管理者実行し、その上でhostsファイルを開くことで編集可能です。これはhostsファイルがWindowsのシステムフォルダに格納されるためです。
Linuxのhostsファイル
Linuxでは以下のファイルにドメイン名やコンピュータのホスト名とIPアドレスを紐づけて記載できます。
/etc/hosts
以下のコマンドで中身を確認します。
cat /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]#
