Table of Contents

https://lazyadmin.nl/it/installing-unifi-controller-on-a-raspberry-pi-in-5-min/

https://www.raspberrypi.org/documentation/installation/installing-images/linux.md

Find the SD Card Mount Point and Unmount It

If your computer has a slot for SD cards, insert the card. If not, insert the card into an SD card reader, then connect the reader to your computer.

The new device that has appeared is your SD card (you can also usually tell from the listed device size). The naming of the device will follow the format described in the next paragraph.

The left column of the results from the lsblk command gives the device name of your SD card and the names of any paritions on it (usually only one, but there may be several if the card was previously used). It will be listed as something like /dev/mmcblk0 or /dev/sdX (with partition names /dev/mmcblk0p1 or /dev/sdX1 respectively), where X is a lower-case letter indicating the device (eg. /dev/sdb1). The right column shows where the partitions have been mounted (if they haven't been, it will be blank).

If any partitions on the SD card have been mounted, unmount them all with umount, for example umount /dev/sdX1 (replace sdX1 with your SD card's device name, and change the number for any other partitions).

Copy The Image To The SD Card

In a terminal window, write the image to the card with the command below, making sure you replace the input file if= argument with the path to your .img file, and the /dev/sdX in the output file of= argument with the correct device name. This is very important, as you will lose all the data on the hard drive if you provide the wrong device name. Make sure the device name is the name of the whole SD card as described above, not just a partition. For example: sdd, not sdds1 or sddp1; mmcblk0, not mmcblk0p1.

dd bs=4M if=2018-04-18-raspbian-stretch.img of=/dev/sdX conv=fsync

Please note that block size set to 4M will work most of the time. If not, try 1M, although this will take considerably longer.

Run sync. This will ensure the write cache is flushed and that it is safe to unmount your SD card.

Remove the SD card from the card reader.

Setup Users & Enable SSH

sudo adduser username
sudo password username
sudo usermod -aG sudo username
sudo userdel -r pi
sudo passwd root
sudo systemctl enable ssh
sudo systemctl start ssh
sudo apt-get install vim

Setup Firewall and Network Interface

sudo vim /etc/network/firewall-rules

then add

*filter
# Flush any existing rules
-F
# Default policy: drop all inbound and allow all outbound
-P FORWARD DROP
-P INPUT DROP
-P OUTPUT ACCEPT
# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow local programs that use loopback (Unix sockets)
-A INPUT -s 127.0.0.0/8 -d 127.0.0.0/8 -i lo -j ACCEPT
# Allow ICMP pings
-A INPUT -i eth0 -p icmp --icmp-type echo-request -j ACCEPT
# Allow SSH from anywhere
-A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
# Allow all the UniFi ports from anywhere
-A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8081 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8443 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8880 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8843 -j ACCEPT
-A INPUT -i eth0 -p udp --dport 3478 -j ACCEPT
-A INPUT -i eth0 -p udp --dport 10001 -j ACCEPT
-A INPUT -i eth0 -p udp --dport 1900 -j ACCEPT
COMMIT
sudo vim /etc/network/interfaces

then put in something like

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
        address 172.21.21.145
        netmask 255.255.255.0
        gateway 172.21.21.1
        network 172.21.21.0
        broadcast 172.21.21.255
        post-up iptables-restore < /etc/network/firewall-rules
sudo vim /etc/resolv.conf

then put in something like

nameserver 8.8.8.8
nameserver 208.67.220.220
sudo vim /etc/hostname

Then put in the name you want

unifi.yourdomainname.com
sudo vim /etc/hosts

Find line 127.0.1.1 and change raspberrypi to your new host name:

172.0.1.1 unifi.yourdomainname.com

Now reboot.

Update & Install Unifi Controller

https://lazyadmin.nl/it/installing-unifi-controller-on-a-raspberry-pi-in-5-min/
https://help.ubnt.com/hc/en-us/articles/220066768-UniFi-How-to-Install-Update-via-APT-on-Debian-or-Ubuntu

sudo apt-get install rpi-update && echo Y | sudo rpi-update
sudo shutdown -r now
sudo apt-get update && sudo apt-get upgrade -y

Because we are using Raspbian Lite we need to make sure that the Oracle Java SDK is installed:

Note: in Raspbian Buster this needs to be openjdk-8-jdk

sudo apt-get -y install oracle-java8-jdk
sudo echo 'deb http://www.ubnt.com/downloads/unifi/debian stable ubiquiti' | sudo tee /etc/apt/sources.list.d/100-ubnt-unifi.list
sudo wget -O /etc/apt/trusted.gpg.d/unifi-repo.gpg https://dl.ubnt.com/unifi/unifi-repo.gpg
sudo apt-get update
sudo apt-get install unifi -y

Reboot

sudo shutdown -r now

After it comes back up go to https://your.ip.address:8443 and start the initial setup.