Posts

Hardware

30 Aug 2026

13 MIN read

••• views

Setting up a Raspberry Pi, headless

From a blank SD card to a Pi you can SSH into, without ever plugging in a keyboard or a screen.

Raspberry Pi

Hey there! I have had a Raspberry Pi for a while. In my old setup it ran Kodi as a media player, and after I moved on from that house it just sat around unused.

I needed a small machine for a project recently, so I dug it out to put it to use again.

One small problem: I had broken my micro HDMI cable, so I could not plug it into a screen. I was kind of locked out of my own Pi 😅. So I set it up headless: no keyboard, no monitor, everything from my laptop over SSH.

All you need is a Pi, a microSD card, and a decent power supply, ideally the official one.

Flashing the card

Get Raspberry Pi Imager and install it. Plug the SD card into your computer, open Imager, and choose:

Raspberry Pi Imager's first step, "Select your Raspberry Pi device", listing Raspberry Pi 5, 4, 3, 2 and 1

  1. Device: your Pi model. Mine is a Raspberry Pi 4, so I picked Raspberry Pi 4.
  2. OS: Raspberry Pi OS (other) → Raspberry Pi OS Lite. Lite has no desktop. That means no display server and desktop apps sitting in memory doing nothing, which leaves more room for what you actually want to run. On a Pi with 1 GB of RAM like mine, that makes a real difference.
  3. Storage: the SD card. Make sure you pick the right drive.

Raspberry Pi Imager's "Choose operating system" step, scrolled down to Raspberry Pi OS (other), where the Lite images live

Customise before you write

After you pick the storage, Imager walks you through a few customisation screens. Do not skip them. This is the part that makes headless work, because the Pi boots up already knowing your Wi-Fi and already running SSH.

Hostname. I used homepi, which means I can reach the Pi as homepi.local instead of hunting for its IP address.

Imager's "Choose hostname" screen with homepi entered

Localisation. Pick your city, and Imager fills in the time zone and keyboard layout for you.

Imager's "Localisation" screen with a capital city, time zone and keyboard layout

User. Create a username and password. Raspberry Pi OS does not come with a default pi user anymore, so this is the only account on the Pi. Wherever you see username in this post, use the one you pick here.

Imager's "Choose username" screen with username and password fields

Wi-Fi. Add your network name and password. If you are plugging in Ethernet, you can skip this one.

Imager's "Choose Wi-Fi" screen with SSID and password fields

SSH. Turn on Enable SSH. If you already have an SSH key, pick public-key authentication and add your public key. If you do not, pick password for now. We will switch to a key later in this post.

Imager's "SSH authentication" screen with SSH enabled and public key authentication selected

The last screen is Raspberry Pi Connect, for reaching the Pi through Raspberry Pi's own service. We are using SSH, so I left it off.

Imager's "Raspberry Pi Connect" screen with the toggle off

Write the card, put it in the Pi, and plug in the power.

First boot

The first boot takes a few minutes while the Pi resizes the filesystem and applies your settings. Once it is up, connect from your computer:

bash
ssh username@homepi.local

If homepi.local does not work, find the Pi's IP address in your router's list of connected devices and SSH to that instead.

Then update everything:

on the Pi
sudo apt update
sudo apt full-upgrade -y
sudo reboot

Quick checks

on the Pi
free -h                # memory
vcgencmd get_throttled # throttled=0x0 means no power or heat problems
timedatectl            # should say "System clock synchronized: yes"
  • get_throttled: anything other than 0x0 means the Pi has been short on power or too hot and slowed itself down. It is usually the power supply.
  • The clock: most Pis do not have a battery-backed clock, so they get the time from the network on every boot. If the time is wrong, HTTPS and anything that uses tokens will fail with errors that do not point at the real cause.

Lock it down so only you can get in

If you set up a password in Imager, the Pi will accept password logins from anything on your network. An SSH key is much harder to guess or steal than a password. The plan: add your key, check it works, then turn off password logins.

