如何在Windows,Mac或Linux上编辑主机文件

On occasion you will need to edit the hosts file on your machine. Sometimes because of an attack or prank, and others so that you can simply and freely control access to websites and network traffic.

有时,您需要在计算机上编辑主机文件。 有时是由于攻击或恶作剧等,因此您可以简单自由地控制对网站和网络流量的访问。

hosts files have been in use since ARPANET. They were used to resolve hosts names before DNS. hosts files would be massive documents used to aide the network name resolution.

自ARPANET起,hosts文件已被使用。 它们用于在DNS之前解析主机名。 主机文件将是海量文件,用于辅助网络名称解析。

Microsoft kept the hosts file alive in Windows networking which is why it varies very little whether used in Windows, macOS, or Linux. The syntax stays mostly the same across all platforms. Most hosts files will have several entries for loopback. We can use that for the basic example for the typical syntax.

Microsoft在Windows网络中使主机文件保持活动状态,因此无论是在Windows,macOS还是Linux中使用,主机文件的变化都很小。 在所有平台上,语法基本保持不变。 大多数主机文件将具有多个回送条目。 我们可以将其用作典型语法的基本示例。

The first part will be the location to redirect the address to, the second part will be the address that you will want to redirect, and the third part is the comment. They can be separated by a space, but for ease of reading are typically separated by one or two tabs.

第一部分是将地址重定向到的位置,第二部分是您要重定向的地址,第三部分是注释。 它们可以用空格隔开,但是为了便于阅读,通常用一个或两个标签隔开。

127.0.0.1 localhosts #loopback

127.0.0.1 localhosts #loopback

Now let’s look at accessing the hosts files in the different operating systems…

现在,让我们看一下在不同操作系统中访问主机文件的过程…

Windows 8或8.1或10 (Windows 8 or 8.1 or 10)

Unfortunately Windows 8 or 10 makes it annoying to open apps as administrator — but it’s not too difficult. Just search for Notepad, then right-click on Notepad in the search results list, and choose to run it as administrator. If you’re using Windows 10 this will be on the Start Menu.

不幸的是,Windows 8或10使以管理员身份打开应用程序变得很烦人-但这并不困难。 只需搜索记事本,然后在搜索结果列表中右键单击记事本,然后选择以管理员身份运行即可。 如果您使用的是Windows 10,它将位于“开始”菜单上。

If you’re using Windows 10, it’ll look more like this:

如果您使用的是Windows 10,它将看起来像这样:

Windows_10

Once you’ve done so, open up the following file using the File -> Open feature.

完成后,使用文件->打开功能打开以下文件。

c:\windows\system32\drivers\etc\hosts

c:\ windows \ system32 \ drivers \ etc \ hosts

Windows_10

Then you can edit as normal.

然后您可以正常编辑。

Windows 7的 (Windows 7)

To access the hosts file in Windows 7 you can use the following command in the Run Line to open notepad and the file.

要在Windows 7中访问hosts文件,可以在运行行中使用以下命令打开记事本和文件。

notepad c:\windows\system32\drivers\etc\hosts

记事本c:\ windows \ system32 \ drivers \ etc \ hosts

sshot-2010-08-31-[19-41-19]

Once notepad is open you can edit the file. In this example we will block Facebook. To do this just enter in the following after the # mark.

打开记事本后,您可以编辑文件。 在此示例中,我们将阻止Facebook。 为此,只需在#标记后输入以下内容。

0.0.0.0    www.facebook.com

0.0.0.0 www.facebook.com

sshot-2010-08-31-[20-51-49]

Now that you have edited your Hosts file make sure to save it.

现在,您已经编辑了Hosts文件,请确保将其保存。

sshot-2010-08-31-[20-54-07]

Now notice if we try to access Facebook in IE we can’t get to the page.

现在请注意,如果我们尝试在IE中访问Facebook,则无法访问该页面。

sshot-2010-08-31-[20-56-44]

