Loading SSH keys at KDE startup

It's really handy to have all my SSH authentication be passwordless, but in a secure way. In openSUSE, the ssh-agent is started for you automatically, but you still need to add your identities manually (and enter passphrases when you do this). That's a bit of a pain to do every time you login.

Here are some simple scripts and steps I use to set up my KDE session so that it will automatically load my SSH identities when I login.

There are three parts for this:

  1. Set up KDE's KWallet with a master password. KWallet will be remembering the passphrases for my SSH keys (as well as other passwords within KDE such as those for web sites visited with Konqueror and Chrome, or email passwords, SQL Connections and so on). This makes it so that I only have to remember the KWallet master password to open my wallet at login, not all the different passphrases (you should use a different passphrase for each SSH key pair)
  2. Start ssh-add at login, using the KDE password tool ksshaskpass to ask for the passphrases (and that will save them to my wallet)
  3. Automatically load all my SSH identities (I have several, for work and for different code hosting systems on the net: bitbucket and github)

Setting up KWallet

You should only need to do this once:

  1. Launch "KDE Wallet Configuration" (press Alt-F2 or whichever shortcun runs KRunner in your setup) type wallet and pick the configuration tool from the list
  2. Make sure Enable the KDE wallet subsystem is checked
  3. Launch Wallet Manager and create a new wallet if you need to, setting the passphrase. This is the one you need to remember and type every time you login to KDE

Setting up ssh-add

I use this script:

export SSH_ASKPASS=/usr/lib/ssh/ksshaskpass

Save this script to a location you keep your shell scripts, chmod +x it so it is executable. Then, it needs to be run automatically at KDE Startup. For this, you add it to the KDE Autostarts.

  1. Launch the "Autostart" KDE Control Module (Alt-F2, Autostart)
  2. Add a new Script File linking to where you saved the above script

From now on, every time you start KDE, it'll run ssh-add and set it's password tool to be the KDE graphical password tool, ksshaskpass. That in turn uses KWallet for password storage, so if you need a password that's in the wallet, it'll retrieve it from there.

Adding my SSH identities at login

I use a second script:

# Finds all private key files (that are protected, based on mode bits) 
# and adds each to ssh agent
for filename in ~/.ssh/*; do
  perms=$(stat $filename|sed -n '/^Access: (/{s/Access: (\([0-9]\+\).*$/\1/;p}')

  filetype=$(file $filename)

  if [[ "$perms" == "0600" && "$filetype" == *"private key" ]]; then
    ssh-add $filename

This one will pick out all the private keys from my SSH directory ~/.ssh, adding them to my SSH agent's keychain.

Add this to Autostart as another Script File. I've saved it as ~/.ssh/addkeys.

You need to chmod +x addkeys and run it once to add all the keys and enter their passphrases (so that they get added to your Wallet).

Once the script has been run, whenever I need to login to an SSH session, if the identity is already in the agent's chain, I can get in without needing to type the passphrase.

So now that my wallet is set up, the two scripts are in place at Autostart, and I've run the addkeys script at least once to add the passphrasses to my wallet, I never need to enter passwords or passphrases when SSHing again (unless keys change or it's a new host).