This guide outlines the steps I follow after a fresh Raspberry Pi OS Lite (64-BIT) installation on a headless RasPi. It assumes you have already installed the OS, enabled SSH, and successfully connected to your RasPi via SSH. While it works well for my purposes, your preferences and requirements might differ. Use this as a starting point and adjust it as needed.
After completing this guide you may want to check out my Headless Raspberry Pi Optimization Guide.
Update the list of available packages and then upgrade all installed packages to their latest versions, automatically handling dependencies:
sudo apt update && sudo apt -y full-upgrade
These settings can also be configured using raspi-config
. I simply prefer using commands.
This configuration change allows your RasPi to automatically log in as the specified user on boot, eliminating the need to manually enter credentials every time.
-
Open the autologin configuration file:
sudo nano /etc/systemd/system/getty@tty1.service.d/override.conf
-
If the file doesn't exist, create it and add the following content:
[Service] ExecStart= ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM
(Replace
pi
with your desired username if it's different.) -
Save and exit the editor (Ctrl+X, then Y, then Enter).
This process ensures that your operating system utilizes all the available space on your SD card, which is especially important if you've installed a larger card after initially setting up your RasPi.
-
Install Required Tool:
The tool to expand the root partition, growpart, is not included in the default Raspberry Pi OS installation.
sudo apt install cloud-guest-utils
-
Check the current partition layout:
sudo parted -l
-
Resize the root partition to fill the SD card:
sudo growpart /dev/mmcblk0 2
-
Expand the filesystem to match the resized partition:
sudo resize2fs /dev/mmcblk0p2
-
Reboot:
sudo reboot
Key-based authentication is significantly more secure than password-based authentication for remote access. Passwords are vulnerable to brute-force attacks, whereas keys are much harder to crack. Additionally, key-based authentication allows for convenient, passwordless logins once set up correctly.
FIRST STEP - ON LOCAL MACHINE:
Generate a pair of cryptographic keys (public and private). The private key remains on your local machine and should be kept secret.
If you have not already generated keys execute this command on your local machine:
ssh-keygen -t rsa
Copy the public key to your Raspberry Pi (adjust for correct user & ip):
scp ~/.ssh/id_rsa.pub <user>@<ip>:
SECOND STEP - ON REMOTE MACHINE (RasPi):
SSH into remote machine and create .ssh
directory, then copy public key to authorized_keys
file:
mkdir -p .ssh && cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
Remove the original copy:
rm ~/id_rsa.pub
Reboot
sudo reboot
sudo apt -y install curl figlet git htop ii neofetch net-tools nmon
This command installs a set of tools commonly used for system administration, monitoring, software development, and basic image viewing. It's particularly useful for setting up a headless Raspberry Pi, where most interactions are done through the command line.
Here's a breakdown of each package:
- curl: A command-line tool for transferring data using various protocols, including HTTP, FTP, and others. Commonly used to download files or scripts from the internet.
- figlet: Creates large text banners from regular text. Mostly used for fun and decorative purposes.
- git: A distributed version control system used for tracking changes in source code during software development. Essential for collaborating on projects.
- htop: An interactive process viewer similar to
top
, but with a more user-friendly interface and features like vertical and horizontal scrolling. - ii: A simple command-line image viewer for viewing images in the terminal.
- neofetch: Displays system information (OS, kernel, hardware, etc.) in a visually appealing format along with your system's logo.
- net-tools: A collection of basic networking tools like
ifconfig
(now replaced byip
) andnetstat
(now replaced byss
). Still useful for some specific tasks. - nmon: A system monitoring tool that displays performance statistics for CPU, memory, network, and disk usage.
GitHub CLI (Command Line Interface) is a tool that lets you interact with GitHub repositories and features directly from your terminal or command prompt. It provides a way to perform common actions like creating pull requests, reviewing code, and managing issues without leaving your command-line environment.
Copy/paste & execute the entire block:
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
Oh My Bash is a free, open-source framework for managing your Bash configuration on Unix-like systems. It simplifies customization by providing a collection of themes, plugins, and helpful functions to enhance your command-line experience.
bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"
The following code changes the default Oh My Bash theme to my preferred theme called Zork.
.hushlogin
is an empty file that suppresses the display of the "Last login" message and other system information that's typically shown when logging into a Unix-like system through a terminal.
Copy/paste & execute the entire block:
# Change Oh My Bash theme
sed -i 's/font/zork/g' ~/.bashrc
# Append the following to ~/.bashrc
cat >> ~/.bashrc << EOL
alias c="clear && source ~/.bashrc"
# Remove duplicate $PATH entries (https://unix.stackexchange.com/a/149054)
PATH="$(perl -e 'print join(":", grep { not $seen{$_}++ } split(/:/, $ENV{PATH}))')"
# A rather comprehensive update alias:
alias update='sudo apt update && sudo apt -o Dpkg::Options::="--force-confdef" dist-upgrade -y && sudo apt autoremove -y && if sudo test -f /var/run/reboot-required; then read -p "A reboot is required to finish installing updates. Press [ENTER] to reboot now, or [CTRL+C] to cancel and reboot later." && sudo reboot; else echo "A reboot is not required. Exiting..."; fi'
# Show line numbers in nano
alias nano="nano --linenumbers "
neofetch
EOL
touch ~/.hushlogin
source ~/.bashrc
Overclocking can boost performance but might increase heat and power consumption. You should have your RasPi in a case with a fan.
-
Edit Configuration:
sudo nano /boot/firmware/config.txt
-
Add Parameters:
over_voltage=6 # Increase voltage for stability (6 is a safe starting point) arm_freq=2147 # Maximum CPU frequency (2147 MHz is a common limit for Pi 4) gpu_freq=750 # GPU frequency (adjust if needed for graphics-heavy tasks)
-
Save and Exit:
Press
Ctrl+X
to save the file and exit nano (follow prompts).
Important Considerations:
- Monitor Temperature: Use
vcgencmd measure_temp
to check your RasPi's temperature regularly. If it gets too hot, reduce the overclock settings or add cooling. - Power Supply: Overclocking requires a reliable power supply capable of providing enough current.
- Warranty: Overclocking might void your warranty. Proceed at your own risk.
- Stability: Not all Raspberry Pis will overclock to the same extent. You might need to experiment to find stable settings.
This section of the guide outlines the steps to set up a dedicated swap partition on a USB drive. By utilizing a USB drive for swap space, you can free up valuable system resources on your RasPi's SD card and potentially improve overall performance.
-
Disable and uninstall
dphys-swapfile
:sudo dphys-swapfile swapoff sudo dphys-swapfile uninstall sudo update-rc.d dphys-swapfile remove
If the following returns a directory:
ls -l /etc/systemd/system/multi-user.target.wants/dphys-swapfile.service
remove it:
sudo rm /etc/systemd/system/multi-user.target.wants/dphys-swapfile.service
-
Identify USB Drive:
lsblk
-
Format (replace sda with correct drive if different):
sudo fdisk /dev/sda
- Type
d
, then1
to delete existing partitions (if any). - Type
n
, thenp
for primary, accept defaults. - Type
t
, then82
for Linux swap. - Type
w
to write changes.
- Type
-
Activate and Make Permanent:
sudo mkswap /dev/sda1 # Replace with your partition sudo swapon /dev/sda1 echo "/dev/sda1 none swap sw 0 0" | sudo tee -a /etc/fstab
-
Verify:
sudo swapon --show free -h
This guide was created with the assistance of Gemini.