2020-05-27-blocking-connections-on-android.md (3755B) - raw

      1 <!-- title: Blocking connections on Android -->
      2 <!-- slug: blocking-connections-on-android -->
      3 <!-- categories: Privacy -->
      4 <!-- date: 2020-05-27T19:01:00Z -->
      6 I have been a user of [NetGuard][ng] for quite some time. It is a great Android
      7 app that lets you control which apps get Internet access and which don't. The
      8 paid version will allow you to block connection on a per-domain basis (for each
      9 app), as well as let you see all the domains an app connects to (which are
     10 normally a lot!). Furthermore, you will be able to block domains for the whole
     11 phone. This is useful because it can act as an add blocker (by default it uses
     12 the list of domains gathered in [this repository][repo]). The Google Play
     13 version doesn't have this feature because Google doesn't allow add blocking on
     14 the store, so make sure you get the app directly from GitHub!
     16 A couple of months ago I decided to use a VPN, it felt like ISP's where very
     17 public about everyone's data, and I decided to put my trust in a company whose
     18 business is protecting their customers' privacy. The problem with VPNs is that
     19 you have to trust them. There is no way for you to ensure they aren't selling
     20 your browsing habits to the best bidder, but I did my research on the provider I
     21 chose and I trust them a lot more than an ISP. Now you may ask, how is this
     22 related to NetGuard? Well, NetGuard uses the VPN functionality on Android to be
     23 able to block certain connections without root access, and Android only allows
     24 one VPN at a time, so I had to choose one[^proxies].
     26 [^proxies]: NetGuard offers a way to do what I wanted, through proxies, but I
     27   didn't like the workaround.
     29 Finally, I decided to go with my VPN. However, I really liked the domain
     30 blocking feature, so I decided to investigate a little further. It turns out you
     31 can use the `/etc/hosts` files to block certain domains just like in a GNU/Linux
     32 computer. It is an easy process and it really makes a difference in your mobile
     33 browsing experience. I'll explain how I did it with my phone in case it helps
     34 anyone else (although simply installing NetGuard is a simpler solution for sure,
     35 and you get more features!).
     37 First of all install Android Debug Bridge (ADB) on your computer. If you are
     38 using GNU/Linux, you can use `pacman -S adb` on Arch based distributions or `apt
     39 install adb` on Debian based distributions, look it up if you have other
     40 distributions or operative systems. Now plug your phone into your computer and
     41 on your phone enable developer settings (look it up if you don't know how to do
     42 it) and do the following:
     44 1. `Android debugging` > `on`
     45 2. `Root access` > `ADB only`
     46 3. Make sure your computer has access to your phone by enabling `PTP` on your
     47    phone (instead of `No data transfer`).
     48 4. On the computer run `$ adb root` to get root access.
     49 5. `$ adb remount`, which will allow you to modify the file on the phone.
     50 6. `$ adb push /path/to/hosts/on/computer /etc/hosts`
     51 7. Done! You can now unplug your phone (and disable the options you enabled
     52    previously if wanted).
     54 If you want to edit the file manually, do the following after step 5:
     56 1. `$ adb shell`, which will give you a terminal on the phone.
     57 2. `# nano /etc/hosts` (`vim` also works on LineageOS).
     58 3. Do your changes.
     59 4. `# exit`
     61 Easy! However, I am using LineageOS and I am unsure if you can do step 2 on a
     62 stock ROM (if you can't, you might need a rooted device). If you try it—whether
     63 on a stock ROM or another custom ROM—, let me know if it works! You still won't
     64 be able to block certain apps' connections as with NetGuard, but you won't have
     65 ads while keeping the VPN feature available for other uses.
     68 [ng]: <https://www.netguard.me/> "NetGuard's"
     69 [repo]: <https://github.com/StevenBlack/hosts> "Unified hosts — GitHub"