Skip to content
shellhacks.dev
SSH and Networking

Add a key to the agent

Add a key to the agent with ssh-add and set a lifetime with -t, so the decrypted key is dropped after an hour without any further command.

1 min read
Summarize in AI

You want to type a key's passphrase once and have the agent forget the key later, rather than keep it loaded all day.

Shell
ssh-keygen -q -t ed25519 -f ./demo_key -N '' -C [email protected]
eval "$(ssh-agent -s)" >/dev/null
ssh-add -t 3600 ./demo_key
ssh-add -l | awk '{print $1, $3, $4}'
ssh-add -D
ssh-add -l
kill "$SSH_AGENT_PID"
Text
Identity added: ./demo_key ([email protected])
Lifetime set to 3600 seconds
256 [email protected] (ED25519)
All identities removed.
The agent has no identities.

ssh-add hands the decrypted key to the running agent, -t sets a lifetime in seconds after which the agent removes it, and -l lists the loaded keys. -D removes all of them. The options are in the ssh-add manual. The agent talks to clients over the socket named in SSH_AUTH_SOCK, so only processes that can read that socket use the key.

The eval of the agent output is needed once per shell, and a script that starts an agent must kill it, as the last line does. A long-lived agent forwarded with ssh -A lets root on the remote host use your keys while you are connected, so prefer ProxyJump where it fits.

Share

More snippets

SSH and Networking

Create an Ed25519 key

Create an Ed25519 key pair with ssh-keygen -t ed25519. Name the file with -f, label it with -C, and set a passphrase instead of the empty one used here.

1 min read
SSH and Networking

Show a key's fingerprint

Show the fingerprint of a key with ssh-keygen -lf. The default hash is SHA256, and -E md5 prints the older format that some web consoles still list.

1 min read
SSH and Networking

Change a key's passphrase

Change or remove the passphrase of an existing key with ssh-keygen -p. Pass -P and -N only in a throwaway demo, since they put secrets in the process list.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.