pauls page

for music, code and the internet of things.

The Headless Raspberry Pi

Starting a new IoT project?

Want to build your own discrete Linux server?

A “Headless” device basically means a device without a display. If you have a project that doesn’t really need an HDMI Display, Keyboard or Mouse – this is a lightweight way to get started with just a bare-bones Raspberry Pi and MicroSD card.

The instructions below will guide you to setup a RaspberryPi 2/3 or ZeroW without any peripherals and with remote access.

Get Ready

Download Raspbian OS

Get the image suitable for your needs. (With or without Desktop)
https://www.raspberrypi.org/downloads/raspbian/
https://www.raspberrypi.org/software/operating-systems/

While that’s happening lets prep your PC…

Install a good Text Editor

Further on we will need to edit some files. To make sure they are handled properly – use a good text editor like Atom or Notepad++.

Windows 10 – Linux Subsystem

The Raspbian (Linux) OS uses a different file system to Windows, to edit files on the SD Card using your windows machine, you’ll need to install the Linux Subsystem for Windows.

SSH Client

When setup we will connect to the Pi using SSH (Secure SHell), if you installed the Linux Subsystem above, you can use the ‘ssh’ command (e.g. ‘ssh user@raspberrypi.lan’) – otherwise Putty is the standard.

For transferring bulk files, I recommend WinSCP.

Build your SD Card

Write the Image to SD Card

Use Etcher to put the image on a MicroSD card.
https://www.balena.io/etcher/

When you insert the card in your PC you may be prompted to initialize the SD Card, do not do this.

Setup SSH for remote management

Enabling SSH is pretty easy, just create a file called “ssh” at the root level of the SD Card. This will let Raspbian know to start the SSH service (sshd) at boot.

Configure the WiFi

Create another file on the SD Card called “wpa_supplicant.conf”. Edit it with your wireless details as below – don’t forget, the Pi 2/3/0W only supports 2.4ghz b/g/n wifi.

If you’re going to be using Ethernet then this step isn’t absolutely necessary but it can serve as a backup connection. If you have both – Ethernet will take priority.

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="your_wifi_ssid"
    scan_ssid=1
    psk="your_wifi_password"
    key_mgmt=WPA-PSK
}

..that should do it! Eject the SD card and insert it in your Pi.

Boot the Pi and connect to it via SSH

Login: pi
Password: raspberry

Customise the install

Use the raspi-config tool to change your password and set boot and localisation options. You might like to enable VNC if you’re going to be using the desktop build of Raspbian.

It may be convenient to enable Auto-Login as well.

sudo raspi-config

Remote Desktop?

VNC – Using this method will allow you to remote control the session that is active on the Pi itself. Enable it with the config utility above. To connect to the pi use VNC Viewer client on your PC.

XRDP – RDP presents your xWindows environment in a NEW session. It’s not remote control, its a new session on the device, separate to the physical session.

This can be installed via console or SSH.

sudo apt-get install xrdp

Other Services…

Select which services run at startup

sudo apt-get install rcconf
sudo rcconf

Advanced Stuff…

Enable root login for SSH.

This is not really recommended, but it can be convenient to be logged in as root (especially when working inside ‘/var/www’ for permissions).

sudo nano /etc/ssh/sshd_config

...
PermitRootLogin yes

then restart the SSH Service.

systemctl restart sshd

Use screen and s-tui for monitoring

sudo apt-get install screen
sudo apt-get install stress
sudo pip install s-tui

…then via SSH start a new screen session and have s-tui output to console.

screen
s-tui > /dev/tty1

Now you can close the SSH session window and it will keep running.

To resume the session, SSH back in and…

screen -list
screen -r 1234.pts-0.raspberryetc...

Launch a script at Login

(and at Boot – if Auto-login is enabled)

Navigate to your home directory and create your script.

cd ~

touch myloginscript
chmod +x myloginscript

sudo nano myloginscript

It could be python if that’s your jam, but this example is a shell script…

#!/bin/bash
# /home/pi/myloginscript
if ping -q -c 1 -W 1 8.8.8.8 > /dev/null; then
  echo -e "\e[32m\n *** IPv4 is UP ***\n\e[25m"
else
  echo -e "\e[31m\n *** IPv4 is DOWN ***\n\e[25m"
fi

Add your script to the bottom of the .bashrc file in your home folder.

sudo nano .bashrc

...
/home/pi/myloginscript

 

…to be continued