How to setup a VPN and a DNS sinkhole on a Debian server, using Wireguard, Pi-hole and Docker.
These instructions are meant for a Debian server and assume that you are using Linux or macOS. Take some time to read the documentation as well as the scripts before you run anything on your server.
- SSH into the server using
ssh root@serverip
and runapt-get update && apt-get upgrade
. - Create a new user with
useradd -m username
and set a password for this user withpasswd username
. - Add the newly created user to the
sudo
group withadduser username sudo
. - Use
su username
to connect as the user you have just created. - By default Debian does not use
bash
, which means you won't have tab completion or syntax colouring. To remedy that, usechsh -s /bin/bash
then log out and log back in withexit
and thensu username
. - In you home directory create a
.ssh
folder as well as a file namedauthorized_keys
withmkdir ~/.ssh && touch ~/.ssh/authorized_keys
.
- On your computer, go into you
.ssh
folder withcd ~/.ssh
. If the folder does not exist create it usingmkdir ~/.ssh
. - Use
ssh-keygen -t rsa -b 4096
to generate an SSH key pair. It is recommended that you name the key so that you can keep track of them. - You need to copy the public key that you have just created to your server. To do that use
scp yourkey.pub username@serverip:
. Do not forget the:
at the end. - To be able to connect to the server using your SSH key, you need to add it to the
authorized_keys
file usingcat yourkey.pub >> .ssh/authorized_keys
. You can then delete the public key from the server usingrm yourkey.pub
. - Make a copy of the
sshd_config
withsudo cp /etc/ssh/sshd_config /root/
. - Edit the content of the SSH daemon configuration using
sudo nano /etc/ssh/sshd_config
. - A configuration example can be found in wg-pihole-docker/example-sshd_config. You might want to change the default SSH port.
- Once you have made changes in the
sshd_config
file, restart the daemon usingsudo systemctl restart sshd
. If you have changed the SSH port, make sure to change the firewall rules accordingly before you log off. - From now on, in order to log back into your server you will have to use the following command
ssh -2 -i ~/.ssh/yourkey username@serverip -p portnumber
.
- Install git with
sudo apt-get install git
- Clone this repository with
git clone https://github.com/foobarberis/wg-pihole-docker.git
- Read, make changes if needed and then run the firewall script with
sudo bash firewall-config.sh
. Note that this script will erase alliptables
rules and chains and replace them. If you run this script after the installation of Docker, you will need to runservice docker restart
in order to re-install the rules and chains Docker needs in order to run properly. - By default,
iptables
rules are reset after a reboot. In order to restore them automatically you will need to install the packageiptables-persistent
withsudo apt-get install iptables-persistent
. The installer will ask you if you want to save your current IPv4 and IPv6 rules, selectYes
for both. If you were to make changes to the rules and want to save them again, usesudo iptables-save > /etc/iptables/rules.v4
and/or orsudo ip6tables-save > /etc/iptables/rules.v6
. You can also usesudo netfilter-persistent save
to save both files at once andsudo netfilter-persistent reload
to restore back to how they were last time you saved them.
The following instruction are taken from https://docs.docker.com/engine/install/debian/, go to this page for more details.
- Update the apt package index and install packages to allow apt to use a repository over HTTPS
sudo apt-get update && sudo apt-get install ca-certificates curl gnupg lsb-release
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Use the following command to set up the stable repository:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update the apt package index, and install the latest version of Docker Engine and containerd:
sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io
At this point the Docker Engine should be up and running, you can check if docker.service is running with systemctl --type=service
.
The following instruction are taken from https://docs.docker.com/compose/install/, go to this page for more details.
- Run this command to download the current stable release of Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
- Test the installation:
docker-compose --version
Modify docker-compose.yml to add your server's static IP and your passwords for the Wireguard and the Pi-hole WebUIs. Then, from inside the wg-pihole-docker
directory, run sudo docker-compose up -d
. You should now be able to access the Pi-hole WebUI at http://yourserverip/admin
or at http://pi.hole/admin
and the Wireguard WebUI at http://yourserverip:51821
.
In order to upgrade, just stop and delete the container you want to upgrade using sudo docker stop container_name
and sudo docker rm container_name
then run sudo docker-compose up -d
.
By default, Pi-hole uses Steven Black's hosts files. To manage the adlists, on Pi-hole's admin page you can go to Group Management > Adlists
then add or remove adlists as you see fit. Once you are done, go to Tools > Update Gravity
and click on Update
to apply the changes.