fshr
The musings of a grumpy hairless ape
Raspberry Pi Headless
A quick run through of setting up a Raspberry Pi when you can’t connect a keyboard or monitor.
All the tasks were done using a Mac, so you may need to adjust some pieces if you’re using Windows or Linux (*).
Install
Download the image and write it to the SD card. For this, I used the RasPI OS Buster Lite image which just gives the base OS without any of the GUI components. You can download from https://www.raspberrypi.org/software/, either using the Imager, or with the separate image file (though you’ll then need to use your own tool to write the card).
Once the image has been written, remove and reinsert the SD card to force a remount (*).
The SD card should mount as ‘boot’. On the card you need to write 2 files…
- An empty file named “ssh”
- A file named “wpa_supplicant.conf” containing the WiFI details:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert your 2 letter country code here, e.g. GB for the UK>
network={
ssid="<Name of your wireless LAN>"
psk="<Password for your wireless LAN>"
}
Eject the SD card, pop it in the PI and boot!
Configure
SSH to the PI with username “pi” and password “raspberry”
Set the root password to something
sudo passwd root
Edit /etc/ssh/sshd_config and change the following line
#PermitRootLogin prohibit-password
to
PermitRootLogin yes
Restart SSHD
sudo systemctl restart sshd
Log out, and ssh back in as root with the password you set
For all commands below replace “newname” with your chosen userid
Change the default “pi” user to a username of your choice, and rename the home directory
usermod -l newname pi
usermod -m -d /home/newname newname
If you want to keep the ability to run sudo as your new account without needing to provide a password, edit the file /etc/sudoers.d/010_pi-nopasswd and change the line
pi ALL=(ALL) NOPASSWD: ALL
to
newname ALL=(ALL) NOPASSWD: ALL
Log out, and ssh back in as your new username with password “raspberry”
Test that you can still run commands with sudo
sudo echo "Hello"
You should see “Hello” echoed back to you
Finish
To “relock” the root account from logins
sudo passwd -l root
Edit /etc/ssh/sshd_config and change the following line
PermitRootLogin yes
back to
#PermitRootLogin prohibit-password
Run an update
sudo apt update
and an upgrade
sudo apt dist-upgrade
And reboot
sudo reboot
Content borrowed from
https://www.raspberrypi.org/documentation/configuration/wireless/headless.md
https://thepihut.com/blogs/raspberry-pi-tutorials/how-to-change-the-default-account-username-and-password