Raspberry Pi Setup (Linux)

Setup SD Card on another linux machine to install headless

I use minimal Raspian distro. This is tested on a Pi Zero and a Pi 4 in Feb 2020. It's easier to

# find the sd card device
lsblk -p | grep -v loop
# mmcblk0 8:16   1  29.7G  0 disk 
# " mmcblk0  " will be different machine-to-machine
 # unmount your sd if necessary 
umount /dev/ mmcblk0p1   
umount /dev/ mmcblk0p2

# get it
wget https://downloads.raspberrypi.org/raspbian_lite_latest
unzip 2019-09-26-raspbian-buster-lite
# write it
sudo dd bs=4M if=2020-02-13-raspbian-buster-lite.img of=/dev/mmcblk0 conv=fsync status=progress
# don't fret... it takes a while: https://superuser.com/questions/1220867/dd-function-seems-to-freeze-when-done

# mount boot and root file system to /media/sdcard1,2
sudo mkdir /media/sdcard1
sudo mkdir /media/sdcard2
sudo mount /dev/mmcblk0p1 /media/sdcard1 # boot parition,
sudo mount /dev/mmcblk0p2 /media/sdcard2 # root

# wifi settings
sudo cp wpa_supplicant.conf /media/sdcard1/
# vim /media/sdcard1/wpa_supplicant.conf # edit using use format here

# add ssh
sudo touch /media/sdcard1/ssh
mkdir /media/sdcard2/home/pi/.ssh/
cp ~/.ssh/authorized_keys /media/sdcard2/home/pi/.ssh/ # ssh *to* it
# optional
cp ~/.ssh/id_rsa /media/sdcard2/home/pi/.ssh/ # if you want to ssh *from* it
cp ~/.bashrc /media/sdcard2/home/pi/ 
cp -r ~/.aws /media/sdcard2/home/pi/ 

# finish on linux pc
sudo umount /dev/mmcblk0p1
sudo umount /dev/mmcblk0p2

# boot it up 

# from linux box,
# Optionally, remove old host keys
ssh-keygen -f "/home/bbeeson/.ssh/known_hosts" -R "raspberrypi.local"
# ssh and go!
ssh pi@raspberrypi.local

Headed Install

sudo raspi-config
  • timezone, locale, keyboard
  • wifi
  • camera, ssh
  • password


sudo apt update && sudo apt dist-upgrade -y
sudo apt install vim git

Beyond Basics

  • RaspAP makes access and WiFi easier
  • autossh (apt install autossh) and and autossh.service
  • persistent logging is helpful:
sudo  mkdir -p /var/log/journal
sudo systemd-tmpfiles --create --prefix /var/log/journal
# test after a reboot
journalctl -b -1

Talking Nice to Windows

  • Samba is easy to install to get file sharing. See various tutorials. Might have to open the firewall:
# install 
sudo apt install smb
# configure
sudo vim /etc/samba/smb.conf
    path = /var/path/
    read only = yes
    public = yes
    writable = no
# and restart
sudo systemctl restart smbd

# ufw firewall
sudo ufw allow proto udp to any port 137 from
sudo ufw allow proto udp to any port 138 from
sudo ufw allow proto tcp to any port 139 from
sudo ufw allow proto tcp to any port 445 from
  • mDNS / Bonjour is a bit of a pain. I hacked through various changes. Try the following in order
    • Pray
    • In HyperV, use a "External Network" to let the VM connect to your real LAN. The default is a VM-Host network with the Host acting as a Nat Gateway
    • In Windows disable IPv6 (via adapter settings)
    • In Windows install Bonjour V2.
    • Maybe? fuck 'round with avahi-daemon.conf on the linux side (no ipv6, etc).
    • Maybe? install Samba
    • Maybe? add wins to /etc/nsswitch.conf

Backup and Restore SD Cards using linux

Once a Pi is setup, it's useful to clone the SD card and save the state. Here's how.

# insert sd, copy whole (incl. partitions) to a file. Will be the SIZE OF THE WHOLE SD CARD!
sudo dd bs=4M if=/dev/mmcblk0 of=rpi.img conv=fsync 
# shrink it (install)
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin
# shrink it
pishrink -p rpi.img 

# write it: insert a fresh card
umount /dev/mmcblk0p1 /dev/mmcblk0p2 # if automounted
dd bs=4M if=rpi.img of=/dev/mmcblk0 status=process conv=fsync 

# some useful commands
sudo fdisk -l /dev/mmcblk0   # 'physical' disk details
file original.img            # img file partitions
losetup                      # mount a .img file
fsck                         # check the filesystem/s

But wait! Is it exactly the same on a different machine? No! Change the cloned Pi a bit:

  • Hostname (raspi-config and reboot)
  • AP and MAC address-related (e.g. ap0 interface):
# get mac addr of wlan0 interface and change from masterpi's mac
iw dev 
sudo vi /etc/udev/rules.d/70-persistent-net.rules
# change AP name
sudo vim /etc/hostapd/hostapd.conf 

Kiosk Mode


Leave a Reply

Your email address will not be published. Required fields are marked *