Keep one SSH session open the whole time and test changes in a second terminal. If something goes wrong, you can fix it from the first one.

1. Make a key, if you do not have one

On your computer, not the Pi, look in your .ssh folder:

on your computer
ls ~/.ssh

If you see id_ed25519 and id_ed25519.pub, you already have a key. Skip to the next step.

If not, make one:

on your computer
ssh-keygen -t ed25519

Press Enter to accept the default location. When it asks for a passphrase, set one, so the key is protected if your laptop is ever lost.

You now have two files:

  • id_ed25519 is your private key. It never leaves your computer.
  • id_ed25519.pub is your public key. This is the one you give to the Pi.

Why ed25519?

-t picks the type of key, and there are a few. The two you will see most are RSA and Ed25519.

  • RSA is the old default. It is fine, but it needs big keys to stay safe (3072 bits or more today), so the keys are long and a little slower to use.
  • Ed25519 is newer. The keys are tiny (your public key is one short line), signing and checking are fast, and it is designed to be hard to get wrong. It has been in OpenSSH since 2014, and since OpenSSH 9.5 it is what ssh-keygen makes if you do not say otherwise.

So -t ed25519 is just asking for the modern default explicitly. The only reason to pick RSA is if you are connecting to something very old that does not support Ed25519, and a fresh Raspberry Pi is not that.

2. Copy your key to the Pi

What is authorized_keys?

  • What: a plain text file with a list of public keys, one per line.
  • Where: in the .ssh folder of a user's home on the machine you are logging into. For you on the Pi, that is /home/username/.ssh/authorized_keys. Every user has their own.
  • Why: it is how the Pi decides who gets in. When you connect, SSH looks up the user you are logging in as, and reads that user's authorized_keys.
  • How: your computer proves it holds the private key that matches one of the public keys in the list, by signing a challenge with it. The private key itself is never sent. If the signature checks out against a listed key, you are in.

So to let your laptop in, you add one line to that file: your public key. To kick a laptop out later, you delete its line.

The manual way

It is worth doing this once, so you know what is going on.

First, copy the public key over to the Pi:

on your computer
scp ~/.ssh/id_ed25519.pub username@homepi.local:~

scp is cp over SSH. This puts id_ed25519.pub in your home folder on the Pi. It will ask for the Pi's password.

Then log in to the Pi and move the key into place:

on the Pi
mkdir -p ~/.ssh
cat ~/id_ed25519.pub >> ~/.ssh/authorized_keys
rm ~/id_ed25519.pub

That makes the .ssh folder if it is not there, adds your key to the end of authorized_keys (>> appends, so any keys already in there stay), and deletes the copy.

Last, fix the permissions:

on the Pi
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

SSH is fussy about this, and for good reason. If other users could edit your authorized_keys, they could add their own key and log in as you. So if the folder or file is open to anyone else, SSH ignores the key and quietly falls back to asking for a password. 700 and 600 mean only you can read or write them.

The quick way

All of the above is exactly what ssh-copy-id does, in one command:

on your computer
ssh-copy-id username@homepi.local

It copies your public key, appends it to authorized_keys and sets the permissions. Use whichever you like.

Either way, open a new terminal and log in:

on your computer
ssh username@homepi.local

If it asks for your key's passphrase, or nothing at all, the key works. If it still asks for the Pi's password, check the permissions again before going any further.

3. Turn off password logins

SSH's settings live in /etc/ssh/sshd_config. Instead of editing that file, we will create our own small file in /etc/ssh/sshd_config.d/. Why there, and why that name, is explained in About those file names below.

Open a new file in nano:

on the Pi
sudo nano /etc/ssh/sshd_config.d/00-lockdown.conf

Type these four lines, using your own username on the last one:

00-lockdown.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowUsers username

Save with Ctrl+O, press Enter, then exit with Ctrl+X.

