hosts file
hosts 文件是历史遗留问题。在ARPANET 时期,是没有DNS的,整个互联网上只有数百台主机,那时就是通过hosts 文件来解析域名的。后来Internet 爆发增长,已经不可能再将所有的主机名与ip的对应关系写进hosts 文件里面了,那样速度太慢。后来就出现了DNS(Domain Name System)。同时原来的hosts 文件也保留了下来 。
The hosts file has priority over DNS. When you type in the domain name of a web site you want to visit, the domain name must be translated into its corresponding IP Address. The operating system first checks its hosts file for the corresponding domain, and if there is no entry(ip) for the domain, it will query the configured DNS servers to resolve the specified domain name.
How to use the hosts file
The location of the hosts file, depending on the operating system that you are using, is:
Windows – SystemRoot > system32 > drivers > etc > hosts
By default, the system root is C:Windows, so if you are using Windows, your hosts file is most probably: C:\Windows\System32\drivers\etc\hosts
Linux – /etc/hosts
Mac OS X – /private/etc/hosts
Let’s say that you wish to resolve yourdomain.com to the IP address 1.2.3.4. In this case, you would need to open up the hosts file with a text editor and append the following line:
1.2.3.4 yourdomain.com www.yourdomain.com
This will “tell” your computer to resolve yourdomain.com to 1.2.3.4. Once you do that you may need to clear your web browser’s cache, afterward, if you try to reach your domain http://yourdomain.com in a browser it should take you to the site hosted on the server with IP 1.2.3.4.
Example
- ping google 没有通
- 修改hosts文件
- ping google
到此对hosts也大致了解,只要在hosts文件中找到域名对应的entry(ip),就不会去dns解析了。
ping
ping 命令是基于 ICMP 协议来工作的,ping 命令会发送一份ICMP回显请求报文给目标主机,并等待目标主机返回ICMP回显应答。因为ICMP协议会要求目标主机在收到消息之后,必须返回ICMP应答消息给源主机,如果源主机在一定时间内收到了目标主机的应答,则表明两台主机之间网络是可达的。
ICMP协议既不是基于TCP,也不是基于UDP,而是直接基于网络层的IP协议,在整个网络协议栈中属于相当底层的协议了。
ping属于哪一层?ping命令使用的tcp报文还是udp报文呢?
首先,ping是应用层直接使用网络层ICMP的一个例子,它没有通过运输层的TCP或UDP。
ping命令是我们能够直接使用的命令,就像HTTP、FTP,属于应用层。ping命令底层使用的是ICMP,ICMP报文封装在ip包里,所以ICMP属于网络层协议。