If you’ve done enough with networking, you probably know about the tried-and-true hosts file. With this, you can do a lot of things (such as map addresses to names) — but one thing you can also do is block ads (or even websites). It’s not the easiest task you’ll ever come across, but it’s certainly worth the challenge.

But why block ads? In a perfect world, that wouldn’t be necessary. Unfortunately, we don’t live in a perfect world, and there are advertising networks out there that have nefarious intentions. To that end, we must protect our devices and our data. If you have any interest in getting your fingers dirty with editing an Android hosts file to block advertisements, you can go a long way to protect said data and your device. Let me show you how this is done.

The one caveat to this is that the device must be rooted (because you need root access to edit or overwrite the hosts file). If you already have a rooted device, this is incredibly simple to do. If your device isn’t rooted, you’ll need to tackle that first. I have covered rooting the Samsung Galaxy S4 in my piece “How to safely root the Verizon Samsung Galaxy S4“. Look that piece over to get the gist of rooting your device.

Once you have the device rooted, the task becomes easy. What you must do is edit the /system/etc/hosts file to direct all ad sites to 127.0.0.1. There are two routes to success with this task. The first (and easiest) is to download this hosts file (written specifically to block a massive number of ad sites by directing all ad sites to 127.0.0.1). Once that file is on your PC, plug in your Android device and copy the file to /system/etc on the Android file system hierarchy. You’ll have to overwrite the current file and use a file manager that can actually see /etc/system, such as ES File Explorer (Figure A).

Figure A

Viewing the hosts file on a rooted Verizon-branded Samsung Galaxy S4.

If you don’t want to replace your default hosts file with a third-party source, you can manually edit that file. What you’ll have to do is locate the ad networks you want to block and create a new hosts file with this format:

172.0.0.1 adnetwork_url

Where adnetwork_url is the actual URL address for the ad network server.

Once you’ve written that file (you’ll have to create it with a text editor and save it as hosts — with no extension), plug your Android device into your PC, and then overwrite your /system/etc/hosts file using your file manager.

You can also edit your hosts file on the device directly. To do this, you must open the file through a supported file manager (such as ES File Manager). Before you do so, you have to make sure the hosts file has the correct properties to allow you to write. To do that, follow these steps:

  1. Open ES File Manager
  2. Navigate to /system/etc
  3. Locate and long-press the hosts file
  4. Tap the menu button
  5. Select Properties
  6. Tap Change in the Permissions section
  7. Tap the check box for Group under the Write column (Figure B)
  8. Tap OK
  9. Tap Cancel
  10. Reboot device

Figure B

Tap the check box for Group under the Write column.

Now, it’s time to edit the hosts file. Here’s how:

  1. Open ES File Manager
  2. Navigate to /system/etc
  3. Locate and long-press the hosts file
  4. Tap the More button
  5. From the resulting pop-up menu, tap Open as (Figure C)
  6. Tap Text
  7. Select which editor you want to use to edit the file (you’ll need to use a text editor such as Turbo Editor — one that can gain superuser access)
  8. Create your entries
  9. Save the file (how will depend on which editor you use)

Figure C

The ES File Manager popup window.

Once you’ve edited the hosts file, it’s time to test. Open up your web browser (on the Android device) and point it to one of the blocked ad servers. Upon testing, you should see the This webpage is not available error (Figure D). Congratulations, you’ve officially blocked an ad server on your Android device.

Figure D

A blocked ad server on the Android device.

Ads are a necessary evil for many apps — especially those that are free. But when you hear of a malicious ad server, and you want to block it, one of the easiest methods is by editing the hosts file on your rooted device. Of course, if you don’t have a rooted device, your only option is to use a third-party app like AdBlock Free — which really only blocks pop-up ads within your browser. If you really want to prevent those ads, hosts file editing is the best route.

Have you successfully managed to block ads from your Android device? If so, what route did you take? Share your experience in the discussion thread below.

Subscribe to the Innovation Insider Newsletter

Catch up on the latest tech innovations that are changing the world, including IoT, 5G, the latest about phones, security, smart cities, AI, robotics, and more. Delivered Tuesdays and Fridays

Subscribe to the Innovation Insider Newsletter

Catch up on the latest tech innovations that are changing the world, including IoT, 5G, the latest about phones, security, smart cities, AI, robotics, and more. Delivered Tuesdays and Fridays