< Back to all hacks

#47 SSH Config + Static IP

Network
Problem
Typing SSH commands with IP, port, key path every time is tedious and error-prone.
Solution
~/.ssh/config alias + static IP assigned on router DHCP. adb forward for USB phase.
Lesson
SSH config aliases eliminate repetitive typing and make multi-hop setups manageable.

Context

Accessing the Moto E2 via SSH requires specifying the host IP, port (8022 instead of default 22), identity file, and disabling strict host key checking (the phone's IP can change). The full command looks like:

ssh -i ~/.ssh/id_moto -p 8022 -o StrictHostKeyChecking=no 192.168.1.14

Typing this dozens of times a day during development is not sustainable. Additionally, the phone has two access modes: USB (via ADB port forwarding to localhost) and WiFi (via direct IP on the local network). Switching between them requires different commands.

Implementation

First, assign a static IP to the phone on your router's DHCP settings. This ensures the phone always gets the same IP address on your local network.

Create or edit ~/.ssh/config on your development machine:

# ~/.ssh/config — PocketClaw phone access

# WiFi access (phone on local network)
Host moto
  HostName 192.168.1.14
  Port 8022
  User root
  IdentityFile ~/.ssh/id_moto
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

# USB access (via ADB port forwarding)
Host moto-usb
  HostName localhost
  Port 8022
  User root
  IdentityFile ~/.ssh/id_moto
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

For USB access, set up ADB port forwarding first:

# Forward local port 8022 to phone port 8022
adb forward tcp:8022 tcp:8022

# Also forward gateway port for API access
adb forward tcp:9000 tcp:9000

Now connect with a single command:

# WiFi access:
ssh moto

# USB access:
ssh moto-usb

# SCP files to phone:
scp file.js moto:/data/data/com.termux/files/usr/tmp/

Verification

# Test WiFi connection:
ssh moto "echo 'WiFi OK'"
# Expected: WiFi OK

# Test USB connection (after adb forward):
ssh moto-usb "echo 'USB OK'"
# Expected: USB OK

# Verify port forwarding for gateway:
curl -s http://localhost:9000/api/status | head -c 50
# Expected: JSON response

Gotchas

  • On Windows/MSYS2, $PATH in double-quoted SSH commands expands to the Windows PATH. Always use single quotes for remote commands: ssh moto 'echo $PATH'
  • StrictHostKeyChecking no and /dev/null for known_hosts is acceptable here because it's a trusted local device. Do not use this pattern for production servers
  • If sshd dies on the phone (OOM), restart it via: adb shell "run-as com.termux sh -c 'export LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib; /data/data/com.termux/files/usr/bin/sshd 2>&1'"
  • Termux sshd listens on port 8022 by default (not 22) because non-root processes can't bind to ports below 1024

Result

MetricBeforeAfter
SSH command length~80 chars9 chars (ssh moto)
Access modesManual switchConfig aliases
Key managementPer-command flagAutomatic