Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    SSH and Remote Work

    SSH (Secure Shell) gives you an encrypted terminal on a remote machine. It uses TCP port 22

    Chapter 10 of 124 min read

    The Plain Picture#

    A secure phone line into the server's terminal. Instead of a password, you use a key: you give the server a copy of your padlock (public key); only you hold the key that opens it (private key) — exactly the padlock picture from Networking 3: DNS, HTTP and HTTPS Ch 10.2.

    The Real Names#

    • SSH (Secure Shell) gives you an encrypted terminal on a remote machine. It uses TCP port 22 (Networking 2: TCP, UDP and Ports Ch 1).

    • Key-based login: your private key stays on your laptop (in ~/.ssh/, permissions 600 — Ch 4). The matching public key is added to the server's ~/.ssh/authorized_keys. When you connect, the server challenges you to prove you hold the private key, without the key ever being sent.

    Under the Hood#

    Terminal
    ssh-keygen -t ed25519 -C "priya@laptop"   # make a key pair → ~/.ssh/id_ed25519 (private) + .pub (public)
    ssh-copy-id priya@13.235.10.20               # add your public key to the server's authorized_keys
    ssh priya@13.235.10.20                       # log in
    ssh -i ~/.ssh/studybuddy.pem ubuntu@13.235.10.20   # use a specific key (common with cloud servers)

    ~/.ssh/config — stop typing long commands:

    Output
    Host sb-prod
        HostName 13.235.10.20
        User priya
        IdentityFile ~/.ssh/id_ed25519

    Now ssh sb-prod is enough.

    Copying files:

    Terminal
    scp report.pdf sb-prod:/tmp/                  # copy a file to the server
    scp sb-prod:/var/log/app.log .                # copy a file from the server to here
    rsync -avz ./site/ sb-prod:/var/www/site/     # sync a folder, sending only what changed (-a keep details, -v verbose, -z compress)

    Port forwarding (tunnels): the production database accepts connections only from inside the private network (Networking 2: TCP, UDP and Ports Ch 11). To inspect it from your laptop, tunnel through a server that can reach it:

    Terminal
    ssh -L 5433:10.0.21.5:5432 sb-prod
    # "connections to MY localhost:5433 → through sb-prod → to 10.0.21.5:5432"
    psql -h localhost -p 5433 -U studybuddy        # now talks to the private database, encrypted

    A machine used only as the secure entry point like this is called a bastion host or jump host. (Cloud providers now also offer keyless alternatives — AWS Systems Manager Session Manager, GCP IAP — covered in AWS (coming soon) and Google Cloud (coming soon).)

    Fingerprint prompt: the first time you connect, SSH shows the server's key fingerprint and asks whether to trust it, then stores it in ~/.ssh/known_hosts. If it later changes, SSH shouts a warning: either the server was rebuilt, or someone is impersonating it. Don't blindly accept.

    Gotchas

    • Never share or commit your private key. If it leaks, remove its public key from every server's authorized_keys.

    • Disable password login on servers (PasswordAuthentication no) — bots try passwords on every internet-facing port 22 constantly.

    • Don't open port 22 to 0.0.0.0/0 in cloud firewalls unless you must (Networking 2: TCP, UDP and Ports Ch 11).

    Check yourself

    4 questions
    1

    In key-based SSH login, which key goes on the server, and which stays on your laptop?

    Show answer

    The public key goes on the server (authorized_keys); the private key stays on your laptop.

    2

    What does ~/.ssh/config save you?

    Show answer

    Typing host names, users and key files every time — you get short aliases.

    3

    Write the command to reach a private database 10.0.21.5:5432 through sb-prod, as localhost:5433.

    Show answer

    ssh -L 5433:10.0.21.5:5432 sb-prod.

    4

    What does it mean if SSH warns that the host key has changed?

    Show answer

    The server's identity is different from last time — it was rebuilt, or someone may be impersonating it.