Giter VIP home page Giter VIP logo

synology-pihole's Introduction

synology-pihole

Install or Update Pi-Hole as Docker Container on a Synology NAS with a Static IP Address

AboutBuilt WithPrerequisitesDeploymentUsageContributingCreditsDonateLicense

About

Pi-hole is an open-source application that blocks advertisements and internet tracking on a private network. By setting up Pi-hole as DNS server on your local router, all devices connected to your network will automatically benefit from this ad-blocking feature. This script simplifies the setup of Pi-hole on a Synology network-attached storage (NAS). It uses Docker to isolate Pi-hole from the NAS. It also assigns a static IP address to the Pi-hole instance using a virtual network (macvlan) to prevent any port conflicts. At the moment, the script only supports IPv4.

Built With

The project uses the following core software components:

  • Docker - Container platform (including Compose)
  • Pi-hole - DNS sinkhole to block unwanted content

Prerequisites

Synology-pihole runs on a Synology NAS with DSM 6 or later. The script has been tested with a DS918+ running DSM 7.0-41890. Other prerequisites are:

  • SSH admin access is required - synology-pihole runs as a shell script on the terminal. You can enable SSH access in DSM under Control Panel ➡ Terminal & SNMP ➡ Terminal.

  • Docker and Docker Compose are required - synology-pihole runs as a Docker container. Install Docker on your NAS in DSM via Package Center ➡ All Packages ➡ Docker and ensure the status is Running.

  • A range of at least four local IP addresses needs to be reserved - synology-pihole assigns Pi-hole to a static IP address. To avoid any networking conflicts, a minimum range of four consecutive IP addresses need to be exclusively reserved by your DHCP server. This calculator displays the characteristics for a given IP address and netmask (the script defaults to /30). Please refer to the manual of your modem and/or DHCP server on how to reserve an IP range.

Deployment

Deployment of synology-pihole is a matter of cloning the GitHub repository. Login to your NAS terminal via SSH first. Assuming you are in the working folder of your choice, clone the repository files. Git automatically creates a new folder synology-pihole and copies the files to this directory. Then change your current folder to simplify the execution of the shell script.

git clone https://github.com/markdumay/synology-pihole.git
cd synology-pihole

Usage

Synology-pihole requires sudo rights. Use the following command to invoke synology-pihole from the command line.

sudo ./syno_pihole.sh [OPTIONS] [PARAMETERS] COMMAND

As an example, the following command installs Pi-hole on your NAS at the address 192.168.0.250.

sudo ./syno_pihole.sh --ip 192.168.0.250 install

The virtual network does not persist during a reboot. Invoke the following command to recreate the network.

sudo ./syno_pihole.sh --ip 192.168.0.250 network

Run the following command to update an existing Pi-hole container if a newer version is available.

sudo ./syno_pihole.sh update

Commands

Synology-pihole supports the following commands.

Command Description
install Installs Pi-hole as Docker container
network Creates or recreates virtual network
update Updates an existing Pi-hole Docker container
version Shows host and Pi-hole versions

In addition, the following options are available.

Option Alias Parameter Description
-f --force Bypass checks to force the installation / update
-l --log LOG FILE Redirect output to LOG FILE

Synology-pihole supports several advanced settings through either command-line parameters or a .env file. An example sample.env is available in the git repository. The command-line parameters take precedence over settings in the .env file.

Variable Parameter Required Example Description
PIHOLE_IP -i, --ip Yes 192.168.0.250 Static IP address of Pi-hole, ensure this IP address is available
INTERFACE -n, --interface No eth0 Host network interface to the subnet, auto-detected if omitted
SUBNET -s, --subnet No 192.168.0.0/24 CIDR notated subnet the Pi-Hole will join, auto-detected if omitted
GATEWAY -g, --gateway No 192.168.0.1 Subnet gateway router address (see --subnet), auto-detected if omitted
HOST_IP --host-ip No 192.168.0.3 New host address for communicating with Pi-hole via macvlan bridge interface. By default the lowest address starting at the first (not the Pi-hole address) of the Docker network range (see --range) is used
IP_RANGE -r, --range No 192.168.0.250/30 CIDR notated address range for Docker to assign to containers attached to the created 'Docker macvlan Network', defaults to PIHOLE_IP/32
VLAN_NAME -v, --vlan No macvlan0 Name assigned to the generated macvlan interface on the host to enable container <-> host communication (defaults to macvlan0)
MAC_ADDRESS -m, --mac No 70:d9:5a:70:99:cd Unicast MAC to assign Pi-hole, randomized if omitted
DOMAIN_NAME -d, --domain No example.com Fully qualified domain of the subnet
PIHOLE_HOSTNAME -H, --host No pihole Hostname of Pi-hole, defaults to pihole
TIMEZONE -t, --timezone No Europe/Amsterdam Local Timezone (see Wikipedia for an overview, auto-detected if omitted)
DNS1 --DNS1 No 1.1.1.1 Primary DNS provider to be used by Pi-hole (see this list for typical providers)
DNS2 --DNS2 No 1.0.0.1 Alternative DNS provider to be used by Pi-hole
DATA_PATH --path No ./data Host data location path for Pi-hole, defaults to ./data
WEBPASSWORD -p, --password No password Password for the Pi-hole administrative web interface (prompted for when omitted)

