Published on 2024-08-31
IoTJust a quick guide to kickstarting Raspberry Pi adventure from scratch - no monitor needed! 😗
I don't have a monitor or an extra set of keyboard, so, I have to configure my Raspberry Pi headless.
To use Raspberry Pi, we need an operating system. By default, Raspberry Pi automatically boots from the microSD slot when the slot contains a card.
First, install an operating system in the boot media (in our case, it's the microSD. it can be any storage devices). What we need to install an OS in the boot media: - Raspberry Pi Imager: a tool to help download and write images to the storage device. - a computer where we can install and use Raspberry Pi Imager - microSD card slot in that computer
Alternatively, we may be able to install an operating system directly to Raspberry pi from the internet (I haven't explored this option). But it requires a monitor, a keyboard and a wired internet connection.
[General]
We need username and password, and of course, Wi-FI to remotely connect to our Raspberry Pi. Let's configure these.
[Services]
We want to be able to SSH into our Raspberry Pi. Let's enable this. We will go with password authentication for the first SSH connetion. Later, we are going to change it to Public-key authentication.
Once done, we can insert our microSD card into Raspberry Pi and let it boot from the SD.
Note: I had to update the bootloader because, for some reasons, my Raspberry Pi won't boot up from SD card. Basically, I had to rewrite the microSD card with Boot Loader configuration information, and explicity states the configuration to read from SD. Here is the online instruction. Then, I inserted the microSD card to Raspberry Pi, and let it get the config from the microSD card for a good 5 mintues. Then, I shut down the Raspberry Pi, and rewrote the microSD card with Bookworm OS. After that, I was able to boot up Bookworm from my Raspberry Pi.
From terminal, ping to the the ip address of raspberry pi to verify raspberry pi has booted up the OS and connected to the Wi-Fi. We can find the ip address of raspberry pi from the network router. Router’s IP address is usually 192.168.0.1. Log in with the router’s credentials, and see the section listing all devices currently connected to the network, including their IP addresses, MAC addresses, and sometimes device names.
For windows, use puTTy. (Not going to explore further into this. I am using macOS.) For macOS, enter below command in the terminal.
ssh [username]@[hostname].local OR ssh [username]@[ip_address].local
username = the name setup during the preconfigure of OS customisationIn the next section, we are going to enable the VNC. Why we want VNC? to be able to control our Raspberry Pi with GUI. It's easier than using command lines. But, to each its own, there are pros and cons. After that, we are going to change our SSH authentication to Public-key authentication from Password authentication.
After ssh-ing into Raspberry Pi, we can configure the raspberry pi configuration.
Enable the VNC in the Raspberry Pi by
sudo raspi-config
In local machine, download, install and launch TigerVNC. Note: the latest VPN connection to Raspberry Pi 5 only works with TigerVNC due to security updates.
Public-key authentication is more secure because it uses cryptographic keys instead of passwords. Even if someone intercepts SSH traffic, they won't be able to log in without the private key. Note, below instruction is written for command line interface. Of course, this can also be done with GUI, now that we have already setup VNC. ;)
ssh-keygen -t rsa -b 4096`
Follow the prompts to save the key pair (it is usually in ~/.ssh/scp <source> <destination>
. Therefore, we need to do: scp <path_to_file> [username]@[hostname].local:<path_to_destination>
. scp will log into the remote server, copy the file, then log out again in one process, so just run it from a shell on local machine. There is no need to ssh into the remote server.
scp pi.pub > [username]@[hostname].local:~
Above command login to Raspberry Pi, copy the ssh [username]@[hostname].local OR ssh [username]@[ip_address].local
b. create the '.ssh' directory and 'authorized_keys' file:
mkdir -p ~/.ssh
mv pi.pub ~/.ssh
cd .ssh
touch authorized_keys
c. copy the content of the cat pi.pub > authorized_keys
d. set the permission of the folder and file:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
e. cd back to the home directory.sudo nano /etc/ssh/sshd_config`
Change "#PubkeyAuthentication yes" to "PubkeyAuthentication yes"sudo systemctl restart ssh
g. The last step is to verfiy the Public-key authentication works. By default, the SSH client looks for private key files in the ~/.ssh directory on your local machine. The most commonly used key files are
~/.ssh/id_rsa (RSA key)But since I am using a different name than id_rsa, I have to explicitly state the key file location. From local machine, enter the command:
ssh -i ~/.ssh/pi [username]@[hostname].local
pi = the name of the private key of mine.
When prompt, enter the passphrase to ssh-ing int the raspberry pi. And, we're done! SSH-ing with Password authentication should not work anymore. 😎