localhost与127.0.0.1 本机IP

谈谈localhost与127.0.0.1

 

localhost意为本地主机,指这台计算机,是给回路网络接口的标准主机名,对应的IP地址为127.0.0.1,可访问本地服务器的web项目(http://localhost)。

那么它们有什么区别呢?

  localhost不通过网卡传输,不受防火墙和网卡限制;而127.0.0.1则依赖于网卡,会受到防火墙和网卡的限制。

  localhost访问时带着本机当前用户的权限;而用IP访问时,是通过网络再去访问主机,涉及到网络用户权限。

  因为用localhost访问时不会解析成IP,也就不会占用网络资源,因此一般情况下用localhost更方便快捷。

无法访问本地的localhost怎么办呢?

  1. 打开http://127.0.0.1/ 看是否能访问IIS(互联网信息服务)的默认网页,能访问则说明IIS已经成功开启,否则检查IIS是否在本机安装且开启;
  2. 开始--命令(cmd)--ping localhost,看返回的数据是否为127.0.0.1,如果是则说明可以解析localhost,否则打开C:WINDOWSsystem32driversetchost文件看里面有没有127.0.0.1 localhost,添加好后再去访问localhost;
  3. 关闭电脑上的防火墙试试,或检查电脑上是否有恶意插件;
  4. 实在不行将IIS卸载掉,重新安装一次。

localhost指向的IP是127.0.0.1这个关系可以修改吗?

  答案是肯定的,可以在Hosts文件中设置,以下是在两种操作系统中的设置方法。

  • Windows中设置:
  两种方法找到localhost设置:
    1、运行 drivers 打开etc文件下hosts 就看到了127.0.0.1 localhost;
    2、打开C:WINDOWSsystem32driversetchost,用记事本打开打开如下:
      # 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 (这里不让显示) # source server
      # 38.25.63.10 (这里不让显示) # x client host
      127.0.0.1localhost
      最下面的:127.0.0.1 localhost,这两个参数不可以随意改。格式是IP空格域名
  • 在Linux中设置:
  在Linux系统中,系统管理员在为用户建立新帐号时赋给用户一个用户名和一个初始的口令。
  另外,Linux系统给计算机赋予一个主机名。
  主机名用于在网络上识别独立的计算机(即使用户的计算机没有联网,也应该有一个主机名)。TurboLinux系统给出的缺省主机名为:localhost。
  在Linux系统成功启动后,屏幕会提示:localhost login ,意思就是系统要求输入用户名,接着要求输入口令。否则将无法登录系统,也就无法使用
原文地址:https://www.cnblogs.com/guojing-anda/p/11027146.html