黑马程序员技术交流社区

标题: 关于localhost的问题 [打印本页]

作者: Gaara33    时间: 2014-6-12 12:34
标题: 关于localhost的问题
本帖最后由 Gaara33 于 2014-6-12 13:14 编辑

如图,我在本地的host记事本里面把www.sina.com.cn指向了本地ip地址,
可是为什么在ie浏览器里面输入这个地址后,还是访问新浪呢?毕老师的视频
不是说可以屏蔽网址吗,还是我的理解有偏差?

localhost.jpg (186.67 KB, 下载次数: 6)

localhost.jpg

作者: 黎志勇    时间: 2014-6-12 12:40
你前面加了#号啊,注释掉,当然不起作用。
作者: Gaara33    时间: 2014-6-12 12:42
黎志勇 发表于 2014-6-12 12:40
你前面加了#号啊,注释掉,当然不起作用。

那倒数第三行呢,不是也有#号吗?localhost还能对应127这个ip
作者: 黎志勇    时间: 2014-6-12 12:45
# 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


看到加粗的部分没有,可以用#添加注释。


作者: Gaara33    时间: 2014-6-12 12:48
黎志勇 发表于 2014-6-12 12:45
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP f ...

那我把#号去掉又试了一下,仍然不行呀
作者: 黎志勇    时间: 2014-6-12 12:52

我把自己电脑的host清空了,再自己添加了下,表示没问题啊。你ping一下试试。


作者: Gaara33    时间: 2014-6-12 13:08
黎志勇 发表于 2014-6-12 12:52
我把自己电脑的host清空了,再自己添加了下,表示没问题啊。你ping一下试试。

...

我把#号去掉,ping了一下,确实和你的一样,然后输入网址,的确正常了,但是不清楚为什么刚才就不行

另外,为什么localhost对应127ip就不去掉#号呢,不理解。

ip.jpg (155.51 KB, 下载次数: 7)

ip.jpg

sina.jpg (73.27 KB, 下载次数: 11)

sina.jpg

作者: 黎志勇    时间: 2014-6-12 13:14
Gaara33 发表于 2014-6-12 13:08
我把#号去掉,ping了一下,确实和你的一样,然后输入网址,的确正常了,但是不清楚为什么刚才就不行

另 ...

127.0.0.1是特殊的,系统固定好localhost就对应这个IP,就算把host文件清空,localhost也对应127.0.0.1。
估计host文件里面
  1. #        127.0.0.1       localhost
  2. #        ::1             localhost
复制代码

这两行是为了让人知道,系统已经把localhost固定为那ip了,毕竟是注释。
作者: Gaara33    时间: 2014-6-12 13:15
黎志勇 发表于 2014-6-12 13:14
127.0.0.1是特殊的,系统固定好localhost就对应这个IP,就算把host文件清空,localhost也对应127.0.0.1。 ...

嗯,很有道理




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2