Skip to main content

HomeBridge Server on Raspberry Pi

What is HomeBridge and why should you use it?

HomeBridge is a utility that bridges all sorts of smart home devices into Apple’s HomeKit, so you can control them with Siri even if the manufacturer never bothered to support HomeKit directly. It’s worth running on something low-power that can stay on all the time — I used a Raspberry Pi 2, which runs about $37.70.

You’ll also want a 5V 2A micro USB power supply and a fast microSD card. I used a SanDisk Ultra 32GB microSD and had no performance complaints.

Ok I’m convinced. Now what?

Parts List

  • Raspberry Pi 2 Model B
  • SanDisk Ultra 32GB microSDHC
  • USB 3.0 card reader
  • Edimax EW-7811Un Wi-Fi USB adapter (optional)
  • Official Raspberry Pi case (optional)
  • RJ45 Ethernet patch cable (optional)

Setup

Grab Ubuntu from the official Raspberry Pi wiki and use Win32DiskImager to write it to the microSD card. Hook up a monitor, mouse, keyboard, and Ethernet to get started.

I have it booted up, what’s next?

Operating System Configuration

Resize the partition and update the system:

$ sudo fdisk /dev/mmcblk0
# Delete the second partition (d, 2), recreate it (n, p, 2, enter, enter), then write (w)
$ sudo resize2fs /dev/mmcblk0p2
$ sudo apt-get update --fix-missing

Install SSH and swap file support:

$ sudo apt-get install openssh-server
$ sudo apt-get install dphys-swapfile

Security Hardening

Change the default credentials:

$ sudo passwd root
$ sudo deluser --remove-home ubuntu
$ sudo adduser homebridge sudo

Lock down the firewall with iptables:

$ sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$ sudo iptables -A INPUT -i lo -j ACCEPT
$ sudo iptables -A INPUT -p tcp -s xxx.xxx.x.x/29 --dport 22 -j ACCEPT
$ sudo iptables -A INPUT -s 192.168.1.1/24 -j ACCEPT
$ sudo iptables -A INPUT -j DROP

Install iptables-persistent so the rules survive a reboot:

$ sudo apt-get install iptables-persistent
$ sudo invoke-rc.d iptables-persistent save

SSH Key Authentication

Generate an RSA key pair:

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ ssh-keygen -t rsa -b 4096
$ cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys

Edit /etc/ssh/sshd_config and update:

PermitRootLogin without-password
AuthorizedKeysFile %h/.ssh/authorized_keys
PasswordAuthentication no

Then restart SSH:

$ sudo service ssh restart

HomeBridge Installation

Switch to the homebridge user:

$ su homebridge

Install Node.js and npm

$ wget https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-armv7l.tar.gz
$ tar -xvf node-v6.2.1-linux-armv7l.tar.gz
$ cd node-v6.2.1-linux-armv7l
$ sudo cp -R * /usr/local/

Install HomeBridge

$ sudo npm install -g homebridge
$ homebridge

Create a config file from the sample and fill in a MAC-address-style username and a unique PIN:

$ sudo nano .homebridge/config-sample.json
$ cp /homebridge/.homebridge/config-sample.json /homebridge/.homebridge/config.json

Install Plugins

$ sudo npm install -g homebridge-sonos
$ sudo npm install -g homebridge-wink
$ sudo npm install -g homebridge-platform-wemo
$ sudo npm install -g homebridge-nest

Follow each plugin’s own docs for device-specific configuration.

Run HomeBridge at Startup

Option 1: screen

$ sudo apt-get install screen
$ nano /etc/rc.local

Add this line before exit 0:

/usr/bin/screen -d -m -U -S homebridge /usr/bin/homebridge

Option 2: an init.d service

Create /etc/init.d/homebridge from an init.d template, setting:

dir="/home/homebridge"
cmd="sudo /usr/bin/homebridge"
user="homebridge"

Then:

$ sudo chmod 755 /etc/init.d/homebridge
$ sudo update-rc.d homebridge defaults

Grant the homebridge user passwordless sudo for the commands it needs, via sudo visudo:

homebridge ALL=(ALL) NOPASSWD: /etc/init.d/homebridge
homebridge ALL=(ALL) NOPASSWD: /usr/bin/homebridge
homebridge ALL=(ALL) NOPASSWD: /usr/local/bin/homebridge
homebridge ALL=(ALL) NOPASSWD: /usr/bin/npm

Start it:

$ sudo /etc/init.d/homebridge start

Conclusion

That’s the whole setup. If you get stuck, the HomeBridge Slack community is a good place to ask around.