Here is what each line does:

  • PasswordAuthentication no: no logging in with a password.
  • KbdInteractiveAuthentication no: closes the other way of typing a password in. Without this line, some setups would still ask for one.
  • PermitRootLogin no: nobody can log in as root directly.
  • AllowUsers username: only your user can log in over SSH.

Before restarting SSH, check the file for mistakes:

on the Pi
sudo sshd -t

If it prints nothing, the config is fine. If it prints an error, fix the file first. A broken config can stop SSH from starting, and then you are locked out.

Now restart SSH with systemctl so it picks up the new settings:

on the Pi
sudo systemctl restart ssh

Test it. Your current session stays connected. Open a new terminal and log in with your key, which should still work. Then try forcing a password login:

ssh -o PubkeyAuthentication=no username@homepi.local

It should fail with Permission denied (publickey). That means passwords are off.

4. Make sudo ask for a password

For a long time, Raspberry Pi OS let the first user run sudo without typing a password. It did that with a small file, /etc/sudoers.d/010_pi-nopasswd, containing one rule: this user can run anything as root, NOPASSWD. It was a convenience for the old default pi user, and it stuck around.

From Raspberry Pi OS 6.2 (April 2026), new installs ask for your password by default. But that change does not touch existing installs, and older images still have the file. So it is worth checking.

Run this:

on the Pi
sudo -k
sudo true

sudo -k forgets that you have recently typed your password, and sudo true runs a command that does nothing, as root. If it asks for your password, you are done, skip ahead. If it does not, look for the file:

on the Pi
ls /etc/sudoers.d/

If 010_pi-nopasswd is there, check that you know your password, then remove it:

on the Pi
sudo rm /etc/sudoers.d/010_pi-nopasswd

Run sudo -k and sudo true again. This time it should ask.

5. Install security updates automatically

on the Pi
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

It asks if you want to download and install updates automatically. Say yes.

Now only your key can get you in, only as your user, and sudo still needs your password. If you get a new computer, add its key to authorized_keys on the Pi before you stop using the old one.

If you lose your only key after turning off passwords, the only way back in is taking the SD card out and editing it on a computer. So keep a second key in authorized_keys, from another machine or backed up somewhere safe. Then losing your laptop does not lock you out of the Pi.

About those file names

We touched two oddly named files: 00-lockdown.conf in step 3 and 010_pi-nopasswd in step 4. Both live in a drop-in directory, and once you know the pattern you will see it all over Linux.

One main file, plus a folder of extras

Some programs read their main config file, and then every file in a folder next to it:

ProgramMain fileDrop-in folder
SSH server/etc/ssh/sshd_config/etc/ssh/sshd_config.d/
sudo/etc/sudoers/etc/sudoers.d/

That is why we never edited the main files. Your change is one small file you can read at a glance, delete to undo, and that a system update will not overwrite when it replaces the main file.

Why the numbers

The files in the folder are read in alphabetical order. The number at the front is just a way to control that order: 00- comes before 10-, which comes before 50-.

For SSH, order matters a lot, because the first value it reads for a setting wins. On Raspberry Pi OS (and Debian), sshd_config pulls in the drop-in folder at the very top, before its own settings. So 00-lockdown.conf is read before almost anything else, and if some other file later tries to set PasswordAuthentication yes, it is ignored. See Include in the sshd_config manual.

Why .conf on one and not the other

Each program has its own rule for which files in the folder count:

  • SSH only reads files ending in .conf. Name your file 00-lockdown and SSH silently ignores it.
  • sudo does the opposite. It skips any file with a . in its name (so editor backups like file.bak cannot sneak in rules). That is why it is 010_pi-nopasswd with no extension. See @includedir in the sudoers manual.

So if a setting you added seems to do nothing, check the file name before anything else.

What is next

That is the Pi sorted. It has a name, it only lets me in, and it keeps itself up to date.

The nice thing is that almost none of this was about the Pi. Swap the SD card for a cloud server and it is the same checklist: create your user, add your key, turn off passwords, turn on automatic updates.

Until then!

Keep reading