Raspberry Pi 3: Wifi Access Point Router with Pi-Hole (DNS-based adblocker)

いつもと感じがちがいますが、だいたいコピペでいけるかと。 :-)

## Overview
- Making your Wifi AP Router with DNS-based-Adblock on Raspberry Pi 3



## Environment

- Raspberry Pi 3 Model B
    - Ethernet(`eth0`): Connected to Internet<br>IP address `192.168.22.65` is taken from DHCP server. ex)Cable router
    - Wifi(`wlan0`): Access Point.<br>It will be static IP, set to `192.168.42.1`
- microSD 8GB
- Raspbian Jessie Lite : [2016-11-25-raspbian-jessie-lite.img](https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2016-11-29/)



## Steps

### RasPi: Initial setup to boot and to update
1. Creating bootable microSD
    - Windows 7 64bit
    - [Rufus v2.11Portable](https://rufus.akeo.ie/downloads/rufus-2.11p.exe)
        1. Choose: 'Device'
        2. Create a bootable disk using: `DD image`
        3. Click Disk icon, and pick Raspbian '.ZIP' file. (You don't need to extract zip. It will burn with extracting. :-) )
2. (Optional) Create the file named '`ssh`' on SD to allow ssh-login.
3. Boot 
4. Update Packages:<br>`sudo apt update; sudo apt upgrade -y`
5. Install **hostapd**:<br>`sudo apt install hostapd -y`

### Setup : wlan0 as STATIC IP (192.168.42.1), hostapd and ip-forwarding

```bash:Copy_and_Paste
###
# Make sure hostapd is installed :)
# sudo apt install hostapd -y 

###
# Unmanage: wlan0 by DHCPCD
echo denyinterfaces wlan0 | sudo tee -a /etc/dhcpcd.conf

###
# Modify: /etc/network/interfaces
sudo sed -i \
-e 's/allow-hotplug wlan0/#allow-hotplug wlan0/g' \
-e 's/iface wlan0 inet manual/#iface wlan0 inet manual/g' \
-e 's@    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf@#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf@'  \
/etc/network/interfaces

cat <<EOF | sudo tee -a /etc/network/interfaces
#
# wlan0 - static IP 
allow-hotplug wlan0 
iface wlan0 inet static 
    address 192.168.42.1
    netmask 255.255.255.0
    network 192.168.42.0
    broadcast 192.168.42.255
EOF


###
# Create: /etc/hostapd/hostapd.conf
#  ssid/wpa_passphrase = Pi3-AP/raspberry
#  you may modify them.
cat <<EOF | sudo tee /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
#driver=rtl871xdrv
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
ssid=Pi3-AP
wpa_passphrase=raspberry
EOF

##
# Edit: /etc/default/hostapd
echo DAEMON_CONF=\"/etc/hostapd/hostapd.conf\" | sudo tee -a /etc/default/hostapd

##
# Setting: IP Forward
sudo sed -i -e 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g'  /etc/sysctl.conf
#
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
#
echo 'iptables-restore < /etc/iptables.ipv4.nat' | sudo tee /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat

##
# Reboot! 
sudo reboot
## 
```


### [Pi-hole](https://pi-hole.net/): Install and setup
1. Install script:<br>`curl -L https://install.pi-hole.net | bash`<br>Wait until configuration starts...
2. Enter > Enter > Enter > Down Arrow > SpaceBar > Tab > Enter<br>![PiHoleWifiRouter04_.png](https://qiita-image-store.s3.amazonaws.com/0/75594/deb5074a-e6fe-f8a1-87e8-f6d3186c765e.png)
3. Up/Down and SpaceBar to select your favorite DNS, and Tab > Enter<br>![PiHoleWifiRouter05.png](https://qiita-image-store.s3.amazonaws.com/0/75594/b99c04f2-1306-80af-549b-383daf7783d9.png)
4. Down>SpaceBar>Tab>Enter<br>Uncheck IPv6<br>![PiHoleWifiRouter06_ipv4.png](https://qiita-image-store.s3.amazonaws.com/0/75594/94e938ef-a2a4-8057-93a7-7cff6aea6420.png)
5. Right(to select 'NO' when it shows eth0 IP address) > Enter<br>![PiHoleWifiRouter07.png](https://qiita-image-store.s3.amazonaws.com/0/75594/95bd8ecf-5808-4a76-0e02-9945652857ff.png)
6. Edit: `192.168.42.1/24`<br>Then, Tab > Enter<br>![PiHoleWifiRouter08.png](https://qiita-image-store.s3.amazonaws.com/0/75594/c78ac365-fe3c-4585-00ae-73c7c95cab60.png)
7. Edit: `192.168.42.1`<br>Then, Tab > Enter<br>![PiHoleWifiRouter09.png](https://qiita-image-store.s3.amazonaws.com/0/75594/12ba342d-2b0a-9cde-ed60-5b8a79dff365.png)
8. Make sure, and Enter: <br>![PiHoleWifiRouter10.png](https://qiita-image-store.s3.amazonaws.com/0/75594/d04077ca-4b94-05b6-14f4-794b99fc32de.png)
9. Tab>Enter<br>![PiHoleWifiRouter12.png](https://qiita-image-store.s3.amazonaws.com/0/75594/fc4fa3e8-6283-2ed0-b8cf-d0dd89015422.png)
10. Wait ... The necessary packages will be installed. 
11. You'll see the admin password.  <br>![PiHoleWifiRouter13_2.png](https://qiita-image-store.s3.amazonaws.com/0/75594/6a8687e5-04e5-1597-9429-97514e07954e.png)

12. Press Enter to back to shell.

13. <font color='red'>Edit: </font>**/etc/dhcpcd.conf** - remove last 4 lines<br>`sudo vi /etc/dhcpcd.conf`<br>type: `Gdddddddd:wq[Enter]` (shift+'g' to go to last line, 'dd' 4times to 4 remove lines, ':' to command, 'wq' to write and quit.)
14. Reboot<br>`sudo reboot`

15. Open WebBrowser: http://RasPi_eth0_IP_Address/admin/settings.php<br>Enter admin-password to login, Check 'DHCP server enabled' and \[Save\]
<br>![PiHoleWifiRouter14.png](https://qiita-image-store.s3.amazonaws.com/0/75594/9ea26f0c-b693-3472-9549-ef495c08f707.png)


### Connect to RPi3 Wifi Access Point
- You'll find SSID 'RPi3-AP' and 'raspberry' as passphrase. (Or you change your own ones.)
    - You should modify `/etc/hostapd/hostapd.conf` for `ssid=`and `wpa_passphrase=`. 


```txt:Windows
C:\> ipconfig
...
Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . : local
   IPv4 Address. . . . . . . . . . . : 192.168.42.213
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.42.1
...
C:\>nslookup www.google.com
Server:  raspberrypi
Address:  192.168.42.1

Non-authoritative answer:
Name:    www.google.com
Addresses:  2607:f8b0:4005:804::2004
          216.58.193.100

C:\>nslookup doubleclick.net
Server:  raspberrypi
Address:  192.168.42.1

Name:    doubleclick.net
Addresses:  2607:f8b0:4005:809::200e
          192.168.42.1

C:\>
```

## Others
- If you lost admin password, you can change it by:<br> `pihole -a -p  your_new_password`
- Related:
    - [[メモ] Raspberry Pi 3で、Wi-Fi アクセスポイントルータ](http://qiita.com/mt08/items/f2e4f9d3b1ed00849e23)
- It's not enough to have been tested, it might something wrong. :-|<br>あまりテストしてないので、不具合あるかも...