Guide · Deploy

Send credentials to your Linux server

A service on your own Linux server needs API keys and app passwords. Pasting them into files over ssh leaves no record of what's there, and the copy on your Mac and the one on the server drift apart.

In shortGive the server a script that stores one credential from standard input, let your account run it with sudo and no password, name each Latchkey item ADAPTER.ACCOUNT, and link the environment with lk remote link ENV ssh host=… script=…. Every lk push asks you with Touch ID, then sends each value over ssh on standard input.

How it works

The values stay in your Mac's keychain, where you can see, rotate and audit them. The server keeps its own copy, one root-only file per credential, which the service loads at start, for example with systemd's LoadCredential=. Latchkey's job is the delivery: it runs your server's script once per item, over your own ssh connection and keys, with the value on standard input, so it never appears in a command line, an environment variable or your shell history.

The service never talks to Latchkey, and keeps running with its last values while your Mac is off.

Set it up

  1. Give the server a credential script

    Latchkey runs it as sudo -n bash SCRIPT ADAPTER ACCOUNT --stdin and sends the value followed by one newline. The script must read at most 64 KiB, strip that one newline, store the value, and print stored ADAPTER.ACCOUNT when it has; anything else counts as a failure. A minimal one, with myapp as the example service:

    /srv/myapp/add-credential.sh
    #!/bin/bash
    set -euo pipefail
    [[ $# -eq 3 && $3 == --stdin && ! -t 0 ]] || { echo "usage: add-credential.sh ADAPTER ACCOUNT --stdin" >&2; exit 64; }
    name="$1.$2"
    [[ $name =~ ^[A-Za-z0-9][A-Za-z0-9_-]*\.[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || { echo "bad name" >&2; exit 64; }
    value=$(head -c 65536; printf x); value=${value%x}; value=${value%$'\n'}
    [[ -n $value ]] || { echo "empty value" >&2; exit 1; }
    dir=/etc/myapp/credentials
    umask 077
    tmp=$(mktemp "$dir/.$name.XXXXXX")
    printf %s "$value" > "$tmp" && mv "$tmp" "$dir/$name"
    systemctl restart myapp
    echo "stored $name"

    Point the service at those files, for example with a LoadCredential=NAME:/etc/myapp/credentials/NAME line for each credential in its unit, so it reads them from $CREDENTIALS_DIRECTORY.

  2. Let your account run it without a password

    A push has no terminal, so sudo can't ask for one. Allow exactly that script, and check from your Mac:

    /etc/sudoers.d/myapp-credentials
    you ALL=(root) NOPASSWD: /usr/bin/bash /srv/myapp/add-credential.sh *
    Your Mac
    ssh myserver sudo -n -l bash /srv/myapp/add-credential.sh

    It prints the command when the rule is in place. myserver is the host as named in your SSH config.

  3. Store each credential under its name

    Name items ADAPTER.ACCOUNT: Latchkey splits at the first dot. Both parts start with a letter or digit and use letters, digits, _ and -; the account may also contain dots.

    Terminal
    lk set myapp/production/bluesky.someone.bsky.social --kind token

    You're asked for the value without it being shown, and saving asks for Touch ID.

  4. Link the environment to the server

    Terminal
    lk remote link myapp/production ssh host=myserver script=/srv/myapp/add-credential.sh

    Linking asks once. The host and script may hold only characters a shell reads literally, and a link synced from another Mac is checked again before each push.

  5. Push, and approve with Touch ID

    Terminal
    lk push myapp/production --only myapp/production/bluesky.someone.bsky.social

    The approval panel names the server, the script and every item. A server counts as production, because its script restarts the service, so tick I mean to change production, then touch the sensor. Each credential the server confirms prints as pushed.

Keep it current

Change a value in Latchkey and push again; the script replaces the file and restarts the service. lk remote status myapp/production lists items never pushed to the server and items changed since they were. The server can't list what it holds, so that comes from Latchkey's own record of what it pushed, not from the server.

What this doesn't do

The script contract has every rule in one place, and Push env vars to your deploy platforms covers Vercel, GitHub, Railway and Fly.

Related