2020-05-27-blocking-connections-on-android.html (4350B) - raw


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