This is a typical example of a trade-off between security and convenience. Luckily, there are a number of options. The most appropriate solution depends on the usage scenario and desired level of security.
Now the passphrase must be entered upon every login. While slightly better from a usability perspective, this has the drawback that ssh-agent
prompts for the passphrase regardless of ifwhether the key is to be used or not during the login session. Each new login also spawns a distinct ssh-agent
instance which remains running with the added keys in memory even after logout, unless explicitly killed.
ssh-ident
is a utility that can manage ssh-agent
on your behalf and load identities as necessary. It adds keys only once as they are needed, regardless of how many terminals, sshSSH or login sessions that require access to an ssh-agent
. It can also add and use a different agent and different set of keys depending on the host beingyou are connected to, or the directory sshssh
is invoked from. This allows for isolating keys when using agent forwarding with different hosts. It also allows to useusing multiple accounts on sites like GitHub.
keychain
keychain
is a small utility which manages ssh-agent
on your behalf and
allows allows the ssh-agent
to remain running when the login session ends. On subsequent logins, keychain
will connect to the existing ssh-agent
instance. In practice, this means that the passphrase must be be entered only during the first login after a reboot. On subsequent logins, the unencrypted key from the existing ssh-agent
instance is used. This can also be useful for allowing passwordless RSA/DSA authentication in cron
jobs without passwordless ssh-keys.
From a security point of view, ssh-ident
and keychain
are worse than ssh-agent
instances limited to the lifetime of a particular session, but they offer a high level of convenience. To improve the security of keychain
, some people add the --clear
option to their ~/.bash_profile
keychain invocation. By doing this, passphrases must be re-entered on login as above, but cron
jobs will still have access to the unencrypted keys after the user logs out. The keychain
wiki pagewiki page has more information and examples.
While it might seem like a straightforward idea to pass the passphrase to ssh-add
from a script, e.g. echo "passphrase\n" | ssh-add
, this is not as straighforwardstraightforward as it seems as ssh-add
does not read the passphrase from stdin
, but opens /dev/tty
directly for reading.
This can be worked around with expect
expect
, a tool for automating interactive applications. Below is an example of a script which adds a ssh-key using a passphrase stored in the script:
Note that as the passphrase is stored in plaintext in the script, from a security perspective, this is hardly better than having a passwordless ssh-key. If this approach is to be used, it is important to make sure that the expect
script containing the passphrase has proper permissions set to it, making it readable, writable, and runnable only by the key owner.