Windows如何自定义域名hosts解析
文章最后更新时间:2021年06月07日已超过389天没有更新。
Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联数据库,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。
需要注意,Hosts文件配置的映射是静态的,如果网络上的计算机更改了请及时更新IP地址,否则将不能访问。
hosts的用途
一般常用于测试未上线的网站和本地测试,可以自定义域名和IP地址
hosts解释
hosts文件完整路径:C:\Windows\System32\drivers\etc\hosts
Windows版本原始host文件内容如下,如果你怀疑hosts文件被病毒木马修改,可复制以上内容修复hosts 文件。
PS:#后都是注释,所以清空hosts文件对系统正常运行并没有什么影响。
# 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
修改方法
本例以Windows 2008系统进行演示。操作步骤如下
右键单击右下角 开始 按钮,在搜索框输入 Windows Powershell >>选择打开如下图中3
打开Powershell后,输入notepad 。此时,notepad是以administrator用户身份打开的,可以编辑并保存大部分的系统文件。
打开notepad(记事本)之后,按照以下步骤打开hosts文件
在弹出的打开窗口进入到C:\Windows\System32\drivers\etc\目录下,选择所有文件 >> 选择hosts文件 >> 点击打开
打开后,显示内容如下
新增内容格式为:IP 域名。自定义完成并保存后
打开浏览器,访问刚刚添加的自定义测试域名,可以成功通过自定义测试域名访问到搭建在本地的网站。