localhost與127.0.0.1
在沒有網(wǎng)絡(luò)的情況下,在命令行ping localhost與ping 127.0.0.1也是可以ping通的(前提需有網(wǎng)卡),但兩者有到底有區(qū)別嗎?有聯(lián)系嗎?
我現(xiàn)在的理解是localhost是127.0.0.1的一個別名,兩者是通過C:\WINDOWS\system32\drivers\etc\hosts(在此假定系統(tǒng)盤為C盤)這個文件來對應(yīng)起來的。用記事本打開這個文件,內(nèi)容如下:
127.0.0.1 happy
保存之后,再打開命令行,ping happy,你會發(fā)現(xiàn),現(xiàn)在happy也可以ping 通了,由此可知localhost只是大家約定的代表127.0.0.1的一個別名,如果你喜歡,你可以通過上面這種方式為127.0.0.1配置不同的別名。
我現(xiàn)在的理解是localhost是127.0.0.1的一個別名,兩者是通過C:\WINDOWS\system32\drivers\etc\hosts(在此假定系統(tǒng)盤為C盤)這個文件來對應(yīng)起來的。用記事本打開這個文件,內(nèi)容如下:
# Copyright (c) 1993-1999 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
127.0.0.1 localhost
在這個文件的最后一行寫明了127.0.0.1與localhost的對應(yīng)關(guān)系,我們不防來做個實驗,在這個文件最后加下面一行:#
# 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
127.0.0.1 localhost
127.0.0.1 happy
保存之后,再打開命令行,ping happy,你會發(fā)現(xiàn),現(xiàn)在happy也可以ping 通了,由此可知localhost只是大家約定的代表127.0.0.1的一個別名,如果你喜歡,你可以通過上面這種方式為127.0.0.1配置不同的別名。
posted on 2008-10-03 22:45 文濤 閱讀(1761) 評論(0) 編輯 收藏 所屬分類: NetWork