| title | Linux Command Tutorial: ssh-add | ||||
|---|---|---|---|---|---|
| date | 2026-09-12 00:00:00 +0000 | ||||
| categories |
|
||||
| tags |
|
||||
| draft | false | ||||
| slug | linux-ssh-add-tutorial | ||||
| description | Authoritative reference tutorial for ssh-add (OpenSSH), detailing private key loading, agent locking, hardware token interaction, and lifetime enforcement. | ||||
| upstream_suite | openssh | ||||
| upstream_version | OpenSSH 10.5 | ||||
| posix_standard | None | ||||
| research_date | 2026-09-12 |
The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.
Upstream:
OpenSSH 10.5| POSIX:None (OpenSSH standard)| Safety Tier:safe-read-only| Scope:Authentication key loading, cache auditing & agent locking
ssh-add is the user-space utility used to inspect, load, and manage private keys in an active ssh-agent(1) process. It decrypts private key files using passphrases and uploads the unencrypted key data to the agent daemon memory.
- Upstream Project & Provenance: Core client utility in OpenSSH (
openssh-clients). - Portability & Standards Baseline: Proprietary client to the OpenSSH authentication agent protocol; not defined in POSIX.1-2024.
- Target Research Implementation: Audited against OpenSSH 10.5 (
ssh-add(1)). - Applicability & Lifecycle: The daily companion to
ssh-agent, responsible for caching identities, setting time-to-live restrictions, locking the agent, and querying loaded fingerprints.
ssh-add [-cDdLlqSTtXx] [-E fingerprint_hash] [-e pkcs11]
[-K] [-k] [-M maxsign] [-m minsign] [-s pkcs11] [file ...]When invoked without arguments, ssh-add searches for default identity files in ~/.ssh/:
id_ed25519id_ecdsaid_rsa
It prompts the user for passphrases on the controlling TTY (or via SSH_ASKPASS) and transfers the keys to ssh-agent via SSH_AUTH_SOCK.
| Flag | Description | Upstream Note |
|---|---|---|
-l |
Lists fingerprints of all currently loaded identities. | Standard query |
-L |
Lists public key parameters of all loaded identities. | Full public keys |
-d |
Delete the specified identity key from the agent. | Selective cleanup |
-D |
Delete all identities from the agent memory. | Full purge |
-x |
Lock the agent with a password. | Security boundary |
-X |
Unlock the agent with the previously set password. | Unlocking |
-t life |
Set a maximum lifetime on added identities (e.g. 2h, 1800). |
Timeout restriction |
-c |
Require explicit confirmation (via ssh-askpass) before each use. |
Zero-trust protection |
-K |
Load resident keys from a FIDO2 hardware authenticator token. | Hardware security |
| Task / Scenario | Command | Key Flags / Behavior |
|---|---|---|
| Add default SSH keys | ssh-add |
Loads ~/.ssh/id_* keys after passphrase entry |
| Add specific private key | ssh-add ~/.ssh/id_ed25519 |
Adds targeted private key file |
| List loaded key fingerprints | ssh-add -l |
Lists key lengths, SHA256 hashes, and comments |
| List loaded full public keys | ssh-add -L |
Prints complete OpenSSH public key strings |
| Add key with expiration | ssh-add -t 1h ~/.ssh/id_prod |
-t sets lifetime limit (e.g. 1 hour) |
| Add key with confirmation | ssh-add -c ~/.ssh/id_root |
-c requires user prompt before each use |
| Remove specific key | ssh-add -d ~/.ssh/id_ed25519.pub |
-d removes target identity from agent |
| Purge all identities | ssh-add -D |
-D clears all cached keys from memory |
| Lock agent with password | ssh-add -x |
-x locks agent memory until -X unlock |
ssh-addSample terminal output:
Enter passphrase for /home/admin/.ssh/id_ed25519:
Identity added: /home/admin/.ssh/id_ed25519 (admin@corp.example.com)ssh-add -lSample terminal output:
256 SHA256:7fK2m9W81XzpBqLa39XjN1sK21vL3p0kZqA7w9eF1m8 admin@corp.example.com (ED25519)
ssh-add -DSample terminal output:
All identities removed.
Loading a production key that automatically purges from memory after 1 hour (3600 seconds):
ssh-add -t 1h ~/.ssh/id_prod_ed25519Sample terminal output:
Enter passphrase for /home/admin/.ssh/id_prod_ed25519:
Identity added: /home/admin/.ssh/id_prod_ed25519 (id_prod_ed25519)
Lifetime set to 3600 secondsLoading a high-privilege key that prompts an interactive confirmation dialog every time a signature is requested:
ssh-add -c ~/.ssh/id_root_ed25519- Every time
sshattempts to use this key,ssh-askpasspops up asking: "Allow use of key /home/admin/.ssh/id_root_ed25519?".
Locking the agent memory before leaving a physical workstation:
ssh-add -xSample terminal output:
Enter lock password:
Enter lock password again:
Agent locked.Unlocking upon return:
ssh-add -XSample terminal output:
Enter lock password:
Agent unlocked.OpenSSH supports discovering keys stored directly on hardware tokens (such as Yubikey 5 series):
ssh-add -KSample terminal output:
Enter PIN for authenticator:
Identity added from token: /home/admin/.ssh/id_ed25519_sk_rk (FIDO token)
In OpenSSH 8.9+, ssh-add allows restricting the maximum number of signatures a key can produce before being purged from agent memory:
ssh-add -M 5 ~/.ssh/id_deploy- The key is invalidated and purged after 5 authentication attempts, preventing persistent exploitation.
| Exit Code | Condition |
|---|---|
0 |
Key successfully added, queried, deleted, or agent locked. |
1 |
Specified key could not be loaded, incorrect passphrase, or agent locked. |
2 |
Could not contact authentication agent (SSH_AUTH_SOCK invalid or unset). |
SSH_AUTH_SOCK: Required. If unset,ssh-addexits with code 2:Could not open a connection to your authentication agent.SSH_ASKPASS: Used to display graphical passphrase entry prompts when no terminal is attached.
Warning
Silent Background Key Abuse: Any local process running as your user ID can connect to SSH_AUTH_SOCK and generate authentication signatures without knowing your private key passphrase.
Adding sensitive keys with the -c confirmation flag completely mitigates silent background abuse by requiring an explicit graphical or console confirmation dialog before any signature is issued.
Note
Agent Locking Without Key Purge: ssh-add -x locks the agent with a temporary password. The agent retains the decrypted keys in RAM but blocks all signature requests until unlocked with ssh-add -X.
-
Always Set Lifetimes on Administrative Keys:
[!TIP] Guidance: Load keys using
ssh-add -t <duration>(e.g.,ssh-add -t 2h). Authoritative Justification: OpenSSH manual states that identities with a set lifetime are automatically removed by the agent upon expiration. -
Use
-cfor Sensitive Production Keys:[!IMPORTANT] Guidance: Add bastion and root deployment keys with
ssh-add -c. Authoritative Justification: Prevents rogue background scripts from issuing unauthorized signatures through the agent socket. -
Lock the Agent (
ssh-add -x) on Inactive Workstations:[!TIP] Guidance: Integrate
ssh-add -xinto desktop screen locker hooks. Authoritative Justification: Freezes signature generation while preserving loaded keys in memory without requiring full passphrase re-entry. -
Purge Identities When Finished:
[!TIP] Guidance: Run
ssh-add -Dat the end of maintenance windows. Authoritative Justification: Clears in-memory cryptographic credentials.
- OpenSSH ssh-add(1) Manual: OpenBSD Manual Pages. https://man.openbsd.org/ssh-add
- OpenSSH ssh-agent(1) Manual: OpenBSD Manual Pages. https://man.openbsd.org/ssh-agent
- OpenSSH 10.5 Release Notes: Official Project Portal. https://www.openssh.com/releasenotes.html