TranslateProject/sources/tech/20221129.5 ⭐️⭐️ How to Set Up Pi-hole to Get an Ad-free Life.md

263 lines
15 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[#]: subject: "How to Set Up Pi-hole to Get an Ad-free Life"
[#]: via: "https://itsfoss.com/setup-pi-hole/"
[#]: author: "Pratham Patel https://itsfoss.com/author/pratham/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Set Up Pi-hole to Get an Ad-free Life
======
Pi-hole is a DNS-based advertisement blocker. Unlike a Chrome or Firefox extension, a Pi-hole can block ads even on your TV! So lets see how to install and take advantage of this amazing tool!
### What is Pi-hole?
Pi-hole is a DNS Server. It blocks advertisement serving domains. Set it up on a dedicated Raspberry Pi or some other computer and then use its IP address as the DNS of your device. If you use it as DNS of your router, youll get an ad-free experience on all connected devices, even your smart TVs and smartphones.
Want more details? Let me explain.
A DNS Server tells your computer what the IP address for `google.com` is. Without a _valid_ IP address, your computer can not communicate over the Internet to another computer.
Pi-hole has a list of domains that must be blocked. Once a computer queries Pi-holes DNS Server for the IP address for a website like `adservice.google.com`, if it is a domain that must be blocked, then, Pi-hole will respond back with an invalid IP address (which is usually `0.0.0.0`).
Since “0.0.0.0” is not a valid IP address, your computer can never talk to the `adservice.google.com` website. This results in the blocking of advertisements.
Set it at the router level and you go ad-free for your entire home networkyes, even for your “smart” devices like TV, toaster and washing machineinstead of being limited to your browser.
### Setting up Pi-hole
Using something like this requires some level of experience with the Linux command line, time and patience. Its more of a [DIY Raspberry Pi project][1] but you can also use it with a normal computer running [Pi-hole][2] in a container.
So, Ill be discussing two methods of installing Pi-hole:
- Automated install on a Raspberry Pi device
- Using Docker or Podman to run Pi-hole in a container
Let us cover the easier method first method.
#### Method 1: Automated installation of Pi-hole (requires Raspberry Pi)
The automated installation is the simplest installation method for installing Pi-hole. It has a few requirements. The picture below mentions OS and hardware support.
![Pi-hole support for hardware and Operating System][3]
As you can see above, Pi-hole supports most of the popular Linux distributions. From my personal experience, Pi-hole does not consume more than ~100 MB of RAM and only uses less than 1% of CPU. Meaning it can even run on a [Raspberry Pi Zero W][4]!
Now that you know which hardware is supported, let us start with the installation steps!
To install Pi-hole using the automated installation method, all you need to do is run the following command. I understand that running a bash script downloaded from the internet is not usual but this is the official installation method.
```
curl -sSL https://install.pi-hole.net | bash
```
Once you run the above command, the Pi-hole installer will start and begin to install necessary dependencies and then prompt you with the following screen, indicating that the installer has begun.
![Pi-hole installer's initial screen][5]
PS: You can use your mouse to interact with this command line installer ;)
As depicted from the message shown below, Pi-hole is a free and open source software that mainly relies on donations made by normal folks like you and me. If you find Pi-hole to be useful, please consider donating. [Here is the hyperlink to Pi-holes donations so you dont have to type the URL yourself][6] ;)
![Pi-hole is a FOSS project. Please donate if it helped you.][7]
Next up, you will be asked if the computer on which Pi-hole is being installed has a static IP address for your Local Area Network or not. Since your computers need to know about Pi-holes IP address beforehand, it is best that the assigned IP address does not change. For more information on how to achieve this, please consult your routers manual; look for the part with “static/reserved IP address”.
![The Pi-hole installer recommends that the IP address of your computer is a static IP address.][8]
Once you have a static IP assigned to the computer running the Pi-hole, press continue. In the next step you will be asked to choose a DNS provider. This is the server that is asked for [DNS Resolution][9]. Some of the most popular DNS providers are listed for you to choose from.
Generally, I would recommend that you use either the “Quad9 (filtered, ECS, DNSSEC)” option or the “OpenDNS (ECS, DNSSEC)” option or “Cloudflare (DNSSEC)” option. They are quite trusted and have good privacy policy (as opposed to Googles DNS service).
![Choosing an upstream DNS provider for the Pi-hole][10]
Once you have selected a DNS provider, you will be asked for another choice. Here, you are asked to choose a “blocklist” that contains a list of websites to block. Pi-hole has a recommended blocklist and is asking if you want to use said blocklist.
I have used this blocklist and it does a good job of blocking a _majority_ of advertisements so I highly recommend you say “Yes” to this prompt.
![Choosing between using the default blocklist or no blocklist][11]
If you want to monitor items like “Number of total DNS queries”, “Number of DNS queries blocked/passed”, etc, you can enable the Web UI to view this data. [This is what the Pi-hole Web UI looks like][12] (this is an older announcement and the Web UI may have changed by the time you read this article).
Disabling or enabling the Pi-hole Web UI will not affect the functionality of Pi-hole itself. It is just another way to manage Pi-hole.
![The Pi-hole installer needs user input regarding the availibility of a Web UI.][13]
If you chose to install the Pi-hole Web UI, the installer will ask you to if you want to install the “lighttpd” web server. This is unnecessary if you already have a web server like Apache. But if you do not already have a web server installed already, I recommend you let the Pi-hole installer handle the installation and setup of the lighttpd web server.
![Should the Pi-hole installer install 'lighttpd' web server or does the user already have a web server?][14]
For the Pi-hole Web UI to show accurate statistics, the data needs to be logged. The next step is asking if you want to enable logging of queries. It logs items like which computer made a query for which domain name and if it was blocked or allowed, etc.
If you have enabled the Pi-hole Web UI, I recommend that you enable this.
![THe Pi-hole installer is asking if the user needs queries logged.][15]
If you enabled query logging in the previous step, you will now be asked for the verbosity of logging. Choose the logging level that you are most comfortable with and proceed with the next step.
![The Pi-hole installer is asking for verbosity of logs.][16]
The installation is now complete! Pi-hole is up and running now.
**But dont close this window just yet!** If you have enabled the Pi-hole Web UI, you will be given a password that will be used to log in the Pi-hole Web UI. Please note this down.
![The final screen of Pi-hole automated installation that shows you methods of accessing the Pi-hole Web UI and the randomly generated password][17]
Once the installation finishes, you will be shown the methods for accessing Pi-hole.
In my case, since the computers IP address is “192.168.122.191”, I will type the address `http://192.168.122.191/admin` in my web browser to access Pi-hole Web UI.
Or, if I am already using “192.168.122.191” as my DNS server, I can simply type in `http://pi.hole/admin` to view it.
#### Method 2: Install Pi-hole using Podman/Docker
This is the recommended method:
- If you want to deploy Pi-hole without much hassle and/or do not wish to interact with any installer prompts (it is only a 3-step process!)
- If you want to test Pi-hole without actually having to install it and without having your config files modified
- If you want a “reproducible” setup without having to configure everything exactly like before
For this method, you must have either Podman or Docker installed. For this tutorial, I will be using Docker on Ubuntu 22.04 LTS. However, you can follow the steps on any Linux distribution.
##### Step 1: Install Docker
As discussed above, you must have Docker installed. If you dont have it installed, we have covered the procedure about [installing Docker on Ubuntu][18].
##### Step 2: Create a docker-compose file
The easiest way to get a container like Pi-hole up and running via Docker is by using the docker-compose file.
You can create the docker-compose file anywhere you wish; its location does not matter. Below are the contents of the `docker-compose.yml` file:
```
version: '3'
services:
pihole:
image: docker.io/pihole/pihole:latest
container_name: pihole-aditi
restart: unless-stopped
ports:
- '53:53/tcp'
volumes:
- './pi-hole/etc-pihole:/etc/pihole'
- './pi-hole/etc-dnsmasq.d:/etc/dnsmasq.d'
environment:
TZ: 'Asia/Kolkata'
WEBPASSWORD: 'your-password-here'
```
**Please replace the string `your-password-here` with a safe and strong password**. This is the password for the Pi-hole Web UI.
##### Step 3: Starting the Pi-hole container
We have a few prerequisites to satisfy before starting the Pi-hole container.
The first pre-requisite is to create a few directories. Do so by running the following command in your terminal:
```
mkdir -vp pi-hole/etc-{pihole,dnsmasq.d}
```
These directories will store only the configuration files, so their size will not be greater than a few hundred MBs. These directories should be created in the same location as the `docker-compose.yml` file.
**This next step is optional but if you are following this guide on Fedora or a RHEL-based distribution**, you need to open port 53 in your firewall.
```
sudo firewall-cmd --add-service=dns --permanent
sudo firewall-cmd --reload
```
Once this is done, we can start out Pi-hole container! Do so by running the following command:
```
docker-compose up -d
```
Executing the above command will automatically fetch the latest Pi-hole image and start a container for you. Logging into the Pi-hole Web UI is the same as the previous method. Either type in the IP address of your computer or the `pi.hole` address in your web browser followed by the `/admin` string.
Both of the following methods are valid for accessing the Pi-hole Web UI:
- `http://<IP_ADDRESS>/admin`
- `http://pi.hole/admin`
You now have Pi-hole installed on your comptuer using Docker! How cool is that?!
### Setting up Pi-hole
To start using Pi-hole, you must follow either of the following methods:
- Add the computers IP address with Pi-hole installed as the DNS server for your router. This is the most recommended method since it enables blocking ads on tricky devices to configure. Please refer to your routers manual on how this can be achieved.
- You can add the IP address of the computer hosting Pi-hole as the DNS server for every computer, phone or tablet on your network. This can be tedious but useful in cases where you wish to allow ads on particular devices. I do not recommend this unless you know what you are doing.
Once you have followed either method 1 or method 2, you can check whether Pi-hole is working.
```
dig +short @<IP_ADDRESS_OF_YOUR_PI-HOLE_SERVER> ads.google.com
```
The `dig` utility is helpful for looking up corresponding IP address for each domain name. In this command, you are querying our Pi-hole server to get the IP address of “ads.google.com” is. The website “ads.google.com” is used to serve ads. So, if you get back `0.0.0.0`, your Pi-hole is working!
Below is the output from my computer:
```
$ dig +short @192.168.122.191 ads.google.com
0.0.0.0
```
As you can see, the IP address I got back from Pi-hole is infact an invalid IP address. Meaning any communication to Googles Ad servers is blocked. Yay!
But let us also see if “google.com” is working. Where will we go to solve our future problems if it doesnt work? So lets see that too!
You can run the same command as above but with “google.com” instead of “ads.google.com”. If the Pi-hole is working correctly, **we should get a** _**valid**_ **IP address in return**. Lets see what happens on my computer.
```
$ dig +short @10.0.0.14 google.com
216.58.203.46
```
As expected, “google.com” works but “ads.google.com” is blocked. Our Pi-hole server is working as intended. Perfect!
### Conclusion
It requires some effort and expertise to set up Pi-hole to get an ad-free internet experience. As you can see, its not entirely complicated. You need to be patience with such DIY projects.
For a Raspberry Pi lover like me, using Pi-hole gives good practice for building projects with [amazing single-board computers][19].
I have tried giving all the proper steps but I understand if it doesnt work for you. If you face any issues, please let me know in the comments and Ill try to help you out.
--------------------------------------------------------------------------------
via: https://itsfoss.com/setup-pi-hole/
作者:[Pratham Patel][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/pratham/
[b]: https://github.com/lkxed
[1]: https://itsfoss.com/raspberry-pi-projects/
[2]: https://pi-hole.net/
[3]: https://itsfoss.com/wp-content/uploads/2022/11/image.png
[4]: https://itsfoss.com/raspberry-pi-zero-w/
[5]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-01.png
[6]: https://pi-hole.net/donate/
[7]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-02.png
[8]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-03.png
[9]: https://www.datadoghq.com/knowledge-center/dns-resolution/#how-does-dns-resolution-work
[10]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-04.png
[11]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-05.png
[12]: https://pi-hole.net/blog/2018/01/13/pi-hole-web-interface-the-next-generation/
[13]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-06.png
[14]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-07.png
[15]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-08.png
[16]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-09.png
[17]: https://itsfoss.com/wp-content/uploads/2022/11/pihole-automated-install-10.png
[18]: https://linuxhandbook.com/install-docker-ubuntu/
[19]: https://itsfoss.com/raspberry-pi-alternatives/