Module 1 · Lesson 21 of 103

SSH and the serial console

Reach the board over SSH with key authentication, interpret the host key prompt, and open a serial console when the network is down.

  • Beginner to advanced
  • 6 min read
  • 21 min lab
What you will need

A computer to flash the SD card and run SSH; a Raspberry Pi 4 or 5 with a power supply and microSD card; a 3.3 V USB-to-serial adapter for the serial section

Your place in the module
By the end you can 3 objectives
  • Connect over SSH, interpret the host-key prompt, and install a key with ssh-copy-id.
  • Generate a key pair and disable password logins on the SSH server.
  • Open a serial console at 115200 8N1 and enable the UART with enable_uart or raspi-config.
Video coming soonSSH and the serial console
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from From board to first boot
  • A Raspberry Pi is a single-board computer around a Broadcom SoC; the microSD card holds everything it boots from.
  • Headless means no monitor or keyboard: enable SSH with a marker file and supply Wi-Fi with a config file.
  • Verify the downloaded image checksum before flashing: a corrupted image or a bad write boots to a silent failure.
01

SSH first contact

3 min read

The board is now flashed and booting headless, so it is reachable on your network. This lesson answers how to reach it over SSH with key authentication and when to fall back to the serial console.

Connect with ssh, giving the user and the board's address:

bash
ssh learner@sensorpi.local

On the very first connection, the server presents its , and the client shows a fingerprint of it. Typical output looks like this (yours will differ):

text
The authenticity of host 'sensorpi.local (192.168.1.42)' can't be established.
ED25519 key fingerprint is SHA256:AbC... (yours will differ).
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Answering yes stores the key in ~/.ssh/known_hosts. From then on the client checks that the server presents the same key, so that a machine impersonating your board cannot silently take over the connection.

That same check explains a warning you will meet one day: if the key changes, ssh refuses to connect and prints a message like REMOTE HOST IDENTIFICATION HAS CHANGED. That happens when you reflash the OS, which gives the board a new host key, or when a new device takes over an old IP address. If the change is expected, remove the stale entry with ssh-keygen -R and reconnect:

bash
ssh-keygen -R sensorpi.local

SSH also uses a . Right now you are logging in with the password you set during setup, but the better way is to authenticate with your own key, which the next section covers.

A changed host key is a signal

Treat the changed-key warning as meaningful. If you did not reflash the board or change its address, investigate before answering yes to a new fingerprint.

First SSH connectionClientServeropen connectionhost key + fingerprintcheck known_hostsencrypted session1. Client → Serveropen connection2. Server → Clienthost key + fingerprint3. Client → Servercheck known_hosts4. Server → Clientencrypted session
The first SSH connection: the server presents its host key and the client records it in known_hosts.
02

SSH key authentication, done right

4 min read

Generate a key pair on your laptop with ssh-keygen. Ed25519 is the recommended type:

bash
ssh-keygen -t ed25519 -C "learner@labpc"

The command writes a private key to ~/.ssh/id_ed25519 and a public key to ~/.ssh/id_ed25519.pub. The public key is safe to copy anywhere; the private key must stay on your machine and readable only by you, which connects back to the permissions work in the previous lesson:

bash
chmod 600 ~/.ssh/id_ed25519

Toggle the bits below to see which mode is owner-only (600):

600 = -rw-------; the private key is readable and writable only by its owner, which sshd requires before it trusts the key.

  • octal: 600

Install the public key onto the board with ssh-copy-id, which asks for your password once and then appends the key to the board's ~/.ssh/authorized_keys file:

bash
ssh-copy-id learner@sensorpi.local

From now on ssh authenticates you with the key. The mechanism is a signature, not an encryption: the server sends a random challenge, the client signs it with the private key, and the server verifies the signature against the public key in authorized_keys. This is the SSH-2 design described in RFC 4252. Ed25519 is a signature-only algorithm, so the older idea of the server encrypting a challenge with your public key does not apply to it.

Step by stepHow public-key authentication works
  1. Request: the client asks to authenticate and sends the public key it wants to use.

  2. Challenge: the server checks that the key is in authorized_keys, then sends random data.

  3. Signature: the client signs the challenge with its private key, which never leaves the client.

  4. Verify: the server verifies the signature using the stored public key.

  5. Grant: a valid signature proves possession of the private key, so the server allows the login.

Once key login works, disable password authentication so that only key holders can get in. Edit /etc/ssh/sshd_config on the board and set:

text
PasswordAuthentication no

then restart the server:

bash
sudo systemctl restart ssh
Test before you lock the door

Open a second SSH session and confirm key login works in it before you disable passwords, and keep the first session open while you do. Disabling password auth before you have a working key can lock you out of a headless board.

Going deeperWhy Ed25519, and why the private key permissions matter

