brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ssh-add -t makes an SSH key vanish from the agent after a set time

Network and access
~/tips/tip-security-ssh-add-t-key-lifetime

ssh-add -t 1h ~/.ssh/id_ed25519

Loads an SSH key into the agent for a limited time only.

A key that sits in the ssh-agent all day can be used by anything that can reach the agent. I like giving it an expiry time.

The -t option sets a maximum lifetime when you add an identity:

ssh-add -t 1h ~/.ssh/id_ed25519

The man page says the lifetime can be given in seconds or in the time format of sshd_config(5), so 3600 and 1h are the same. After that time the agent drops the key, and the next use needs the passphrase again.

To see what the agent holds right now, list the fingerprints:

ssh-add -l

To empty it before you walk away, delete all identities:

ssh-add -D

If ssh adds your keys automatically, the AddKeysToAgent option in ~/.ssh/config accepts a time interval as well. The man page says the key is then removed from the agent after that lifetime, so you do not have to type the flag each time:

Host *
  AddKeysToAgent 1h

There is a related option for a different worry. ssh-add -c marks the key so that every use must be confirmed before it is used for authentication, and the man page says the confirmation goes through ssh-askpass(1).

The caveat is the nuisance: a short lifetime means more passphrase prompts, so pick a time that fits your working day. It also limits only how long the key is loaded, not what a program can do with it while it is.

Further reading: ssh-add man page, ssh_config man page

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link