Scheduled Tasks

Updating Pi-Hole to the Latest Version

It is recommended to schedule a task to ensure Pi-hole uses the latest version available. Follow these steps to do so.

  1. Access Task Scheduler via Control Panel ➡ Task Scheduler in DSM.
  2. Now click on Create ➡ Scheduled Task ➡ User-defined script to create a custom script. Give the task a familiar name in the tab General, such as Update Pi-hole container, and select root as user.
  3. Schedule the task in the tab Schedule, for example running it at 00:00 daily.
  4. Finally, enter the following script in the user-defined script section of the Task Settings tab. Be sure to update /path/to/your/script/. The optional instruction --log /var/log/syno_pihole.log copies all messages to a log file.
    /bin/sh /path/to/your/script/syno_pihole.sh update --ip 192.168.0.250 --log /var/log/syno_pihole.log

Ensuring the Host <-> Container Bridge Interface is Available After Reboot

By default, Docker containers are automatically restarted after a system reboot. However, the macvlan bridge interface setup by synology-pihole is lost after a system reboot and/or update. Similar to the instructions in the previous paragraph, you can setup a task to automatically recreate it during the boot process of your Synology NAS. Follow these steps to do so.

  1. Access Task Scheduler via Control Panel ➡ Task Scheduler in DSM.
  2. Now click on Create ➡ Triggered Task ➡ User-defined script to create a custom script. Give the task a familiar name in the tab General, such as Recreate Pi-hole Bridge Interface.
  3. In the same screen, select root as user and Boot-up as event.
  4. Finally, enter the following script in the user-defined script section of the Task Settings tab. Be sure to update /path/to/your/script/. The optional instruction --log /var/log/syno_pihole.log copies all messages to a log file. The option --force is required to avoid the script asking for user confirmation.
    /bin/sh /path/to/your/script/syno_pihole.sh network --ip 192.168.0.250 --log /var/log/syno_pihole.log --force

Configuration

The Pi-hole FAQ describes various options on how to configure the Pi-hole DNS server. The Pi-hole administrator web interface is available by navigating to http://ip_address/admin/ (replacing ip_address with the correct IP address).

Contributing

  1. Clone the repository and create a new branch
    git checkout https://github.com/markdumay/synology-pihole.git -b name_for_new_branch
  2. Make and test the changes
  3. Submit a Pull Request with a comprehensive description of the changes

Credits

Synology-pihole is inspired by the following code repositories and blog articles:

Donate

Buy Me A Coffee

License

Copyright © Mark Dumay

synology-pihole's People

Contributors

kieren avatar markdumay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

synology-pihole's Issues

how to attach nginx proxy manager network to pihole to do reverse proxy

networks:
macvlan:


nginx-proxy-manager_network:
external:
name: nginx-proxy-manager

networks:
macvlan:
ipv4_address: ${ipv4_address}
nginx-proxy-manager_network:

when i use this there is issue with port 53 is being used. please help how to attach nginx proxy manager in order to reverse proxy port 80 for using with ssl. thanks

Recreating Host <-> Container Bridge Interface after reboot ends in timeout

While trying to add this to my task scheduler

/bin/sh /path/to/your/script/syno_pihole.sh network --ip 192.168.0.250 --log /var/log/syno_pihole.log --force

ends with timeout. Obviously updated the path and tried executing it directly from the folder with sudo rights.

It outputs

Step 1 from 5: Testing Docker service availability
Testing... |^C

And eventually stops here. This is with the latest DSM DSM 7.2-64570 Update 1.

Workaround is i removed the check whether Docker service is available and the remaining steps run through without issues. Also the installation worked flawless.

Might be interesting, that i use bond0 as network parent (both eth0 and eth1 are being used). Not sure if that makes a difference.

