Raspberry (or other SBC) setup for on the go operation


> Introduction

This section covers the use of a Raspberry Pi Zero W (or other SBC) running headless to provide a lightweight setup while on the go.

I assume you're familiar with setting up a Raspberry Pi / Raspbian and know some command line, nano editor, ... If not, you should be able to easily fill the blanks by DuckDuckGoing or StartPaging.

> Hardware

Raspberry Pi Zero W
External battery to power the RPi
Android phone

> Configuration

We'll configure an Android phone to be used as a way to access the RPi (via SSH) and the RPi to connect to its hotspot.

Android configuration

Install Termux which comes with a built-in ssh client.
Configure your Android hotspot (access point name and password).
While on the go, the Android phone's hotspot can be enabled anytime you want to connect to the Raspberry and disabled when not needed.

Raspberry configuration

Raspbian Lite
GNU Screen

First you need to enable the ssh service on the RPi.
This can be done either via raspi-config or by creating an empty file named "ssh" in the /boot partition of the RPi (reboot needed in this case).

Then the RPi has to be configured to auto-connect to the Android hotspot.
All you have to do is to set the access point name and passcode in the /etc/wpa_supplicant/wpa_supplicant.conf file.

network={
   ssid="accesspointname"
   psk="password"
}
Note: I usually use the same access point name and credentials for both my home AP and Android hotspot configurations.
By doing so, when coming back home with a running RPi in my backpack, it will automatically pick up an address on my home network.

> While on the go

Enable the hotspot on your Android phone and power up the RPi.
Wait for the connection to happen (see the tiny number next to the hotspot icon at the top of the screen) and check the RPi IP address in the hotspot menus (you should get the same IP for later connections).
Launch the Termux app and SSH to the RPi using the RPi IP address and user (pi in the default configuration):

ssh pi@192.168.xxx.xxx
Then start Screen

screen
The main reason for using Screeen is that you can detach Screen sessions (Ctrl+a d), in which processes will keep running, end the SSH session (Ctrl+d), and even stop the hotspot service on the phone when not needed to save some battery (assuming those processes don't need an Internet access).

If you later need to reconnect to the RPi, you just have to re-enable the hotspot service, wait a few seconds until the RPi reconnects, start Termux, SSH to the RPi and reattach the running Screen session.

ssh pi@192.168.xxx.xxx
screen -r

> Passwordless SSH authentication

Each time you connect to the RPi via ssh, you'll be asked for the pi user password.
You can avoid this by copying the ssh client's (ie: Termux) public key to the RPi.
First make sure the RPi is connected to you Android hotspot.
Then, from the Termux command prompt (and any other device you want to use to connect to the RPi), copy the key with this command.

ssh-copy-id pi@192.168.xxx.xxx
Going further, you can also disable ssh password authentication on the RPi from any other client.
SSH to the RPi, edit the /etc/ssh/sshd_config file.

ssh pi@192.168.xx.xx
sudo nano /etc/ssh/sshd_config
Uncomment (remove the # prefix) and set the following parameters.

PasswordAuthentication no
PermitEmptyPasswords no
Then save the changes and restart the ssh service on the RPi

sudo service ssh restart
From now on, only your Android Termux app (and clients whose key was copied to the RPi) will be allowed to SSH to the RPi.
Note: you can still login with a screen and keyboard or reverse the sshd config file changes if needed.
Note: keys copied via ssh-copy-id are stored on the RPi in .ssh/authorized_keys. This file also can be edited directly to add/remove public keys.