My previous post of setting up a Raspberry-Pi with HA, HB, Siri got a massive amount of hits and I had a few emails, as tbh, the post was more for me than general public so I’m going to write it again, in a multi-blog entry. First of all setting up a Pi to run without a monitor, headless we call it.
Firstly I will state that all my work is done with a Macbook Pro, you can obviously, 100% use Windows, but for SSH you’ll have to download Putty or some other client, I won’t go into Putty usage here, it is however very intuitive to use. The only real difference between using a terminal on a Mac and Putty is the way you connect, in Putty you have a connection tab, enter the details in that, IP, username, password, in a terminal shell you simply use one line of text.
I’m using a Raspberry Pi 3, as want a few things to run on it at once. I have heard that Alexa works fine on a 2, but your mileage may vary.
Initial setting up of a Raspberry Pi in headless mode
Download Rasbian Jessie from:
https://www.raspberrypi.org/downloads/raspbian/
I chose Jessie with Pixel as on occasion I use VNC to connect to the desktop, it can be useful. Jessie is a great product, massive props to the developers.
Once downloaded extract the iso to somewhere useful like Download folder.
Now we need to prepare the Micro-SD card for the image to be written to.
Insert your Micro-SD card (at least 8gb) into your mac card reader via an adapter. Use Disk utility to erase and partition the disc as Fat. Check “About this Mac” to find what disc number it is by clicking on “System Report” and “Card Reader” under the hardware list. Look for BSD Name, in my case disk2.
Go back to disk utility and unmount the disk, don’t eject it.
Open terminal (cmd-space type “terminal” if you’ve no idea what the terminal is) and type:
sudo dd bs=1m if=/Users/Puk/Downloads/2017-03-02-raspbian-jessie.img of=/dev/rdisk2
(where the /Users/Puk/Downloads is the dir of your image)
Let it write the image, there will be no output for probably five/ten minutes.
Here’s Windows installation guide, here’s a Mac installation guide too if you didn’t follow mine.
Allowing SSH access to the Pi
As of Dec 2016 the Rasbian people decided to turn off SSH as default, which is an arse. So we can’t SSH into the pi without making it SSH friendly. Now thankfully this is very easy. If you don’t you’ll receive an error like:
ssh: connect to host 192.168.0.10 port 22: Connection refused
With the SD card still in your mac, navigate via the file manager to it. You need to make a file called ssh in the boot dir. I did this just by copying and pasting an existing text file, deleting it’s contents, then renaming it to ssh without a file extension. Without this, you can’t SSH into the Pi, which means you’ll be buggered unless you plug a monitor and keyboard into it.
Powering up the Pi and finding it’s IP
Eject the disk via disk utility and insert into the pi and plug in Ethernet or a wifi dongle, plug in power via the micro usb port and it’ll boot. You’ll have a few flashing lights, and that’s it, but to all intents and purposes it’s on and working!
Now we need to find the pi, so log into your router with a web browser by typing 192.168.0.1 in your browser window. You’ll usually find the login details on a sticker either under or on the back the router itself, it’s probable the username is admin and password admin. You should change this if you want tighter security and open your pi to the outside world later on.
Look for something like connected devices, or list devices and determine which is your pi, in my case since I’m using an ethernet cable for speed it’s very easy.
Access the Pi with SSH
Since you’re using a Mac, this is easy. As i stated earlier, this is Putty time for Windows users.
Open a terminal window and type:
ssh pi@192.168.0.10
Where the ip address is the one you found from using the router config in the previous step. The syntax for this is, command (ssh) username(pi)@ipaddress(192.168.0.10).
The default password is:
raspberry
Hurray you’re in!
Time to setup the Pi
To enter the RaspberryPi’s setup, you simply type:
raspi-config
I setup mine as follows (numbers relate to options on the config):
1) Change the password to something more secure than raspberry.
3) Boot Options, select boot Desktop/CLI – B1 Console (no need to boot graphically if we’re using headless).
4) Localisation options – Timezone, select your country then city.
5) Interfacing options, turn off Camera, VNC server (you can enable later as req, turn it off when it’s not used to save memory)
6) Advanced Options – Expand file system (so the Pi uses the whole SD card), Memory Split, choose 16 (let the Pi use the memory, it doesn’t need to let the GPU use any as it’s headless). Audio, force to 3.5mm jack, we’re not plugging in the HDMI port.
Finish and reboot. Use your new password when you re-connect SSH into it.
Setting up a static IP
I want my Pi to have one (static) ip, not change on my network, now this kinda contradicts a lot of what network admins feel, but since my network only has about twenty devices tops on it, I feel it’s justified, seeing as I want to forward ports and all sorts to my pi later on, continuity of a static ip makes a lot of sense.
You can do this if you’re using Pi-Hole as per my instructions in the next post, or incase you’re not using Pi-Hole then you can do it here.
In all my posts you’ll see I use Nano for my shell text editor, it’s by far the simplest terminal editor available imo, vi is superb, but it’s just not new user friendly.
So type into your SSH terminal:
sudo nano /etc/dhcpcd.conf
Scroll to the bottom and add either:
interface eth0
static ip_address=192.168.0.24/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
interface wlan0
static ip_address=192.168.0.24/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
My choice of ip is 192.168.0.24, yours could be anything else.
Depending on whether you’re using Ethernet (eth0) or wireless(wlan0), I’m using the static ip address of 192.168.0.24, make sure you leave the /24 on the end, I’m using ethernet so my config is:
interface eth0
static ip_address=192.168.0.24/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1
Paste that code in. Press ctrl-x to quit, then Y to save and overwrite the file.
Reboot the pi with:
sudo reboot
Now SSH into your new ip address:
ssh pi@192.168.0.24
This next step is for me, as I have used the same IP address this time as my previous Pi, i need to delete the host file as I get an error of: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
So i need to update my Mac’s host file. I do this by:
ssh-keygen -R 192.168.0.24
Obviously that’s my Pi’s IP address, if you get the above you’ll have to alter the IP to the one you’re having problems with. Now I can access my Pi again.
Making the Rasbian installation smaller
There’s lots of awesome stuff on a Rasbian image, but this is headless and I’m only using a 8gb SD card, so want to free up some space.
First check to see how big it is, type into your SSH terminal:
df - h
So at standard my install is 3.6GB large. Time to slim it down by uninstalling a few things i’ll never use, do this by removing unwanted packages: libreoffice, wolfram, sonic-pi, scratch, minecraft-pi, dillo, gpicview, mathematica.
sudo apt-get purge wolfram-engine
(then again and again with libreoffice…)
sudo apt-get clean
sudo apt-get autoremove
Then check again with df –h.
So I’ve saved 1GB of space! Happy days! Also, yes i changed the colours of my terminal as was working at night.
Right, time to update the pi.
sudo apt-get update
sudo apt-get upgrade
So you’re Raspberry-Pi is now running in a headless mode, with fixed IP, slimmed down install and ready to be your workhorse!
Time to move onto my next blog post, setting up Pi-Hole and Home-Assistant.