Raspberry Pi Headless Setup

Finding a set of USB keyboard, USB mouse, HDMI monitor, Wired Internet connection just to set up a new raspberry pi is tedious. Why not just setup it up using WiFi connection and power supply only? I did find some guide from the raspberry official website, but it lacks some detail, so I googled it and curated all the info needed here.

What You Need

  • power supply through micro usb, 5A, at least 1A (official recommendation is 2.5 A, I used a spare 1A phone charger, worked well)
  • WiFi connection
  • micro SD card, class 6 or above, 8GB or above

P.S.

what is micro usb:

micro USB

what is micro SD car:

micro SD

rasbperry pi 3b(+) layout:

rasberry 3b

Headless Setup

  1. Follow the official document to download and install the image RASPBIAN STRETCH LITE image should be fine
  2. create a file named ssh on the boot partition of your SD card to enable ssh only at the first bootup (you have to enable ssh when you get into the system or you won’t be able to use ssh after reboot)
  3. create a file named wpa_supplicant.conf on the boot partition to enable WiFi connection (will be moved to the proper location when booting up)
# /etc/wpa_supplicant/wpa_supplicant.conf
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NETWORK-NAME"
    psk="NETWORK-PASSWORD"
}
  1. turn ssh on after your first loggin
    sudo systemctl start ssh
    sudo systemctl enable ssh
    

SSH to Your Pi

ssh pi@raspberrypi.local

Mordern raspberrypi has a native support of mDNS(broadcasting one’s self’s hostname to all other hosts without a central DNS server.) On macOS you can ssh to raspberrypi.local directly. On Windows you have to configure the mDNS service by installing Apple Bonjour or using other method.

SSH to Your LAN Device from the Internet

https://github.com/fatedier/frp#access-your-computer-in-lan-by-ssh

  1. configure the server: Modify frps.ini:
    # frps.ini
    [common]
    bind_port = 7000
    

Start frps:

./frps -c ./frps.ini

Auto Start frps When Booting Up using Upstart

# /etc/init/frps.conf
description "frps daemon"
# start in normal runlevels when disks are mounted and networking is available
start on runlevel [2345]
# stop on shutdown/halt, single-user mode and reboot
stop on runlevel [016]
exec /opt/frp/frps -c /opt/frp/frps.ini
sudo service frps start
sudo service frps status
sudo cat /var/log/upstart/frps.log
  1. configure the client Modify frpc.ini, server_addr is your frps’s server IP:
# frpc.ini
[common]
server_addr = [YOUR SERVER IP/DOMAIN NAME]
server_port = 7000

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

Start frpc:

./frpc -c ./frpc.ini

Auto Start frpc When Booting up Using Systemd

# pi@raspberrypi:/etc/systemd/system $ cat frpc.service
[Unit]
Description=frpc
Wants=network-online.target
After=network-online.target
StartLimitInterval=200
StartLimitBurst=5

[Service]
ExecStart=/opt/frp/frpc -c /opt/frp/frpc.ini
User=pi
Group=pi
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload sudo systemctl start frpc sudo systemctl enable frpc

  1. Connect to server in LAN by ssh assuming that username is test:
    ssh -p 6000 test@[YOUR SERVER IP/DOMAIN NAME]
    

Write to NTFS USB disk

sudo apt update
sudo apt install ntfs-3g
sudo fdisk -l
sudo umount /dev/sda1
mkdir /mnt/disk1
sudo mount -t ntfs-3g /dev/sda1 /mnt/disk1

Turn Your Pi to A Remote Download Device

sudo mkdir /opt/xware
sudo chown -R pi:pi /opt/xware
cd /opt/xware
wget https://github.com/kuoruan/Xware/raw/master/1.0.31/Xware1.0.31_armel_v5te_glibc.zip
unzip Xware1.0.31_armel_v5te_glibc.zip
sudo ./portal
# copy the activation code and paste it in `yuancheng.xunlei.com`
# mount your external USB disk, then you can start downloading using the web console
# to stop:
sudo ./portal -s

Access Pi’s Folder from Your Mac

# Install Netatalk on Raspberry PiA
sudo apt-get install netatalk
# Connnect to Pi from Mac using AFP
open afp://[LOCAL PI IP ADDRESS]

China Optimization

# /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
#deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
# sudo nano /etc/apt/sources.list.d/raspi.list
deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui
#deb-src http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui

Live Video Streaming with Raspberry Pi

TODO

References: