hosts 文件的作用

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属于网络层协议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值