Scheduled maintenance: Community will be offline Monday June 17th, 1:00 AM - 6:00 AM (PT)
Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

[Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi Controller and Pi-hole from scratch (headless)

[ Edited ]

Greetings,

 

Thought I'd make a step-by-step tutorial for anyone looking for a straight forward installation of both the UniFi Controller (version 5.10.X or lower) and Pi-hole (Advertisement Blocking DNS server) without needing to hook up the Raspberry Pi to a monitor with keyboard and mouse (headless). You can install one without the other as well.


If you still have questions or if I forgot to explain some part you're stuck at, please post here so I can add this to the tutorial.

Dutch version: https://gathering.tweakers.net/forum/list_messages/1873125

 

0.  What you will physically need

 

- Raspberry Pi 3 model B(+) with a MicroSD card of atleast 4GB

- Ethernet cable to connect it to an existing network

- capability to connect the MicroSD card to a computer via a card reader

 

 

1.  Download the necessary tools and raspbian operating system image

 

1. Raspbian Stretch Lite (= without desktop interface) through the ZIP option (or torrent).

https://www.raspberrypi.org/downloads/raspbian/

Direct: https://downloads.raspberrypi.org/raspbian_lite_latest

 

2. Rufus to install the image on to the MicroSD card.

https://rufus.akeo.ie

Direct: https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5.exe

 

3. Advanced IP Scanner to figure out the assigned IP address of your Raspberry Pi.

http://www.advanced-ip-scanner.com

Direct: http://www.advanced-ip-scanner.com/download/Advanced_IP_Scanner_2.5.3850.exe

 

4. If you are not using Windows 10, you can use PuTTY instead of the command prompt to connect to your Raspberry Pi.

https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

Direct: https://the.earth.li/~sgtatham/putty/latest/w64/putty-64bit-0.71-installer.msi

 

5. If you are having issues with the partitions on your MicroSD card, I recommend using MiniTool Partition Wizard. This program makes it very easy to start from scratch by removing every partition on your MicroSD and creating a new Fat32 partition.
https://www.partitionwizard.com/free-partition-manager.html
Direct: https://www.partitionwizard.com/download/v11/pw11-free.exe

 

 

2.  Install Raspbian on the MicroSD

 

Open up Rufus to install Raspbian.

  1. Make sure your MicroSD card is selected

  2. Press SELECT and select the downloaded raspbian-stretch-lite.zip
    Tip: you don’t have to extract the .zip

  3. Press START

  4. Accept the warning about removing all of the data on the MicroSD

    Rufus.png

  5. wait until the process is done.

  6. Windows may ask you to format the Linux partition, do not do this, just cancel the request.

 

 

3.  Enable SSH

 

Enable SSH before starting the Raspberry Pi by adding a new text file to the main folder of the MicroSD card (boot partition).

Rename this text file to    ssh     and remove the .txt extension.

 

Image 1.png

 

The MicroSD is ready now, insert it into your raspberry pi, connect an ethernet cable and turn on the Raspberry Pi.

 


4.  Lookup the IP address

 

If you’re using Advanced IP Scanner, you can simply press SCAN and wait until the Raspberry Pi shows up.

 


5.  Login via SSH

 

1. Open up the command prompt (cmd in start of windows)
Tip: You should be able to paste commands by right clicking into the command prompt.

2. Login with the SSH command
Note: change the IP address with the IP address of your Raspberry Pi

ssh 192.168.1.2 -l pi

 

3. As we have not defined an SSH key, it will ask if we trust the host.

The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
ECDSA key fingerprint is SHA256:XXX.
Are you sure you want to continue connecting (yes/no)? 

yes

 

4. When typing in the password, you will not see characters being displayed, simply type it and enter

The default password is:

raspberry

 

 

Use the script to save time

 

I've added all the following commands into a simple script that I share through my GitHub project. By entering the line of code right after you login into your Raspberry Pi, you will not have to copy/paste all of these commands. You also get to choose your Stable version of the UniFi controller you wish to install.

If you wish to check the script before executing, you can check it out at:
https://github.com/SmokingCrop/UniFi/blob/master/install-unifi-pihole-English.sh


Copy/Paste this line of code to execute the script:

wget "https://github.com/SmokingCrop/UniFi/raw/master/install-unifi-pihole-English.sh" -O install-unifi-pihole.sh && chmod +x install-unifi-pihole.sh && ./install-unifi-pihole.sh

Or if you do not wish to install pi-hole, you can copy/paste this line of code to only install UniFi:
(It's the same script, but with an argument at the end to not install pihole)

wget "https://github.com/SmokingCrop/UniFi/raw/master/install-unifi-pihole-English.sh" -O install-unifi-pihole.sh && chmod +x install-unifi-pihole.sh && ./install-unifi-pihole.sh no-pihole

If you do not wish to use the script, just continue following this tutorial.

 

 

6.  Change the default password and Update all existing packages

 

It's very unsafe to keep the default password, so we'll change it.
After entering the passwd command, it'll ask you to type the current password once and your new password twice.

pi@raspberrypi:~ $ passwd
Changing password for pi.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

 

To make sure you have all the latest and greatest (firmware) updates and to clean up any unused/old ones, execute the following command:

 

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get autoremove && sudo apt-get autoclean

 


7.  Install Java 8

 

The default available Java version from Oracle is too old to make the 5.10.X UniFi controller work.
Instead of doing multiple extra commands to upgrade this version, we're just going to install OpenJDK.

 

sudo apt-get install openjdk-8-jre-headless -y

 

 

 8.  Install haveged

 

In order to fix the slow start-up of the UniFi controller, we have to install haveged.
The reason for the slow start-up is basically the lack of user interaction (no mouse movements) causing issues with the standard mechanisms for harvesting randomness. Luckily the fix is an easy one:

 

sudo apt-get install haveged -y

 

 

9.  Install the UniFi Controller

 

I'll show two ways of installing the UniFi Controller.


Method A: manually installing the Controller by using the download link on the website/update blog.

  • Easily install any Controller version you want, whenever you want. As soon as the blog comes out.
  • If you're not using the script, it'll take more commands to update your Controller in the future

Method B: Setting up the repository so you can install and update with the apt-get command.

  • Once it is setup, you can install/update the Controller like any other program with apt-get.
  • When a new Controller version is out, it takes like a week before you'll be able to install it.
  • It's recommended to still go the blog of the update to know if any changes will affect your setup!

 

 

9.1  Install the UniFi Controller with method A

 

You can go to the (beta) updates blog and copy the Debian download link at the bottom of the posts (UniFi Controller for Debian/Ubuntu) if you want a different version than the one used in this tutorial.
https://community.ubnt.com/t5/UniFi-Updates-Blog/bg-p/Blog_UniFi

https://community.ubnt.com/t5/UniFi-Beta-Blog/bg-p/Blog_UniFi_Beta (you need beta access)


Download UniFi.png 

1. Use the link with the wget command to download the Controller installation package.

wget https://dl.ubnt.com/unifi/5.10.21/unifi_sysvinit_all.deb

 

2. Install UniFi and its dependencies.

sudo dpkg -i unifi_sysvinit_all.deb; sudo apt-get install -f -y

 

Go to your UniFi Controller via the IP address and port:

E.g: https://192.168.1.2:8443

 

You’ll want to set a reserved IP in the UniFi Controller for the Raspberry Pi.

 

 

 

9.2  Install the UniFi Controller with method B

 

1. Add a new source to the list of sources from which packages can be obtained with 'apt'

echo 'deb http://www.ubnt.com/downloads/unifi/debian stable ubiquiti' | sudo tee /etc/apt/sources.list.d/100-ubnt-unifi.list

 

2. Add the GPG keys

sudo wget -O /etc/apt/trusted.gpg.d/unifi-repo.gpg https://dl.ubnt.com/unifi/unifi-repo.gpg

 

3. Update the package list and install UniFi

sudo apt-get update; sudo apt-get install unifi -y


Go to your UniFi Controller via the IP address and port:

E.g: https://192.168.1.2:8443

You’ll want to set a reserved IP in the UniFi Controller for the Raspberry Pi.

 

 

 

10.  Install Pi-hole

Execute the following command and configure Pi-hole

 

curl -sSL https://install.pi-hole.net | bash
> Choose an interface:
(*) eth0  (= ethernet cable)

> Choose your DNS provider
Custom
1.1.1.1, 8.8.8.8
(= fast cloudflare dns & basic Google dns) > Choose your adblocking providers
leave them enabled > IPv4 and/or IPv6
both > PiHole wants to set a static IP. Let it do its thing. I guess they haven’t heard of DHCP reservation/central management yet. > Install the Web admin interface and the web server

Change the password used to log into the web admin interface

 

pi@raspberrypi:~ $ pihole -a -p
Enter New Password (Blank for no password):
Confirm Password:
  [✓] New password set

 

Go to your Pi Hole web admin interface via the IP address and /admin:

E.g: http://192.168.1.2/admin

 

 

11.  Updating the UniFi Controller and Pi-hole with future updates

 

11.1  Updating the UniFi Controller with method A

 

I've also made a simple script for updating your system, Controller and Pihole in one go.
Simply enter ./update.sh in the future to use the script again without having to redownload it.

 

wget "https://github.com/SmokingCrop/UniFi/raw/master/update-unifi-pihole-English.sh" -O update.sh && chmod +x update.sh && ./update.sh

or keep following the tutorial:

 

In order to update the UniFi Controller manually you can use the following commands:

 

1. Remove the old downloaded UniFi Controller package

 

rm unifi_sysvinit_all.deb

 

2. Download the new UniFi Controller package, adjust the link below for the version you want to install.

 

wget http://dl.ubnt.com/unifi/x.x.xx/unifi_sysvinit_all.deb


3. Upgrade the UniFi Controller.

 

sudo dpkg -i unifi_sysvinit_all.deb

 

 

 

11.2  Updating the UniFi Controller with method B

 

Updating is easy but it may not be available right away, in that case, you will need to wait a few days longer before you can update (usually about a week after they have released the blog).

1. Simply execute the following commands. This will also upgrade your other programs.

 

sudo apt-get update; sudo apt-get upgrade -y

 


11.3  Updating Pihole

It's very easy to update Pihole with the following command:

 

pihole -up

 

 

New Member
Posts: 2
Registered: ‎01-19-2017
Kudos: 2

Re: [Step-By-Step Tutorial] Raspberry Pi with UniFi controller and Pi-hole from scratch (headless)

Great Tutorial! Thanks!
If I may suggest an improvement; it just lacks the explanation on how/where to change the settings in the Unifi controller in order to force all clients to use Pi-Hole.
PS: It is fortunately quite easy to find; I used this one. Of the two options I gave all network devices the IP address of my Raspberri Pi as DNS Server.

Emerging Member
Posts: 88
Registered: ‎01-02-2018
Kudos: 13
Solutions: 1

Re: [Step-By-Step Tutorial] Raspberry Pi with UniFi controller and Pi-hole from scratch (headless)

Awesome job man! Thanks for putting this together! This will help a lot of people!

Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

Re: [Step-By-Step Tutorial] Raspberry Pi with UniFi controller and Pi-hole from scratch (headless)

I've added an install-script and an upgrade-script to the tutorial for people who do not wish to copy/paste all the commands.
I've tested both and they worked flawlessly on my Pi. Thumbsup


All you'll have to do with the script is:
1. copy/paste the line of commands to download and run the script
2. enter a new password for your raspberry pi
3. enter the (stable) unifi controller version you wish to install
4. press enter a couple of times for the initial configuration of pi-hole
5. enter a new password for the pi-hole web interface

 

Check the install script here on Onedrive: https://1drv.ms/u/s!AtK94tegQTvMiu92g9EVSXSQz6SJrQ

Check the upgrade script here on Onedrive: https://1drv.ms/u/s!AtK94tegQTvMiu8giMwtcnKuh9nYrg

Highlighted
New Member
Posts: 11
Registered: ‎06-26-2017
Kudos: 1
Solutions: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

Really nice guide! Thank you. Worked as a charm

New Member
Posts: 1
Registered: ‎07-25-2016
Kudos: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

Great man make things simple!

I'm new to linux, great I found this post.

New Member
Posts: 2
Registered: ‎01-19-2018
Kudos: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

This worked like a charm.

 

I have one question however.

 

I set up the controller with a backup and now it doesn't work with "Cloud access".

Any idea what I need to do?

 

It only says "connecting" but nothing happens and I can't see it at "unifi.ubnt.com" when I log in.

 

 

New Member
Posts: 11
Registered: ‎07-20-2018
Kudos: 2

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

Wow!

 

This is great! Thank you very much!

 

I've set this up and it works like a charm.

 

I have one question: when I want to make a reboot or shutdown the combination, what is the best and safest way?

 

Should I log in with ssh and give a command reboot or halt?

 

Thanks.

 

New Member
Posts: 1
Registered: ‎12-02-2018
Kudos: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@johlun wrote:

This worked like a charm.

 

I have one question however.

 

I set up the controller with a backup and now it doesn't work with "Cloud access".

Any idea what I need to do?

 

It only says "connecting" but nothing happens and I can't see it at "unifi.ubnt.com" when I log in.

 

 


This happened to me too. I realized that the controller I installed the backup from was still running on my computer.

 

After closing the old controller I was able to access the new controller on my Raspberry Pi via cloud access with no problems.

New Member
Posts: 1
Registered: ‎06-26-2014
Kudos: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

Great guide, thanks!

 

After instalation completed, I am unable to access the Unifi controller.. How do I access this?  I also wonder how I access remotely.  Sorry, noob question.  And VERY new to linux.

 

Thanks in advance!!

Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

[ Edited ]

"Should I log in with ssh and give a command reboot or halt?"

 

I usually just login with ssh and "sudo reboot now" and both the controller & Pi hole will start up again without problems.

 

 

"After instalation completed, I am unable to access the Unifi controller.. How do I access this?  I also wonder how I access remotely.  Sorry, noob question.  And VERY new to linux.

 

Thanks in advance!!"


You should be able to go to the IP address with the port 8443 in your web browser.
like: https://192.168.0.2:8443


If you want to access it from outside the network, you could setup cloud access.
https://help.ubnt.com/hc/en-us/articles/115012240067-UniFi-How-to-Enable-Cloud-Access-for-Remote-Man...

 

or setup a port forwarding rule in your USG via the controller.
You'll have to port-forward the 8443 port to the IP address of the controller.

Image 1.png

 

After that you'll need to know your WAN IP address.
You can easily find this by using a website like this one:
https://whatismyipaddress.com/

You can then use this IP address to access your controller.
example: https://123.123.123.123:8443
if you changed the "Port" to something else like: 20000 then you'll have to do: https://123.123.123.123:20000

 

If you have a dynamic WAN IP address like most people do (which means it can change), you should probably setup a DDNS.
You can set this up in the settings of the controller -> services -> Dynamic DNS.
You'll need an account on a website like "afraid": https://freedns.afraid.org/dynamic/

 Image 4.png

Image 3.png

 

When the DDNS is setup, you can go to your controller by doing:

https://hostname.domain: port

example: https://your-hostname-you-have-chosen-on-afraid.uk.to:20000

 

This will work because everytime your WAN IP changes, the "your-hostname-you-have-chosen-on-afraid.uk.to" will change to the new IP address.

New Member
Posts: 3
Registered: ‎06-14-2018

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

Has anyone tried the Pi with the PoE module?

Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@itskv wrote:

Has anyone tried the Pi with the PoE module?


No, but It should work fine. There was an initial problem with it but they've recalled those bad ones.

New Member
Posts: 1
Registered: ‎08-25-2017

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

awesome, however after following the instructions the server does not  work. checking the log file I see a bunch of java errors:

[2018-12-19 00:31:07,670] <UniFi> ERROR system - [exec] error, rc=141, cmdline=[/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt
/jre/bin/java, -Dfile.encoding=UTF-8, -Djava.awt.headless=true, -Dapple.awt.UIElement=true, -Xmx1024M, -XX:+ExitOnOutOfM
emoryError, -XX:+CrashOnOutOfMemoryError, -XX:ErrorFile=/usr/lib/unifi/logs/hs_err_pid%p.log, -jar, /usr/lib/unifi/lib/a
ce.jar, start]

any ideas please?

New Member
Posts: 3
Registered: ‎06-14-2018

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

I just installed the PoE hat, and works perfect!

Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@mlasham wrote:

awesome, however after following the instructions the server does not  work. checking the log file I see a bunch of java errors:

[2018-12-19 00:31:07,670] <UniFi> ERROR system - [exec] error, rc=141, cmdline=[/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt
/jre/bin/java, -Dfile.encoding=UTF-8, -Djava.awt.headless=true, -Dapple.awt.UIElement=true, -Xmx1024M, -XX:+ExitOnOutOfM
emoryError, -XX:+CrashOnOutOfMemoryError, -XX:ErrorFile=/usr/lib/unifi/logs/hs_err_pid%p.log, -jar, /usr/lib/unifi/lib/a
ce.jar, start]

any ideas please?


Hmm, honestly don't really know why a Java error would occur.
Haven't had an error yet after installing it a couple of times over the months with different versions etc.
Maybe try a clean install again, maybe some bug? I don't know.

New Member
Posts: 11
Registered: ‎07-20-2018
Kudos: 2

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@SmokingCrop wrote:


Updating Pi-hole is very simple

pihole -up

 

 


I confirm that this works as a charm, too. The new version of Pi-Hole was just released and I had no problems updating it with this command. It was done in less than a minute.

 

Thank you again, this Controller + Pi-Hole are a super couple!

 

New Member
Posts: 2
Registered: ‎01-19-2018
Kudos: 1

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@itskv wrote:

Has anyone tried the Pi with the PoE module?


Yes, I've been running it for about a week now and it work perfectly. One less cord Man Happy

New Member
Posts: 11
Registered: ‎07-20-2018
Kudos: 2

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl


@johlun wrote:

@itskv wrote:

Has anyone tried the Pi with the PoE module?


Yes, I've been running it for about a week now and it work perfectly. One less cord Man Happy


Is it 24V or 48V?

 

Edgerouter X could push 24V from it's port 5 I guess?

 

 

Emerging Member
Posts: 77
Registered: ‎05-22-2016
Kudos: 101

Re: [Step-By-Step Tutorial/Guide] Raspberry Pi with UniFi controller and Pi-hole from scratch (headl

[ Edited ]

@ramsesii wrote:

@johlun wrote:

@itskv wrote:

Has anyone tried the Pi with the PoE module?


Yes, I've been running it for about a week now and it work perfectly. One less cord Man Happy


Is it 24V or 48V?

 

Edgerouter X could push 24V from it's port 5 I guess?

 

 


You need 802.3af PoE / 37–57V DC according to the specs. So I don't think passive 24V PoE will do the trick.

 

https://www.raspberrypi.org/products/poe-hat/

"This product:

  • Shall only be powered using 802.3af-compliant power sourcing equipment;"

https://help.ubnt.com/hc/en-us/articles/115000263008--UniFi-Understanding-PoE-and-How-UniFi-Devices-...