Step 1 from 5: Validating DSM, Docker, and Docker Compose versions on host
Current DSM: 7.2
Current Docker: 20.10.23
Current Docker Compose: 2.9.0
Step 2 from 5: Testing network service availability
Testing... Network service detected
Step 3 from 5: Initializing network and Pi-hole settings
Interface: bond0
Subnet: 192.168.254.0/24
Gateway: 192.168.254.1
Host IP address: 192.168.254.28
VLAN: macvlan0
Pi-hole MAC address: d0:7a:c5:93:db:f4
Pi-hole IP address: 192.168.254.27
Docker network IP range: 192.168.254.27/32
Domain name: pihole.local
Hostname: pihole
Timezone: Europe/Luxembourg
Europe/Amsterdam
Europe/Brussels
DNS1: 1.1.1.1
DNS2: 1.0.0.1
Data path: /volume1/docker/synology-pihole/data
Web password: (not set)

Is there a better way by not assigning 2 ip address

Hello,

I have tried your solution, thanks for great work. It works fine.
I have question regarding each macvlan container will use 2 ip address from router when creating network link between container and synology(Host)
1st is for actual container and 2nd is to network traffic between host(synology) and container app.

Is there other way so we can have only 1 ip address on actually DHCP server(Router). as when i will have many macvlan containers then i need to maintain 2 ip address for each one.

Thank you for your help.

--log command line option is positional

If the --log option is used with syno_pihole.sh and a log file is not specified then this option must be specified last after all other commands, parameters and options. Otherwise a parsing error occurs when the next command line argument is blindly assumed to be the name of the log file.

For example: syno_pihole.sh -l update will fail with a 'no command' error and error messages logged to ./update

Update command requires IP to function

  • Existing instructions for scheduled update will timeout
    /bin/sh /path/to/your/script/syno_pihole.sh update -l /var/log/syno_pihole.log

  • They should specify
    /bin/sh /path/to/your/script/syno_pihole.sh update --ip 192.168.0.250 -l /var/log/syno_pihole.log

  • Additionally, the script should exit if --ip is not specified.

Error in step 4 and step 6

I receive the following error when running the script for install as sudo:

Step 4 from 8: Generating Docker Compose file
sed: -e expression #1, char 53: unterminated `s' command

However, it continues though to step 6:

Step 6 from 8: Creating Pi-hole container
The TIMEZONE variable is not set. Defaulting to a blank string.
Creating network "synology-pihole_macvlan" with driver "macvlan"
Creating pihole ... error

ERROR: for pihole Cannot start service pihole: b"Bind mount failed: '/volume1/homes/sam/synology-pihole/data/dnsmasq.d' does not exists"

ERROR: for pihole Cannot start service pihole: b"Bind mount failed: '/volume1/homes/sam/synology-pihole/data/dnsmasq.d' does not exists"
Encountered errors while bringing up the project.
ERROR: Could not create Docker network and/or container

At this point the script exits.

Incorrect version

The update script displays the incorrect version for the latest available package. Tested on DSM 7.1.

Install or Update Pi-hole as Docker container on Synology

Step 1 from 4: Validating DSM, Docker, and Docker Compose versions on host
Current DSM:               7.1
Current Docker:            20.10.3
Current Docker Compose:    1.28.5
Step 2 from 4: Detecting current and available Pi-hole versions
Current Pi-hole:           Unavailable
Target Pi-hole version:    76184753
76184753
76184753
2022.09.1
76184753
1998970
1998970
2022.09.1
2022.09.1
2022-09-02
20:37:10
2022-09-02
21:30:40
2022.09.1
2022.09.1
0d5a001916335
4b27408217063
9e04
7e0
2022.09
2022.09.1
5.14.1
2323
2324
76184753
Step 3 from 4: Creating Pi-hole container
Step 4 from 4: Testing Pi-hole availability
Testing...  Successfully connected to Pi-hole portal (http://192.168.1.250/admin/)
Done.

Boot error Pi-hole version 2022.01

The latest Docker version of Pi-hole fails to start correctly. The issue can be found here. The logs show the following error:

[cont-init.d] executing container initialization scripts...
[cont-init.d] 20-start.sh: executing... 
 ::: Starting docker specific checks & setup for docker pihole/pihole
Failed to set capabilities on file `/usr/bin/pihole-FTL' (Operation not supported)
The value of the capability argument is not permitted for a file. Or the file is not a regular (non-symlink) file
ERROR: Failed to set capabilities for pihole-FTL. Cannot run as non-root.
[cont-init.d] 20-start.sh: exited 1.

How to uninstall?

I'm moving my pi-hole from my synology-nas to a dedicated raspberry pi that I got. What steps do I need to do to uninstall synology-pihole from my nas? I removed the scheduled task and deleted the docker container, but I don't know what else the script does. Can documentation or a command for uninstall, be added?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.