We also were not able to get to it in Google Chrome… (check notes at the end). Also for more info on editing your Hosts file, check out The Geek’s article on how to create a shortcut to quickly edit your Hosts file.

我们也无法在Google Chrome浏览器中使用它(请查看最后的注释)。 另外,有关编辑您的Hosts文件的更多信息,请参阅Geek关于如何创建快捷方式以快速编辑Hosts文件的文章

sshot-2010-08-31-[21-04-27]

的Ubuntu (Ubuntu)

In Ubuntu 10.04 and most Linux distro’s you can edit the hosts file directly in the terminal. You can use your favorite editor or even open your favorite GUI text editor. For this example we will use VIM. Like Windows 7, Ubuntu’s hosts file is located in the /etc/ folder, though here it is in the root of the drive. In order to edit the file you will need to open it as root which is why we use sudo here.

在Ubuntu 10.04和大多数Linux发行版中,您可以直接在终端中编辑hosts文件。 您可以使用喜欢的编辑器,甚至可以打开喜欢的GUI文本编辑器。 在此示例中,我们将使用VIM。 与Windows 7一样,Ubuntu的hosts文件位于/ etc /文件夹中,尽管此处位于驱动器的根目录中。 为了编辑文件,您将需要以root身份打开它,这就是我们在此处使用sudo的原因。

Now that it is open we can edit it to redirect Facebook into nothing. You will notice that with Ubuntu there is also a section for IP6. For most needs you will only need to edit it the top section and ignore the IP6.

现在它已经打开,我们可以对其进行编辑以将Facebook重定向为空。 您会注意到,对于Ubuntu,还有一个IP6部分。 对于大多数需求,您只需要在顶部编辑它,而忽略IP6。

Now we can save the file and try to go to Facebook.com. Just like in windows we will see that we are now redirected to a site that does not exist.

现在,我们可以保存文件,然后尝试访问Facebook.com。 就像在Windows中一样,我们将看到我们现在已重定向到一个不存在的站点。

sshot-2010-08-31-[23-14-30]

macOS(任何版本) (macOS (Any Version))

In macOS, accessing the hosts file is very similar to Ubuntu. Begin in terminal and use your favorite editor, even is you wish to call a GUI text editor, it is easier to do so from terminal.

在macOS中,访问hosts文件与Ubuntu非常相似。 从终端开始并使用您喜欢的编辑器,即使您希望调用GUI文本编辑器,也可以从终端进行操作。

The file will look a bit more like Windows, only with a little less explanation. Again we are going to redirect Facebook.

该文件看起来更像Windows,但解释较少。 同样,我们将重定向Facebook。

This time it seems that 0.0.0.0 is a loopback and will direct you to the computers Apache test page.

这次看来0.0.0.0是一个环回,它将把您定向到计算机的Apache测试页。

笔记 (Notes)

There are some things to note from this walkthrough that we did notice. When tested it, Chrome did not use the hosts file in any operating system but we were able to block Facebook in Chrome by adding www.facebook.com. Also, make sure to place and extra line after the last entry for the section.

在本演练中,我们确实注意到了一些事情。 经过测试,Chrome并未在任何操作系统中使用hosts文件,但我们可以通过添加www.facebook.com来阻止Chrome中的Facebook。 另外,请确保在该部分的最后一个条目之后放置多余的行。

This should get you started in understanding the Hosts file and how it can help protect your computer. You can use it to block sites that you don’t want a PC to be able to access. If you have more suggestions for any of the operating systems we coved, then leave a comment and let us know!

这应该使您开始了解Hosts文件以及它如何帮助保护您的计算机。 您可以使用它来阻止您不希望PC能够访问的站点。 如果您对我们开发的任何操作系统有更多建议,请发表评论并告诉我们!

Create a Shortcut to Quickly Edit Your Hosts File in Windows

创建快捷方式以在Windows中快速编辑主机文件

翻译自: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值