How to Edit the Hosts File on Mac

By 4 weeks ago

Like Windows-primarily based totally computers, Mac has a hosts record to configure how your gadget connects to the websites on the Internet. This record consists of references to websites and IP addresses, and you may use it in lots of one-of-a-kind methods for your Mac.

One of the motives you can need to edit the hosts’ record to your Mac is to dam web sites. It’s a simply wonderful manner to dam sure web sites from being accessed to your gadget. The 2d feasible utilization is to check out your personal websites regionally to your gadget. You will have the host’s record redirect your preferred area call to the community direction of your neighborhood store.

Mac Hosts File Location

Since enhancing the host’s record is an unstable task, Apple has deliberately located it in a personal folder to your system. That’s to save you, customers, from editing it with outright information and inflicting harm to the complete system.

For the curious customers out there, the direction is /etc/hosts/ and you could get admission to it the usage of a Terminal window.

Edit Hosts File On Mac

It’s pretty smooth to edit the hosts report to your Mac due to the fact there may be an integrated editor to do it. It’s placed within the Terminal and is known as nano editor. You can use it to open and edit any textual content report along with the hosts report to your machine.

Make sure you’re using an admin account to do the following

  1. Click on Launchpad in the Dock, search for Terminal, and launch it.
  1. Type the following command into the Terminal and press Enter. It’ll open the hosts file in the nano editor.

    sudo nano /etc/hosts
  1. Since it’s a sudo command, it’ll ask you to enter your password. Enter the password and continue.
  1. The file should now be open on your screen and you can start editing it.

Understanding The Mac Hosts File

If you have not edited a host’s report before, you may locate the report chunk complex to paintings with. However, it is now no longer as hard to edit it because 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 matters you may do with the host’s record is to install redirects. You will have a site call factor to something absolutely exceptional than what it is alleged to factor too.

For example, if you are seeking to reduce down your social media usage, you may have domain names like facebook.com redirect your browser to web websites like, say, Wikipedia. You can use pretty much any area and IP cope with you want.

  1. 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.
  1. 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.
  1. Press the Tab key on your keyboard to get to the source domain field.
  2. Here, type the domain name that is to be redirected to the IP address you typed earlier. We’ll use facebook.com here.
  1. Once the changes are made, press the Ctrl + O keys on your keyboard to save the file.
  2. Press Enter to confirm the action.
  3. Press the Ctrl + X keys to close the nano editor.
  4. 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
  1. 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 could see withinside the above procedure, you want the IP to cope with the web website online which you need to redirect humans too. If you do not already recognize it, you could use a command withinside the Terminal to discover the IP cope with of any website.

  1. 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
  1. It’ll display an IP address on your screen. That’s what you can use in the hosts file.

The amazing factor approximately the host’s record is it helps you to block websites without requiring you to put in any third-birthday birthday celebration apps for your Mac. You can upload access to the record and all of the connection requests to that access can be denied.

  1. Launch the hosts file in the nano editor as shown above.
  2. Bring your cursor where the localhost entry ends and press Enter to add a new line.
  3. Type in the IP address 127.0.0.1 and press Tab on your keyboard.
  4. Enter the domain name of the site that you want to block. For example, if you want to block Instagram, type instagram.com.
  1. Press Ctrl + O to save the changes.
  2. Press Ctrl + X to close the file.
  3. Type the following command and press Enter to flush the DNS cache.

    dscacheutil -flushcache

Now on every occasion, you attempt to get right of entry to the blocked site, it’s going to take you to the localhost with a view to display a blunders page.

The Mac hosts record gives you many approaches to mess around with outgoing community requests, and you may block and unblock them as you wish. Have you used the host’s record for your Mac before? If so, what becomes it for? Let us understand withinside the feedback below.

Share
asktechnicians

Recent Posts