当前位置: 首页>>技术教程>>正文


/etc/hosts有什么用?

, ,

问题描述

我是Ubuntu的新手,我想知道:/etc/hosts的确切用途是什么?

让我们说我正在添加它:

127.0.x.x mydomain

那是什么意思?

最佳解决思路

Purpose

The hosts file is one of several system facilities that assists in addressing network nodes in a computer network. It is a common part of an operating system’s Internet Protocol (IP) implementation, and serves the function of translating human-friendly hostnames into numeric protocol addresses, called IP addresses, that identify and locate a host in an IP network. In some operating systems, the hosts file’s content is used preferentially to other methods, such as the Domain Name System (DNS), but many systems implement name service switches (e.g., nsswitch.conf for Linux and Unix) to provide customization. Unlike the DNS, the hosts file is under the direct control of the local computer’s administrator

File content

The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names. Each field is separated by white space (blanks or tabulation characters). Comment lines may be included; they are indicated by a hash character (#) in the first position of such lines. Entirely blank lines in the file are ignored. For example, a typical hosts file may contain the following:

# This is an example of the hosts file  127.0.0.1  localhost loopback ::1        localhost 

This example only contains entries for the loopback addresses of the system and their host names, a typical default content of the hosts file. The example illustrates that an IP address may have multiple host names, and that a host name may be mapped to several IP addresses.

Wikipedia


这是无效的(?):

127.0.x.x mydomain

通配符是*所以它应该是127.0.*.*但我几乎看不到任何其他东西,而不是127.0.0.1😉


这意味着您可以使用mydomain作为该IP地址的名称。使用www.google.com比记住谷歌用于其搜索引擎的IP地址(74.125.132.106)更容易。同样适用于您的计算机上的本地(localhost)。如果您运行自己的Apache实例,则可以将本地IP地址设置为名称,并在浏览器中使用该名称。


示例中的::1是IP地址的IP v6版本。

次佳解决思路

这意味着系统不会对mydomain进行DNS查找,它将自动重定向到您在hosts文件中指定的IP地址。

在大多数系统上,hosts文件中的默认条目是:

127.0.0.1    localhost

127.0.0.1始终是您所在计算机的地址。例如,如果您在PC上运行Web服务器,则可以通过http://localhost:port从Web浏览器访问它,而不是键入整个IP地址http://127.0.0.1:port

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2921.html,未经允许,请勿转载。