与基于 Windows 的计算机一样,Mac有一个 hosts 文件来配置您的计算机如何连接到Internet上的网站。此文件包含对网站和 IP 地址的引用,您可以在Mac上以多种不同方式使用它。
您可能想要在Mac上(Mac)编辑主机文件(edit the hosts file)的原因之一是阻止网站。这是阻止某些网站在您的计算机上被访问的好方法。第二种可能的用法是在您的机器上本地测试您自己的网站。您可以让 hosts 文件将您选择的域名重定向到本地存储的网络路径。
Mac 主机文件位置(Mac Hosts File Location)
由于编辑主机文件是一项有风险的任务,Apple有意将其放置在您系统上的私人文件夹中。这是为了防止用户在没有适当知识的情况下修改它并导致整个系统损坏。
对于好奇的用户,路径是/etc/hosts/,您可以使用终端(Terminal)窗口访问它。
在 Mac 上编辑主机文件(Edit Hosts File On Mac)
在Mac(Mac)上编辑 hosts 文件相当容易,因为有一个内置编辑器可以做到这一点。它位于终端(Terminal)内,称为 nano 编辑器。您可以使用它来打开和编辑任何文本文件,包括您机器上的主机文件。
确保(Make)您使用管理员帐户执行以下步骤。
- 单击Dock 中的Launchpad,搜索Terminal并启动它。
- 在终端(Terminal)中键入以下命令,然后按Enter。它将在 nano 编辑器中打开 hosts 文件。
sudo nano /etc/hosts
- 由于它是一个 sudo 命令,它会要求您输入密码。输入密码并继续。
- 该文件现在应该在您的屏幕上打开,您可以开始编辑它。
了解 Mac 主机文件(Understanding The Mac Hosts File)
如果您之前没有编辑过 hosts 文件,您可能会发现该文件的使用有点复杂。但是,编辑它并不像看起来那么难。
您将在文件中找到的条目之一是127.0.0.1 localhost。
带数字的第一部分是Mac的本地 IP 地址。它具有主机名的第二部分是您用来访问该 IP 地址的部分。
上述两部分组合在一起时,会将所有localhost查询重定向到 IP 地址127.0.0.1。当您在浏览器中输入localhost时,您的浏览器会查看 hosts 文件,获取指定的 IP 地址,然后将您带到该 IP 地址。
简而言之,hosts 文件只不过是 IP 地址和域名的组合。您可以通过任何方式修改它们以获得所需的结果。
使用主机文件设置重定向(Set Up Redirects With The Hosts File)
您可以使用 hosts 文件做的一件事是设置重定向。您可以让域名指向与它应该指向的完全不同的东西。
例如,如果您想减少社交媒体的使用,您可以让 facebook.com 之类的域将您的浏览器重定向到诸如Wikipedia 之(Wikipedia)类的网站。您可以使用几乎任何您想要的域和 IP 地址。
让我们看看如何使用 hosts 文件设置上述重定向(set up the above redirection using the hosts file)。
- 当文件在 nano 编辑器中打开时,使用箭头键将光标移动到localhost结束的位置。然后按Enter为您的条目添加一个新行。
- 在您刚刚添加的新行中,键入要将源域重定向到的 IP 地址。我们将使用103.102.166.224,这是维基百科的 IP 地址。
- 按键盘上的Tab键进入源域字段。
- 在这里,键入要重定向到您之前键入的 IP 地址的域名。我们将在这里使用facebook.com。
- 进行更改后,按键盘上的Ctrl + O键保存文件。
- 按Enter确认操作。
- 按Ctrl + X键关闭 nano 编辑器。
- 您现在需要刷新DNS缓存以使更改生效。为此,请在终端(Terminal)中键入以下命令,然后按Enter。
dscacheutil -flushcache
- 打开浏览器,输入facebook.com,然后按 Enter(Enter)。您会发现它打开的是Wikipedia而不是Facebook。
快速提示:如何查找网站的 IP 地址(Quick Tip: How To Find a Website’s IP Address)
正如您在上述过程中看到的,您需要将人们重定向到的站点的 IP 地址。如果您还不知道,可以使用终端(Terminal)中的命令来查找任何网站的 IP 地址(find out the IP address of any website)。
- 在终端(Terminal)窗口中,键入以下命令并按Enter。确保将wikipedia.org替换为您选择的网站。
ping wikipedia.org
- 它会在您的屏幕上显示一个 IP 地址。这就是您可以在主机文件中使用的内容。
通过在 Mac 上编辑 Hosts 文件来阻止网站(Block Websites By Editing The Hosts File On Mac)
hosts 文件的优点在于它可以让您阻止网站,而无需您在(it lets you block sites)Mac上安装任何第三方应用程序。您可以在文件中添加一个条目,所有对该条目的连接请求都将被拒绝。
- 如上所示,在 nano 编辑器中启动 hosts 文件。
- 将光标移到localhost条目结束的位置,然后按Enter添加新行。
- 输入 IP 地址127.0.0.1并按键盘上的Tab 。
- 输入(Enter)要阻止的站点的域名。例如,如果您想屏蔽Instagram,请输入instagram.com。
- 按Ctrl + O保存更改。
- 按Ctrl + X关闭文件。
- 键入以下命令并按Enter以刷新 DNS 缓存。
dscacheutil -flushcache
现在,每次您尝试访问被阻止的站点时,它都会将您带到 localhost,这将显示一个错误页面。
Mac主机文件为您提供了多种方式来处理传出的网络请求,您可以根据需要阻止和取消阻止它们。您以前在Mac上使用过 hosts 文件吗?如果是这样,那是为了什么?请在下面的评论中告诉我们。
How to Edit the Hosts File on Mac
Like Windows-based computers, Mac has a hоsts file to configure how your machine connects to the websites on the Internet. This file contains references to websites and IP addresses, and you can use it in mаny different ways on your Mac.
One of the reasons you may want to edit the hosts file on your Mac is to block websites. It’s a great way to block certain websites from being accessed on your machine. The second possible usage is to test out your own websites locally on your machine. You can have the hosts file redirect your chosen domain name to the network path of your local storage.
Mac Hosts File Location
Since editing the hosts file is a risky task, Apple has intentionally placed it in a private folder on your system. That’s to prevent users from modifying it without proper knowledge and causing damage to the entire system.
For the curious users out there, the path is /etc/hosts/ and you can access it using a Terminal window.
Edit Hosts File On Mac
It’s fairly easy to edit the hosts file on your Mac since there’s a built-in editor to do it. It’s located inside the Terminal and is called nano editor. You can use it to open and edit any text file including the hosts file on your machine.
Make sure you’re using an admin account to do the following steps.
- Click on Launchpad in the Dock, search for Terminal, and launch it.
- Type the following command into the Terminal and press Enter. It’ll open the hosts file in the nano editor.
sudo nano /etc/hosts
- Since it’s a sudo command, it’ll ask you to enter your password. Enter the password and continue.
- The file should now be open on your screen and you can start editing it.
Understanding The Mac Hosts File
If you haven’t edited a hosts file before, you might find the file a bit complicated to work with. However, it’s not as difficult to edit it as it looks.
One of the entries you’ll find in the file is 127.0.0.1 localhost.
The first section with numbers is the local IP address for your Mac. The second section where it has the host name is what you use to access that IP address.
The above two parts, when combined together, redirect all the localhost queries to the IP address 127.0.0.1. When you enter localhost in your browser, your browser looks into the hosts file, obtains the specified IP address, and takes you to that IP address.
In a nutshell, the hosts file is nothing but a combination of IP addresses and domain names. You can modify these in any way you want to achieve desired results.
Set Up Redirects With The Hosts File
One of the things you can do with the hosts file is to set up redirects. You can have a domain name point to something completely different than what it’s supposed to point to.
For example, if you’re trying to cut down on your social media usage, you can have domains like facebook.com redirect your browser to sites like, say, Wikipedia. You can use just about any domain and IP address you want.
Let’s take a look at how you can set up the above redirection using the hosts file.
- While the file is open in the nano editor, use the arrow keys to bring your cursor to where localhost ends. Then press Enter to add a new line for your entry.
- In the new line that you just added, type the IP address where you want to redirect the source domain to. We’ll use 103.102.166.224, which is Wikipedia’s IP address.
- Press the Tab key on your keyboard to get to the source domain field.
- Here, type the domain name that is to be redirected to the IP address you typed earlier. We’ll use facebook.com here.
- Once the changes are made, press the Ctrl + O keys on your keyboard to save the file.
- Press Enter to confirm the action.
- Press the Ctrl + X keys to close the nano editor.
- You now need to flush the DNS cache to bring the changes into effect. To do it, type the following command into the Terminal and press Enter.
dscacheutil -flushcache
- Open a browser, type facebook.com, and hit Enter. You’ll find it opens Wikipedia rather than Facebook.
Quick Tip: How To Find a Website’s IP Address
As you can see in the above procedure, you need the IP address of the site that you want to redirect people to. If you don’t already know it, you can use a command in the Terminal to find out the IP address of any website.
- In the Terminal window, type the following command and hit Enter. Make sure to replace wikipedia.org with the website of your choice.
ping wikipedia.org
- It’ll display an IP address on your screen. That’s what you can use in the hosts file.
Block Websites By Editing The Hosts File On Mac
The great thing about the hosts file is it lets you block sites without requiring you to install any third-party apps on your Mac. You can add an entry to the file and all the connection requests to that entry will be denied.
- Launch the hosts file in the nano editor as shown above.
- Bring your cursor where the localhost entry ends and press Enter to add a new line.
- Type in the IP address 127.0.0.1 and press Tab on your keyboard.
- Enter the domain name of the site that you want to block. For example, if you want to block Instagram, type instagram.com.
- Press Ctrl + O to save the changes.
- Press Ctrl + X to close the file.
- Type the following command and press Enter to flush the DNS cache.
dscacheutil -flushcache
Now each time you try to access the blocked site, it’ll take you to the localhost which will show an error page.
The Mac hosts file provides you with many ways to play around with outgoing network requests, and you can block and unblock them as you wish. Have you used the hosts file on your Mac before? If so, what was it for? Let us know in the comments below.