Where is the hosts file in Mac OS X and how to edit it. How to edit Hosts file on Mac Hosts file mac os

Most of us, for one reason or another, had to deal with a file that is quite important in any operating system called "hosts" and editing it. This file is used to map certain domain names to their network addresses and, unlike DNS, is controlled by the administrator of each specific computer, i.e. locally.

And if Windows users who have already come across this file and editing it are familiar with this concept and, most likely, remember the path to the desired file by heart (but we, just in case, recall -Windows \ system32 \ drivers \ etc \ hosts), then new users of Apple computers running Mac OS X (macOS) often get lost at first, not finding the hosts file they are looking for in their usual path, as well as the path itself in their Mac.

Precisely about where is the hosts file on mac and how is it open and edit we will explain in this detailed instruction below.

Fortunately, finding and editing the hosts file on a Mac is no more difficult than doing it on Microsoft's operating system. We know at least two methods, which will be discussed below. You can use whichever is faster and more convenient to you.

How to open hosts file through Mac OS terminal

For those Apple technology users who are not afraid of the word "terminal", this method will seem the fastest and most convenient. To view and edit the hosts file on Mac and enter the following command in it.

sudo nano / private / etc / hosts

You will also need to enter the administrator password if it is set on your computer.

If you entered everything correctly, the contents of your Mac's hosts file will open in front of you.

Make the necessary changes to this file (the mouse cursor in the terminal does not work, use the keyboard arrows to move between lines), for example, add the site you want to block.

After making all the necessary changes, press the keyboard shortcut "ctrl + X" to save.

Press the key "Y" to confirm saving changes. That's all, the hosts file has been edited successfully.

In order for the changes you made to take effect immediately, it is sometimes necessary to have your Mac.

How to open the hosts file in a text editor

For those Mac users who are not yet very good with Terminal, there is an alternative option to open and modify the hosts file using your favorite text editor and the standard Finder file manager.

To do this, right-click (or use a similar trackpad action) on the Finder icon on the bottom panel (Dock) of your computer and select the item "Go to folder ...".

In the window that opens, enter the path where the hosts file is located in computers with the OS X operating system and click the "Go" button. The path looks like this.

As a result, a Finder window will open in the folder where the file you are looking for is located. It will already be selected. Just right-click on it and open it with your favorite text editor (standard TextEdit will do as well).

The file itself is protected from editing. However, when you try to make changes, you will be prompted to create a copy of it - do so. Make the necessary changes to a copy of the file and save it, for example, on your desktop as a file called hosts. Txt permission from the saved file after that delete.

In the last paragraph of this instruction, you just need to replace the old hosts file with the newly created one by simply dragging and dropping it from the desktop into the folder where the original file is located. In the pop-up window, confirm the action by pressing the button "Replace".

It's all. As a result of the described actions, your computer will use the already new hosts file. But sometimes, in order for the changes to take effect immediately, you may need to update the DNS cache. More on this below.

Refresh your DNS cache

In most cases, the changes you made in the hosts file will be applied immediately, but if for some reason this did not happen, do not worry, try resetting the DNS cache. To do this, run the following command in the terminal (you will need to enter the administrator password).

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

After that, the rules specified in the new hosts file will definitely work as they should.

Computer users have always had two gradations: amateurs and advanced users. So for the first to explain the meaning and essence of the file hosts, the methods of editing it, apparently, does not make sense, which means that the material of this article is intended exclusively for users who are constantly interested in how their home or office assistants work.

Sometimes, editing a file hosts it is necessary after using the utility, which serves to redirect your iOS device from the IP address of Apple servers to the servers. This deception is necessary in order to take advantage of the saved ones, and thus get the opportunity to install (rollback, downgrade, downgrade).

So, after launching, TinyUmbrella can enter the "necessary lines" in the hosts file, however, if later you need to restore the iOS device to the official firmware, then iTunes is mandatory, which will mean that the program, and with it your device cannot communicate with Apple servers. As a rule, the treatment will be to delete the line 74.208.105.171 gs.apple.com from the file hosts.

So there are two ways to edit the file hosts on Mac. One of them using the native app Terminal:

  • run the program Terminal... It is in the folder Applications -> Utilities(you can also use the Shift + Cmd + U key combination while the Finder window is active);
  • we open hosts(via the command sudo nano / etc / hosts);
  • enter a password allowing the installation of new programs;
  • edit the file.

