{"id":44,"date":"2021-10-05T13:11:13","date_gmt":"2021-10-05T11:11:13","guid":{"rendered":"https:\/\/www.mutareb.com\/?p=44"},"modified":"2021-10-05T17:39:51","modified_gmt":"2021-10-05T15:39:51","slug":"pi-hole-on-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.mutareb.com\/index.php\/2021\/10\/05\/pi-hole-on-raspberry-pi\/","title":{"rendered":"Pi-Hole on Raspberry Pi"},"content":{"rendered":"\n<p>The internet has always been invested with a lot of Ads, but the amount of tracking, personalized and intrusive Ads has grown exponentially.<\/p>\n\n\n\n<p>While I&#8217;ve kept my own devices setup to combat them, the number of smart devices and also users is growing at home. In addition, blocking the ads from displaying or the scripts from running happens on the end device, after they have been fetched and loaded. The Pi-Hole project on the other hand blocks the requests sent by the end device on the network level, which safes resources and bandwidth.<\/p>\n\n\n\n\n\n<p>Here is how I installed and configured my Pi-Hole Raspbi to filter and reduce Ads and tracking in the whole home network, across all devices.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Requirements<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Raspberry Pi with SD card &amp; power supply: 35\u20ac single point of failure<\/li><li><a href=\"https:\/\/www.raspberrypi.org\/software\/operating-systems\/\" target=\"_blank\" rel=\"noreferrer noopener\">Raspbian Lite<\/a>: minimal Raspberry OS to keep the device lite and fast<\/li><li>Laptop with\/and SD card reader: to install OS to SD card<\/li><li><a href=\"https:\/\/unetbootin.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">UNetbootin<\/a> or <a href=\"https:\/\/www.balena.io\/etcher\/\" target=\"_blank\" rel=\"noreferrer noopener\">Etcher<\/a>: to burn the Raspbian image to the SD card<\/li><li>Putty: SSH Client to manage the Raspbi remotely<\/li><li><a href=\"https:\/\/pi-hole.net\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pi-Hole<\/a>: the ads blocking local DNS server<\/li><\/ul>\n\n\n\n<p>I have used an old <a href=\"https:\/\/www.raspberrypi.org\/products\/raspberry-pi-2-model-b\/\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi 2 model B<\/a> I had purchased in 2015 for my first Eclipse SmartHome project. Since then, this particular Raspbi has gone through quite a few projects, like the <a href=\"https:\/\/github.com\/MiczFlor\/RPi-Jukebox-RFID\" target=\"_blank\" rel=\"noreferrer noopener\" data-type=\"URL\" data-id=\"https:\/\/github.com\/MiczFlor\/RPi-Jukebox-RFID\">RFID Jukebox<\/a> and a <a href=\"https:\/\/www.eclipse.org\/kura\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kura gateway<\/a> for an <a href=\"https:\/\/smartwatering.blogspot.com\/2017\/02\/final-report-quick-summary-to-project.html\" target=\"_blank\" rel=\"noreferrer noopener\">Eclipse Open IoT challenge in 2017<\/a>.<\/p>\n\n\n\n<p>If you don&#8217;t have a Raspberry laying around to spare, you will need a Raspberry Pi (35\u20ac), SD card(<a href=\"https:\/\/www.amazon.de\/SanDisk-Ultra-microSDHC-Speicherkarte-Adapter\/dp\/B073K14CVB\/ref=psdc_1626220031_t3_B00T2U7R7I\" target=\"_blank\" rel=\"noreferrer noopener\">7\u20ac<\/a>), Raspberry Case(<a href=\"https:\/\/www.amazon.de\/Aukru-transparent-Geh%C3%A4use-Raspberry-K%C3%BChlk%C3%B6rper\/dp\/B00UCSO6SW\/ref=pd_sbs_14\/261-8408381-5445944?psc=1\" target=\"_blank\" rel=\"noreferrer noopener\">6\u20ac<\/a>), a CAT5e cable(<a href=\"https:\/\/www.amazon.de\/Premium-Cord-Patchkabel-RJ45-RJ45-Level\/dp\/B07L5KTBQ2\/ref=sr_1_28?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&amp;crid=2MXM1U07FZUCH&amp;keywords=cat%2B5e&amp;s=computers&amp;sprefix=cat%2B5%2Ccomputers%2C182&amp;sr=1-28&amp;th=1\" target=\"_blank\" rel=\"noreferrer noopener\">0.5\u20ac<\/a>) and an USB charger(<a href=\"https:\/\/www.amazon.de\/Aukru-Micro-USB-Ladeger%C3%A4t-Stromversorgung-Raspberry\/dp\/B01566WOAG\/ref=pd_bxgy_1\/261-8408381-5445944?psc=1\" target=\"_blank\" rel=\"noreferrer noopener\">10\u20ac<\/a>). <br>This is less than 60\u20ac for improving privacy and internet safety at home.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Raspbian Lite<\/h2>\n\n\n\n<p>Go to the <a href=\"https:\/\/www.raspberrypi.org\/software\/operating-systems\/\" target=\"_blank\" rel=\"noreferrer noopener\">official Raspbrian site<\/a> and grab the Raspberry Pi OS Lite. Use UNetbootin or Etcher to burn it to the SD card.<br>BTW, here is an alternative installation using <a href=\"https:\/\/github.com\/pi-hole\/docker-pi-hole\" target=\"_blank\" rel=\"noreferrer noopener\">Docker<\/a>, which is also well documented.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enable SSH<\/h3>\n\n\n\n<p>Don&#8217;t forget to add a single SSH text file, without the &#8220;.txt&#8221; file extension, this will activate the SSH server on the Raspberry, so you need to do it before you install the SD card into the Raspberry Pi.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Pi-Hole<\/h2>\n\n\n\n<p>That&#8217;s it, the Raspbi is ready. Now we just need to plug it the card in, connect the Raspi to the router and log into the system using SSH.<br>First thing to do is change the default password and update the OS<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">passwd\nsudo apt-get update\nsudo apt-get dist-upgrade<\/pre>\n\n\n\n<p>And install Pi-hole using these two commands<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget -O basic-install.sh https:\/\/install.pi-hole.net\nsudo bash basic-install.sh<\/pre>\n\n\n\n<p>The installation process is quite forward, just remember to choose a good upstream DNS for Pi-hole. Tip: you might want to avoid Google.<\/p>\n\n\n\n<p>You can check your Pi-Hole Admin interface at 192.168.XXX.XXX\/admin<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"401\" src=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pi-hole-admin-1024x401.jpg\" alt=\"pi-hole admin web interface\" class=\"wp-image-48\" srcset=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pi-hole-admin-1024x401.jpg 1024w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pi-hole-admin-300x117.jpg 300w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pi-hole-admin-768x301.jpg 768w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pi-hole-admin.jpg 1262w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Change the DNS in your Router<\/h3>\n\n\n\n<p>You need to change the primary DNS for the network in your router configuration, so that most devices will use it. I write most, because there are a rising number of applications and devices that have hard coded DNS settings to offset this, read more about it on this <a href=\"https:\/\/www.reddit.com\/r\/pihole\/comments\/930g2z\/psa_google_services_including_ads_and_others_try\/\" target=\"_blank\" rel=\"noreferrer noopener\">Reddit thread<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setup Log2Ram<\/h3>\n\n\n\n<p>Log2Ram reduces the number of times that Pi-hole writes to your SD card, which can reduce the lifespan of your SD Card. Check out <a href=\"https:\/\/github.com\/azlux\/log2ram\" target=\"_blank\" rel=\"noreferrer noopener\">their GitHub page<\/a> for all the information you need to set it up and customize it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DNS-Over-HTTPS<\/h3>\n\n\n\n<p>If you would like to have an even more secure DNS you can set up your Pi-Hole to run DNS-Over-HTTPS.<\/p>\n\n\n\n<p>In case you came across DNS-OVER-TLS before and are wondering why I opted for DNS-Over-HTTPS instead, you can read about the difference here on this <a href=\"https:\/\/www.cloudflare.com\/learning\/dns\/dns-over-tls\/\" target=\"_blank\" rel=\"noreferrer noopener\">cloudflare blog post<\/a>.<br>In short: DOH provides more privacy, as DNS queries are hidden within the larger flow of HTTPS traffic.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installing cloudflared<\/h4>\n\n\n\n<p>The installation is fairly straightforward. We download the precompiled binary and copy it to <code>\/usr\/local\/bin\/<\/code> to allow execution by the cloudflared user. Then proceed to run the binary with the <code>-v<\/code> flag to check it is all working.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget https:\/\/bin.equinox.io\/c\/VdrWdbjqyF\/cloudflared-stable-linux-arm.tgz\ntar -xvzf cloudflared-stable-linux-arm.tgz\nsudo cp .\/cloudflared \/usr\/local\/bin\nsudo chmod +x \/usr\/local\/bin\/cloudflared\ncloudflared -v\n<\/pre>\n\n\n\n<p>We need to create a cloudflared user for the daemon<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo useradd -s \/usr\/sbin\/nologin -r -M cloudflared<\/pre>\n\n\n\n<p>We will create a config file at \/etc\/default\/cloudflared to pass the options to the daemon at startup. It will contain the following command-line options<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Commandline args for cloudflared\nCLOUDFLARED_OPTS=--port 5053 --upstream https:\/\/1.1.1.1\/dns-query --upstream https:\/\/1.0.0.1\/dns-query<\/pre>\n\n\n\n<p>And it needs to be owned by the new user<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown cloudflared:cloudflared \/etc\/default\/cloudflared\nsudo chown cloudflared:cloudflared \/usr\/local\/bin\/cloudflared<\/pre>\n\n\n\n<p>Now we need to create the <code>systemd<\/code> script by copying the following in to <code>\/lib\/systemd\/system\/cloudflared.service<\/code>. This will control the running of the service and allow it to run on startup.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[Unit]\nDescription=cloudflared DNS over HTTPS proxy\nAfter=syslog.target network-online.target\n\n[Service]\nType=simple\nUser=cloudflared\nEnvironmentFile=\/etc\/default\/cloudflared\nExecStart=\/usr\/local\/bin\/cloudflared proxy-dns $CLOUDFLARED_OPTS\nRestart=on-failure\nRestartSec=10\nKillMode=process\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n\n\n\n<p>Now enable the systemd service to run at startup<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable cloudflared\nsudo systemctl start cloudflared\nsudo systemctl status cloudflared<\/pre>\n\n\n\n<p>Now let&#8217;s that it&#8217;s working<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/dig-cloudflared.jpg\" alt=\"\" class=\"wp-image-60\" width=\"798\" height=\"502\" srcset=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/dig-cloudflared.jpg 827w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/dig-cloudflared-300x189.jpg 300w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/dig-cloudflared-768x484.jpg 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Reconfiguring DNS<\/h4>\n\n\n\n<p>We now have a working DNS-over-HTTPS service, at least locally on the Raspberry. The next steps will cover how to implement the service for network-wide DNS lookups.<br>Go to the Pi-Hole Admin interface -&gt; Settings -&gt;DNS tab<br>You will need to disable the selected upstream DNS server and add the new local cloudflared daemon<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"614\" src=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pihole-dns-settings-https-1024x614.jpg\" alt=\"\" class=\"wp-image-61\" srcset=\"https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pihole-dns-settings-https-1024x614.jpg 1024w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pihole-dns-settings-https-300x180.jpg 300w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pihole-dns-settings-https-768x460.jpg 768w, https:\/\/www.mutareb.com\/wp-content\/uploads\/2021\/10\/pihole-dns-settings-https.jpg 1263w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Keeping cloudflared updated<\/h4>\n\n\n\n<p>To keep cloudflared updated, we will need a weekly cron job. Just create the file <code>\/etc\/cron.weekly\/cloudflared-updater<\/code> with the following update commands<\/p>\n\n\n\n<pre id=\"__code_15\" class=\"wp-block-code\"><code>wget https:\/\/github.com\/cloudflare\/cloudflared\/releases\/latest\/download\/cloudflared-linux-arm\nsudo systemctl stop cloudflared\nsudo cp .\/cloudflared-linux-arm \/usr\/local\/bin\/cloudflared\nsudo chmod +x \/usr\/local\/bin\/cloudflared\nsudo systemctl start cloudflared\ncloudflared -v\nsudo systemctl status cloudflared<\/code><\/pre>\n\n\n\n<p>and adjust permissions<\/p>\n\n\n\n<pre id=\"__code_17\" class=\"wp-block-code\"><code>sudo chmod +x \/etc\/cron.weekly\/cloudflared-updater\nsudo chown root:root \/etc\/cron.weekly\/cloudflared-updater<\/code><\/pre>\n\n\n\n<p>That&#8217;s it. You now have a working Pi-Hole running DNS queries over HTTPS. You can test your connection <a href=\"https:\/\/en.internet.nl\/connection\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The internet has always been invested with a lot of Ads, but the amount of tracking, personalized and intrusive Ads has grown exponentially. While I&#8217;ve kept my own devices setup to combat them, the number of smart devices and also users is growing at home. In addition, blocking the ads from displaying or the scripts&hellip; <a class=\"more-link\" href=\"https:\/\/www.mutareb.com\/index.php\/2021\/10\/05\/pi-hole-on-raspberry-pi\/\">Continue reading <span class=\"screen-reader-text\">Pi-Hole on Raspberry Pi<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":76,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[13,11,12],"series":[7],"class_list":["post-44","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it-security","tag-privacy","tag-raspberry-pi","tag-security","series-linux-server","entry"],"_links":{"self":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":12,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions\/78"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/media\/76"}],"wp:attachment":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/tags?post=44"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/series?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}