Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Networking and Servers

SSH, VNC and Remote Access

Control a headless Raspberry Pi: enable SSH, log in with key pairs instead of passwords, copy files with scp and rsync, forward ports, and use VNC or Raspberry Pi Connect.

Most Pis live without a monitor: behind a TV, in a greenhouse, on a shelf. You work on them from your laptop through SSH, a secure remote shell. Once SSH works, you can edit files, install software and read logs as if you were sitting at the Pi. SSH also copies files and carries other network traffic safely, so it is the most valuable tool in your belt.

The real commands need a Pi and a network, so they are read-only here. The runnable blocks demonstrate the ideas behind key authentication with Python's standard library.

Turning SSH on

On Raspberry Pi OS, SSH is off until you enable it. The easiest way is Raspberry Pi Imager: in the settings dialog tick Enable SSH, set a username and password (or paste a public key). You can also enable it afterwards:

Terminal
sudo raspi-config        # Interface Options -> SSH
sudo systemctl enable --now ssh

For a card you have already written, create an empty file called ssh on the boot partition before first boot (older images) or use Imager's settings. Then connect from your laptop:

Terminal
ssh ana@greenhouse.local
ssh -p 2222 ana@192.168.1.50      # non-default port

The first time you connect, SSH shows the server's key fingerprint and asks whether you trust it. It stores the answer in ~/.ssh/known_hosts. If the fingerprint later changes without you reinstalling the Pi, SSH warns loudly: that can mean a different device or a man-in-the-middle, so do not blindly delete the line.

Key pairs instead of passwords

A password can be guessed. A key pair cannot: you create a private key that never leaves your laptop and a public key you place on the Pi. When you connect, the Pi challenges you to prove you hold the private key, without the key ever being sent.

Terminal
ssh-keygen -t ed25519 -C "laptop"          # creates ~/.ssh/id_ed25519 (+ .pub)
ssh-copy-id ana@greenhouse.local           # appends the public key to the Pi
ssh ana@greenhouse.local                   # now logs in without the password

The public key goes into ~/.ssh/authorized_keys on the Pi (permissions: the .ssh folder 700, the file 600, or SSH ignores it). Protect the private key with a passphrase, and use ssh-agent so you type it once per session.

The idea of a challenge can be modeled with a shared secret and an HMAC. Real SSH uses public-key signatures, but the shape is the same: the server sends a random number, the client answers with something only the key holder can compute, and nothing secret crosses the wire. This is a simplified stand-in.

Python Stand-in: challenge and response

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The third line shows why each login uses a new challenge: an eavesdropper cannot reuse an old answer.

Once keys work, turn off password logins in /etc/ssh/sshd_config.d/10-local.conf:

TEXT
PasswordAuthentication no
PermitRootLogin no

Apply with sudo systemctl reload ssh. Test from a second terminal before closing the first, so a typo cannot lock you out.

A config file for convenience

~/.ssh/config on your laptop gives hosts short names:

TEXT
Host gh
    HostName greenhouse.local
    User ana
    IdentityFile ~/.ssh/id_ed25519

Now ssh gh is enough, and scp, rsync and VS Code's Remote-SSH reuse it. The next block parses such a file the way a tool would, to show how names resolve.

Python Stand-in: resolve an SSH config entry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Copying files

Terminal
scp report.csv gh:/home/ana/data/                 # laptop -> Pi
scp gh:/home/ana/weather/log.csv .                # Pi -> laptop
rsync -avz --progress ~/project/ gh:~/project/    # only changes are sent
rsync -avz --delete gh:~/photos/ ./photos-backup/

rsync compares files and transfers only what changed, so repeating it is fast. Be careful with --delete and with trailing slashes: dir/ means the contents, dir means the folder itself.

Tunnels

SSH can forward a port so a service on the Pi appears on your laptop without opening it to the network:

Terminal
ssh -L 8080:localhost:5000 gh        # laptop:8080 -> Pi's localhost:5000
ssh -D 1080 gh                       # SOCKS proxy through the Pi

Open http://localhost:8080 on the laptop and you reach a web app that listens only on the Pi's loopback. This is a safe way to use admin pages.

Graphical remote access

  • VNC (RealVNC server is built in): enable it in raspi-config; works for the desktop, including on a headless Pi if you set a screen resolution there.
  • Raspberry Pi Connect: official screen sharing and remote shell through a browser, without port forwarding.
  • X11 forwarding (ssh -X) runs a single graphical program remotely; fine for a quick tool but slow.

For automation, SSH alone is usually enough. Text is lighter than pixels.

Never forward port 22 straight from your router to the internet with password login enabled. Bots will find it within hours. Use keys, a VPN such as WireGuard or Tailscale, or Raspberry Pi Connect.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file on the Pi stores the public keys allowed to log in?

    authorized_keys lists accepted public keys; known_hosts is the client's list of servers.
  2. 2. Which key must NEVER leave your laptop?

    The private key proves your identity; only the public key is shared.
  3. 3. What does ssh -L 8080:localhost:5000 gh do?

    -L creates a local forward through the encrypted SSH connection.
  4. 4. Why test a new sshd configuration from a second terminal?

    If the change breaks logins, the first session is still open to undo it.

Summary

  • Enable SSH in Imager or raspi-config, then ssh [email protected].
  • Prefer key authentication (ssh-keygen, ssh-copy-id) and disable password logins once it works.
  • ~/.ssh/config gives short aliases; scp and rsync copy files.
  • ssh -L forwards ports safely; VNC and Raspberry Pi Connect give a desktop.
  • Do not expose SSH to the internet with passwords.

Next: Serving a Web Page from the Pi