Editing is carried out by the following key combinations:

  • next / previous page - Ctrl + V / Ctrl + Y;
  • create a record - after the typed text, move the cursor to the end and press Ctrl + O;
  • exit the file - Ctrl + X;
  • delete record - move the cursor to the line to be deleted and press Ctrl + K;
  • cancellation of deletion - Ctrl + U.

There is another way to edit the file hosts... This uses the Finder and a simple text editor.

In the Finder window, find the tab Go -> Go to folder(or Shift + Cmd + G);

  • in the launched window we drive in the line / etc and click "Go";
  • find a file named hosts and double-click to open it;
  • a regular text editor will be launched with information from the file hosts, the only drawback is that all changes cannot be saved due to access restrictions.

If there is a need for the changes made to take effect immediately, experts recommend updating DNS records using the "Terminal" (command dscacheutil –flushcache).

Your Mac's hosts file is a small, but important text document that has the ability to map hostnames to specified IP addresses. Although the modern Internet uses a variety of public and private DNS servers to map IP addresses, the hosts file is a handy way to override those DNS servers, allowing you to manually point a website address to the desired IP address, or block access to a site altogether by pointing to an unused or internal IP address.

Here's how to edit the Mac hosts file on macOS (previously known as Mac OS X).

Edit Your Mac Hosts File with Text Edit

There are two primary ways to edit the hosts file in Mac OS X. The first is by using TextEdit since the hosts file is a plain text document. However, you can’t open the file directly, since it resides in a protected area of ​​the file system. Instead, we need to copy the file to an unprotected location, like the Desktop, edit it, and then copy it back.

To find the hosts file, open the Finder, which you can access by clicking on the desktop, the smiley face finder in the lower left of your screen, or by pressing command + spacebar + "Finder"... Then follow these steps:



To open it, simply double-click and it will display the file's contents in TextEdit (or the text editor of your choice).

Block IP Addresses

