Unix Tutorial 0/120 lessons ~6 min read Lesson 85

    SSH Key Authentication

    Generate keys with ssh-keygen -t ed25519, copy with ssh-copy-id, then disable password login in sshd_config — the gold standard for server access.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Generate keys with ssh-keygen -t ed25519, copy with ssh-copy-id, then disable password login in sshd_config — the gold standard for server access.

    Beginner analogy: think of Unix as a kitchen. The shell is the chef who reads your order, the kernel is the stove and fridge that actually cook and store, files are the ingredients, and pipes are the conveyor belts moving food from one chef to the next. Every Unix command you learn is one well-designed kitchen tool.

    In this lesson we will walk through SSH Key Authentication step by step, see exactly how Linux handles it under the hood, look at the practical commands you will type every day on real servers, study a real-world DevOps scenario, and finish with the interview questions you will absolutely face when applying to AWS, Google Cloud, Red Hat, Netflix, Stripe and every modern infrastructure team.

    Understanding the topic

    Core concepts to understand:

    • 🧠 Clear definition and mental model of ssh key authentication.
    • 🐧 How the Linux kernel and shell collaborate to make it happen.
    • 📂 Where files, processes and configuration live on a standard Linux server.
    • 🔁 How ssh key authentication fits inside scripts, cron jobs and CI/CD pipelines.
    • 🛡 Permissions, users, groups and least-privilege practices around ssh key authentication.
    • 🚧 Common pitfalls: missing quotes, unset variables, wrong exit codes, dangerous rm -rf.
    • 🏢 Real production scenarios at AWS, Netflix, Stripe and modern SaaS DevOps teams.

    Syntax reference

    Visual workflow / architecture:

    bash
    User Command
    |
    v
    Shell Interpreter
    |
    v
    Command Parsing
    |
    v
    Kernel Interaction
    |
    v
    System Resources
    |
    v
    Command Execution
    |
    v
    Terminal Output
    Interactive Workflow
    SSH Connection Flow
    Client
    ssh user@host
    Key exchange
    Auth (key/pwd)
    Encrypted shell
    Remote Server
    sshd listens :22
    Negotiate cipher
    Validate key
    PTY allocated
    Step 1 / 4
    ssh ops@server.example.com

    Client initiates TCP to port 22 of the remote.

    Informative example

    Hands-on commands you can copy-paste:

    Linux servers are hardened by reducing the attack surface: a default-deny firewall (ufw), key-only SSH, and tailing /var/log/auth.log for suspicious activity. These three steps stop the vast majority of opportunistic attacks.

    bash
    # Lock down a server
    sudo ufw default deny incoming
    sudo ufw allow 22/tcp
    sudo ufw allow 443/tcp
    sudo ufw enable
    # Disable password SSH
    sudo sed -i 's/^#*PasswordAuthentication.*/PasswordAuthentication no/' \
    /etc/ssh/sshd_config
    sudo systemctl reload ssh
    # Watch auth attempts
    sudo tail -f /var/log/auth.log

    Sample terminal output:

    bash
    Status: active
    To Action From
    22/tcp ALLOW Anywhere
    443/tcp ALLOW Anywhere
    sshd[2310]: Accepted publickey for ops from 10.0.0.4

    Walk-through: notice how every Unix tool prints structured text and returns an exit code (0 = success, anything else = failure). That is the contract that lets you chain commands with &&, pipe them with |, and trust them inside automation. Reading these messages carefully is the difference between a senior Linux engineer and a junior one.

    Real-world use

    In production, SSH Key Authentication is part of every infrastructure engineer's daily flow at companies like AWS, Google Cloud, Netflix, Stripe, Shopify, GitHub and Red Hat. Engineers SSH into Linux servers, write small focused bash scripts, schedule them with cron or systemd timers, monitor them in Grafana and ship them through CI/CD. Mastering ssh key authentication means safer deploys, faster incident response and dramatically fewer 3 AM pages.

    Best practices

    • Always start scripts with #!/bin/bash and set -euo pipefail so they fail fast on errors and unset variables.
    • Quote variables: "$file" not $file — protects against spaces and word-splitting bugs.
    • Use absolute paths in cron, scripts and systemd units — $PATH is minimal in those environments.
    • Log to /var/log/<app>/ and rotate with logrotate so disks never fill up.
    • Run as the least-privileged user; reserve sudo for the few commands that truly need root.

    Common mistakes

    • rm -rf $VAR/ when $VAR is empty — wipes the whole filesystem. Always quote and validate.
    • Cron jobs that run from a fresh shell with no $PATH — your script works manually but fails at 2 AM.
    • Forgetting 2>&1 on logs — silent failures because stderr was thrown away.
    • Editing config files without taking a backup (cp file file.bak) — no way to roll back.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Q1. Explain SSH Key Authentication in one sentence as if to a junior teammate.
    • Q2. Walk through the exact Linux commands you would run for ssh key authentication on a production server.
    • Q3. What is the difference between Unix and Linux, and where does ssh key authentication live in the stack?
    • Q4. How would ssh key authentication behave inside a cron job vs an interactive shell, and why?
    • Q5. Name two security or permission concerns around ssh key authentication and how you would mitigate them.
    • Q6. How does ssh key authentication integrate with monitoring, logging and a CI/CD pipeline?
    • Q7. Scenario: a 3 AM PagerDuty alert says ssh key authentication failed in production. Walk me through your debugging.
    Ready to mark this lesson complete?Track your journey across the entire course.