Quickly Setup Headless Raspberry Pi

Quickly Setup Headless Raspberry Pi

Download

Raspberry Pi OS Lite

Raspberry Pi OS Lite (32-bit; May 2020) sha256sum: f5786604be4b41e292c5b3c711e2efa64b25a5b51869ea8313d58da0b46afc64

Diet Pi

DietPi_RPi-ARMv6-Buster.img (Jun 2020) sha256sum: 046529ab9d4878fedf292eda0b27643bd0787391458a9370d827c7bbf4a37081

Install on microSD Card

lsblk -l | grep -vE 'lvm|sda' to find microSD

-v, --invert-match; -E, --extended-regexp

Unzip and copy image to sdX

unzip -p raspi.zip | sudo dd bs=4m of=/dev/sdX conv=fsync

or,

7z e dietpi.7z | sudo dd bs=4m of=/dev/sdX conv=fsync

Setup

Wifi (RasPi OS)

You will need to define a wpa_supplicant.conf file for your particular wireless network. Put this file in the boot folder, and when the Pi first boots, it will copy that file into the correct location in the Linux root file system and use those settings to start up wireless networking.

vim /run/media/daniel/boot/wpa_supplicant.conf

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

network={
 ssid="<SSID>"
 psk="<Password>"
}

(DietPi)

vim /run/media/daniel/boot/dietpi.txt

AUTO_SETUP_NET_WIFI_ENABLED=1

vim /run/media/daniel/boot/dietpi-wifi.txt

aWIFI_SSID[0]='MySSID' 
aWIFI_KEY[0]='MyWifiKey'

Still to try getting headless over Wifi working... correctly setting localization may be the problem.

Setup SSH (RasPi OS)

For headless setup, SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card from another computer. When the Pi boots, it looks for the ssh file. If it is found, SSH is enabled and the file is deleted. The content of the file does not matter; it could contain text, or nothing at all.

touch /run/media/daniel/boot/ssh

More connection options.

Connect

ping raspberrypi.local to find the IP Address.

Then,

ssh [email protected]<IPADDRESS>

Enter default password: raspberry

Change Admin Password & Add User

passwd                           	# Choose a long root password!
sudo adduser username
sudo usermod -aG sudo 

Update, add vim, and add automatic updates.

sudo apt update && sudo apt upgrade
sudo apt install vim
sudo crontab -e
0 12 * * * apt update && apt upgrade -y
@reboot apt update && apt upgrade -y

sudo systemctl restart cron

Restrict SSH

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server
sudo vim /etc/ssh/sshd_config
AllowUsers username
DenyUsers pi

sudo systemctl restart ssh

Setup Fail2Ban

sudo apt install fail2ban && sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local