skip to content

Clients & Automation

Unattended transfers use sftp -b batch files, scp (SFTP-backed since OpenSSH 9.0) or libraries like Paramiko and JSch. Interviewers probe host-key pinning, error handling and idempotent retries.

on this pageshow

explore

questions

5

For a CI job pushing files with OpenSSH's sftp, how do you pin the SFTP server's host key and what do BatchMode=yes and StrictHostKeyChecking=yes add?

level: middleimportance: must knowfreq 45%

answer

  1. ephemeral runners start with nothing
  2. prompts nobody can answer
  3. first contact is the attack window
  4. a known_hosts file you ship yourself
  5. UserKnownHostsFile pointing at it

basics

~20 s

Ship a known_hosts line for the server, obtained from its operator or checked against their fingerprint, and point UserKnownHostsFile at it; StrictHostKeyChecking=yes refuses any other key and BatchMode=yes turns every prompt into an immediate failure.

solid answer

~40 s

A CI runner usually starts with an empty `~/.ssh/known_hosts`, so the first connection is the moment an impostor could answer. I get the server's public host key out of band — from the partner, or a scan checked against the fingerprint they publish — and keep that line in the repository or a CI variable, since it is public. The job then runs `sftp -o UserKnownHostsFile=./ci_known_hosts -o StrictHostKeyChecking=yes -o BatchMode=yes …`. `StrictHostKeyChecking=yes` never adds keys and refuses a changed one; `BatchMode=yes` disables password prompts and host-key confirmation, so nothing waits for a human. A changed key then fails the job loudly, and the fix is to verify the new key with the partner, never to switch checking off.

code

bash · 9 lines
bash
cat > ci_known_hosts <<'EOF'
[sftp.bank.example.com]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...
EOF
ssh-keygen -l -f ci_known_hosts
sftp -b push.batch -P 2222 \
  -o BatchMode=yes \
  -o StrictHostKeyChecking=yes \
  -o UserKnownHostsFile=./ci_known_hosts \
  -i "$DEPLOY_KEY" [email protected]

go deeper

for a junior

Recall that an unattended job must not answer the host-key question itself, and that the server's key should come from a file you supply, not from the first connection.

for a middle

Explain the StrictHostKeyChecking values, what BatchMode=yes disables, and why accept-new and a scan at job start both reduce to trust-on-first-use on an empty runner.

for a senior

Show the operational side: obtaining the key out of band, the bracketed port format, rotating with two lines, and treating a changed-key failure as an incident, not a nuisance.

for a principal

Decide where pinned host keys live across many partner jobs: per-partner files under review, who may change them, and how rotation notices from partners reach the right team.