By default, the / etc / hosts file is relatively simple. It contains a number of lines of descriptive text which are “commented out” with the pound or number (also called a pound or hashtag) sign (#).

Any line that starts with a # sign is a comment and is not read by the / etc / hosts file. So comments are how you can add notes to your hosts file and comment out any lines that you want the / etc / hosts file to stop reading as entries but do not want to delete in case you need them in the future.

For each line, any text after the pound sign is ignored by the computer, making it a good way to add notes and descriptions to your files. It also contains default IP values ​​for localhost and broadcasthost. To edit the file, you’ll add your own lines after broadcasthost.


In addition to testing newly developed websites and web hosting migrations, another reason to edit the hosts file is to block access to specific websites.

In our example, we’ll pretend that the computer we’re using is a device that we want to use exclusively for work, not allowing ourselves to get distracted by Facebook on our work machine.

To do this, type the IP address you wish to assign, followed by two tabs and the hostname. In our case, we want to block Facebook so we’ll map www.facebook.com to 0.0.0.0 which, as an invalid IP address, will result in an error.


Now, whenever we try to go to www.facebook.com from our Mac, the Web browser will fail to load the page, hopefully encouraging us to get back to work!

Reroute Web Addresses

Alternatively, you can enter the IP address of a valid site instead of 0.0.0.0, which will result in users attempting to access Facebook being directed to the site of your choosing.

To determine a website’s IP address, you can use the dig command, which comes standard with macOS. To “dig” the site via Terminal, we simply open up Terminal and then run the dig command on URL, which will then return the IP address as output.

$ dig www.site + short
104.25.27.105

Note: The + short option keeps the output to just the information we need, which is the IP address.

Make note of the IP address that’s returned and use it in your Mac hosts file mapping. For example, The New York Times website at www.nytimes.com returns an IP address of 170.149.172.130. If we map that to Facebook in our hosts file, any time someone using the Mac tries to go to Facebook, they’ll see The New York Times load instead.


Note: Many websites won’t allow you to map a domain name to the IP address currently mapped to the site so you’ll get a security error when you try.

You can add as many entries to the hosts file as you wish by entering new mappings on each line. Once you’ve made your desired changes to the hosts file, save it in its current location on the Desktop.

Then, drag and drop the hosts file from your Desktop back to its original location at / private / etc... If you’ve closed the Finder window pointing to this directory, simply use the Finder> Go> Go to Folder command, discussed above, to reopen it.

When you drop the hosts file back in its original location, macOS will ask you what to do about the unmodified hosts file that’s already there. Choose “Replace” and then enter your administrative user password to authenticate the transfer.

With the modified hosts file now in place, fire up your Web browser to test the changes.

Flush Your DNS Cache

In most cases, the new mapping should work immediately, but if you’re not seeing the correct behavior, you may need to flush your DNS cache, forcing the browser to read from your hosts file to determine the mapping.

To flush the DNS cache on your Mac, open Terminal and use the following command (sudo enables you to run this command as the admin of your machine):

$ sudo killall -HUP mDNSResponder

Note: You will need to enter your admin password to execute this command.

To get your Mac to clear your DNS cache, then confirm it's cleared your cache by echoing a confirmation, enter these two commands separated by a semi-colon as shown here:

$ sudo killall -HUP mDNSResponder; say DNS cache has been flushed

Edit Your Mac Hosts File In Terminal With Nano

The steps in the previous section are easy enough, but if you want to avoid copying the hosts file you can edit it directly in Terminal using the UNIX Nano Text Editor, which is built into macOS.

To get started, launch Terminal, type the following command, and press Return. As with all sudo commands, you’ll need to also enter your admin password to execute it:
$ sudo nano / private / etc / hosts

You’ll now see the hosts file open in the Nano editor or vim or another editor of your choice. To navigate and edit the file in Nano, use the arrow keys on your keyboard.


Just as we did with the TextEdit method, above, we can add, edit, or remove hostname mappings at will. Because we launched Nano using sudo, any changes will be authenticated and saved directly to the original hosts file, without the need to copy it outside of its home directory.

When you’re done making changes, press Control-X to exit the editor, Y to save, and Return to overwrite the existing hosts file.

As we mentioned earlier, make sure to flush your DNS cache if you notice that your new mappings aren’t working properly.

Our examples mentioned blocking and redirecting distracting sites in a work environment but you can also use these steps to manually block access to malicious websites and, of course, other uses as well.

If you ever make a mistake and you’re not sure how to fix it, you can always restore the default hosts file contents by using one of the methods above to enter the following default information:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost :: 1 localhost
fe80 :: 1% lo0 localhost

If you are a Mac user and found this article useful, you might want to check out other TechJunkie tutorials, including and

What’s the reason you are seeking to edit your Mac’s hosts file? How did it work out? Please tell us about it in the comments below!

For developers of sites and client-server applications on Mac OS HOSTS file serves as a convenient tool for software debugging. The file has a string format, where each record describes the order in which domain names are resolved on a given computer. Virtual subnet management is an essential function for any computer with Internet access.

Technology

To open a web page, we indicate its address, which includes the domain name. So, the domain is apple.com. Physically, the page is located on the Apple server, which has some address in IPv4, for example: 205.192.12.11. To turn "apple.com" into an IP address, you need to contact the domain name server of the .com zone (in other words, the DNS server of the COM domain). Server addresses ..com, .ru, etc. are known in advance and are needed for the browser to work.

The HOSTS file has records similar to the DNS server table, with the file data taking precedence over the external DNS. This means that if you specify the IP address of your computer and the path to the folder containing some web page as the address for the apple.com domain, then it will be it that will be loaded in browsers and other third-party applications when you enter apple.com in the address bar. Such a mechanism helps to test server programs before actually shipping them to the Internet.

How to edit posts

On Mac OS HOSTS file located in system memory, so you can open it through the terminal.

  • Press Command + R to bring up the Application Launcher and select "Terminal".
  • With Nano's built-in editor, file contents can be viewed and edited right in the terminal.
  • To do this, enter the command "sudonano / private / etc / hosts" and press Enter.

The standard entry in the hosts file is to map the standard computer address to the localhost domain. Further user data can be recorded. If you want to map the address 200.200.1.1 to the mysite.com domain, you need to add the line "200.200.1.1 mysite.com"

The file structure is almost the same as HOSTS for Windows. It is important when editing to keep the broadcast address (entry from 255.255.255.255), which is used for mass distribution of network packets. If the Internet and local network settings provide for the use of the IPv6 protocol, the localhost address must also be described for it.

In addition to redirection, there may be a block of closed addresses (in particular, suspicious sites found by the antivirus are entered here), which begins with the "# Block" line.