Ed25519 is a modern elliptic-curve signature scheme that OpenSSH generates by default. It is fast, small, and signature-only, which is why the SSH-1 style of encrypting a challenge with the public key simply does not exist for it. RSA keys are still common, and they too authenticate by signing in SSH-2. The ~/.ssh directory and the private key must not be writable by others, or the server and client both refuse to use them; this is the same least-privilege principle from the permissions lesson, applied to a credential rather than a document.

SSH public-key authenticationClientServerauth request + public keyrandom challengechallenge signed with privatekeyverify with public keyaccess granted1. Client → Serverauth request + public key2. Server → Clientrandom challenge3. Client → Serverchallenge signed with privatekey4. Server → Clientverify with public key5. Server → Clientaccess granted
Public-key authentication: the client signs a challenge with its private key and the server verifies it with the public key.
03

The serial console and GPIO numbering

3 min read

A on the Pi is exposed on the GPIO header. Transmit is GPIO14 on physical pin 8, receive is GPIO15 on physical pin 10, and ground is pin 6. The trick is to cross the wires: your adapter's TXD wire goes to the Pi's RXD (pin 10), and the adapter's RXD wire goes to the Pi's TXD (pin 8). Ground connects to ground.

The signal is 3.3 V TTL, not the 12 V of old PC serial ports, so you need a USB-to-serial adapter that speaks 3.3 V logic (FTDI, CP2102 or CH340 based boards all work). Leave the adapter's power pin unconnected: the Pi is already powered.

Open the console with a terminal program at 115200 baud, 8 data bits, no parity, one stop bit, written 115200 8N1:

bash
sudo apt install picocom
picocom -b 115200 /dev/ttyUSB0

screen and minicom are equivalent alternatives: screen /dev/ttyUSB0 115200 or minicom -D /dev/ttyUSB0 -b 115200. On the Pi, the UART may need enabling first. On a Pi 4 the primary UART is wired to Bluetooth by default, so add this line to config.txt:

text
enable_uart=1

On a Pi 5 the GPIO14/15 UART is enabled with a device-tree parameter instead:

text
dtparam=uart0_console

The Pi 5 also has a dedicated 3-pin debug UART connector that is active by default, so on that model you can attach a lead there without any config change. On Pi 1, 2 and 3 the UART on GPIO14/15 is the primary console already. The raspi-config menu (Interface Options, then Serial) writes the same setting for you.

For the kernel to actually print to the serial port, the cmdline.txt file must name it:

text
console=serial0,115200 console=tty1 root=PARTUUID=xxxxxxxx-02 rootfstype=ext4 fsck.repair=yes rootwait

The console=serial0,115200 part tells the kernel to send its boot messages to the UART at 115200 baud, which is how you capture the boot log even when nothing else works.

Cross the wires, keep the levels at 3.3 V

TXD must reach RXD, not TXD. And never feed 5 V or RS-232 levels into these pins; both can damage the SoC.

UART pin numbering schemesPhysical pinBCM (GPIO)WiringPi(legacy)UART transmit(TXD)8GPIO1415UART receive(RXD)10GPIO1516Ground6GNDGND
The same UART pins expressed in physical, BCM and WiringPi numbering schemes.
Enable the UART and the serial console
learner@embeddex:~/lab$ # on a Pi 4, enable_uart=1 turns on the GPIO14/15 UART
learner@embeddex:~/lab$ printf 'enable_uart=1\n' > config.txt && cat config.txt
enable_uart=1
learner@embeddex:~/lab$ # on a Pi 5 the same UART is enabled with a device-tree parameter
learner@embeddex:~/lab$ printf 'dtparam=uart0_console\n' > config.txt && cat config.txt
dtparam=uart0_console
learner@embeddex:~/lab$ # cmdline.txt must name the UART at 115200 for the kernel to print boot
logs to it
learner@embeddex:~/lab$ printf 'console=serial0,115200 console=tty1 root=PARTUUID=xxxxxxxx-02 ro
otfstype=ext4 fsck.repair=yes rootwait\n' > cmdline.txt && cat cmdline.txt
console=serial0,115200 console=tty1 root=PARTUUID=xxxxxxxx-02 rootfstype=ext4 fsck.repair=yes ro
otwait

Key takeaways

  • SSH proves identity with a key pair; the client signs a challenge with its private key and the server checks the public key.
  • The serial console is a 3.3 V UART on GPIO14 and GPIO15 at 115200 8N1, opened with picocom or screen.
  • The host key identifies one specific SSH server; a changed key after a reflash is fixed with ssh-keygen -R, and an unexpected change must be investigated.
  • Cross the UART wires: the adapter's TXD goes to the Pi's RXD (pin 10) and its RXD to the Pi's TXD (pin 8).
Reflect
  1. Explain to a colleague, in two sentences: connect over SSH, interpret the host-key prompt, and install a key with ssh-copy-id.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close