## Why CI is a special case When an SSH client such as OpenSSH's `sftp` connects, the server proves its identity with a **host key**. The client compares that key with the entries in its `known_hosts` files. On a laptop this list builds up over months; on a CI runner it is usually **empty at the start of every job**, because runners are created fresh. Every run is therefore a first contact — and first contact is the one moment when an impostor in the network path (a hijacked DNS name, a hostile proxy) could present its own key and receive the settlement files. ## What the client does by default OpenSSH's client option `StrictHostKeyChecking` decides what happens with a key it has not seen: | Value | Unknown host | Changed key for a known host | |---|---|---| | `ask` (default) | add only after the user confirms | refuse | | `accept-new` | add automatically | refuse | | `yes` | never add; refuse | refuse | | `no` / `off` | add automatically | allow, subject to some restrictions | `BatchMode=yes` disables "password prompts and host key confirmation requests". Combined with the default `ask`, an unknown key on an empty runner therefore fails the connection at once rather than waiting — which is when someone, under time pressure, reaches for the wrong fix. ## The tempting fixes that remove the check - **`StrictHostKeyChecking=no`** accepts any new key and even lets changed keys through. The session is still encrypted — to whoever answered. - **`accept-new`** sounds safer, and on a long-lived machine it does catch later changes. On an ephemeral runner every run is a first connection, so it trusts whatever key answers, every time. - **`ssh-keyscan` at the start of the job** fetches the key from the same network path the attacker would sit on. The `ssh-keyscan` man page says it directly: building a known-hosts file from keys you have not verified leaves you open to man-in-the-middle attacks. All three turn host-key checking into trust-on-first-use, repeated on every run. ## Pinning the key properly 1. **Obtain the key out of band.** Ask the partner for the server's public host key or its fingerprint through a channel the network path cannot tamper with. If you scan it yourself, compare the scanned key's fingerprint (`ssh-keygen -l -f file`) with the one they published. 2. **Write one `known_hosts` line.** Format: host pattern, key type, base64 key. A non-standard port is written as `[sftp.bank.example.com]:2222`. 3. **Store it with the job.** The line is a **public** key, not a secret; what matters is its integrity, so keep it in the repository or a protected CI variable where changes are reviewed. The deploy account's private key is the secret, and lives in the CI secret store. 4. **Point the client at it** with `UserKnownHostsFile`, and set `StrictHostKeyChecking=yes` and `BatchMode=yes`. ```bash sftp -b push.batch -P 2222 \ -o BatchMode=yes \ -o StrictHostKeyChecking=yes \ -o UserKnownHostsFile=./ci_known_hosts \ -i "$DEPLOY_KEY" [email protected] ``` With this in place, an impostor's key matches nothing in the file and `StrictHostKeyChecking=yes` refuses it; the job fails before a single byte of payload leaves the runner. ## Living with a pinned key - **Rotation.** The `known_hosts` format permits several lines or different host keys for the same name. Ask the partner for the new key before they switch, add it beside the old one, and remove the old line once the switch is done. - **A changed-key failure is a signal, not noise.** Confirm with the partner whether they rebuilt or rotated the server; update the file only after verifying the new fingerprint. Clearing a stale entry on one workstation is a separate, everyday procedure on a single host. - **One file per partner** keeps a compromised or rotated entry from touching other jobs, and makes the review of each change obvious. ## Why both options, not one `StrictHostKeyChecking=yes` is the policy: no key is trusted unless it is already listed. `BatchMode=yes` is the guarantee that nothing waits for a human: password prompts, key passphrases and host-key confirmations all fail immediately. Together they make the job either connect to the server you pinned or fail fast with a clear error.

  • Why is StrictHostKeyChecking=accept-new not enough on a CI runner?
    `accept-new` adds unknown keys automatically and only refuses keys that changed. On a runner that starts empty, every connection is a first contact, so it accepts whatever key answers on every run. It protects a long-lived machine against later changes; it gives an ephemeral runner no protection at all.
  • The partner will rotate the server's host key next month. How does the job keep working?
    Get the new public key or fingerprint from the partner ahead of the switch, verify it, and add it as a second line for the same host; OpenSSH permits several keys for one name. Both keys match during the overlap. After the partner confirms the switch, remove the old line in a reviewed change.
  • Is the pinned known_hosts line a secret that belongs in the CI secret store?
    No. It holds the server's public key, which anyone connecting can see. What matters is integrity: whoever can edit it decides which server the job trusts, so keep it under review in the repository or a protected variable. The deploy account's private key is the real secret.

  • StrictHostKeyChecking=no is fine because the session is still encrypted
  • Running ssh-keyscan at the start of each job pins the host key
  • accept-new on a fresh CI runner protects as well as a pinned file
  • BatchMode=yes makes ssh accept unknown host keys automatically
  • A changed host key in CI is fixed by switching checking off
open as a page

In OpenSSH's sftp, how does batch mode with sftp -b change error handling, and how should a CI job's batch file use that?

level: middleimportance: must knowfreq 40%

basics

~20 s

With sftp -b, OpenSSH's sftp stops at the first failing file command (put, rename, rm, mkdir and others) and exits non-zero, so the CI step fails; a leading '-' tolerates one line's failure, and authentication must need no typing.

open as a page

A Paramiko SFTP upload script calls set_missing_host_key_policy(AutoAddPolicy()); what does that do, and how should Paramiko, JSch and ssh2-sftp-client check host keys instead?

level: juniorimportance: should knowfreq 30%

basics

~20 s

AutoAddPolicy makes Paramiko trust and store any host key it has not seen, so an impostor is accepted silently; load a pinned file and keep the default RejectPolicy, use JSch's setKnownHosts with StrictHostKeyChecking yes, and give ssh2-sftp-client a hostVerifier.

open as a page

Your SFTP upload job sometimes leaves a bank's consumer reading half-written settlement files; how do you make each drop atomic and each retry safe?

level: seniorimportance: should knowfreq 28%

basics

~20 s

Upload under a temporary name the consumer ignores, close it, then rename it to the final name in one request; before retrying, check whether the final file already exists, because plain SFTP rename fails on a clash and posix-rename silently overwrites.

open as a page

After a CI image moved to a current OpenSSH, a script's scp to an older partner server fails; why, and when is scp -O the right fix?

level: seniorimportance: nice to knowfreq 14%

basics

~20 s

Since OpenSSH 9.0, scp speaks SFTP by default, so it fails on servers without an SFTP subsystem, on shell-style inner quoting, and on ~user paths an old sftp-server cannot expand; -O restores the legacy protocol for those servers only.

open as a page