Skip to content

Instantly share code, notes, and snippets.

@martin-sicho
Created March 7, 2024 10:57
Show Gist options
  • Save martin-sicho/957fa4fb8a2a8fe7aff1e249ff54c2b0 to your computer and use it in GitHub Desktop.
Save martin-sicho/957fa4fb8a2a8fe7aff1e249ff54c2b0 to your computer and use it in GitHub Desktop.
Quick Guide to Add SSH Keys Automatically on Startup in KDE

Quick Guide to Add SSH Keys Automatically on Startup in KDE

This always takes me a while to figure out when installing a new system so here is a foolproof guide to do this right once and for all. Kudos to all the people in this Manjaro thread. However, this should work on any system with KDE and systemd.

Step 1

Make sure to install required packages:

sudo pacman -Syu --needed kwallet5 ksshaskpass kwalletmanager kwallet-pam signon-kwallet-extension

Step 2

Create a new shell script named ssh-askpass.sh in /etc/profile.d/:

#!/bin/sh

# in /etc/profile.d/ssh-askpass.sh
export SSH_ASKPASS=/usr/bin/ksshaskpass
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR"/ssh-agent.socket

It needs to be at this location, adding this to user directories in $HOME did not seem to have the desired effect. But perhaps adding to $HOME/.profile could solve this and root privelages might not be needed for this step.

Step 3

Create an ssh-agent systemd service for your local user:

mkdir -p ~/.config/systemd/user
touch ~/.config/systemd/user/ssh-agent.service

Contents of ~/.config/systemd/user/ssh-agent.service:

[Unit]
Description=SSH agent (ssh-agent)

[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
Environment=DISPLAY=:0
ExecStart=ssh-agent -D -a $SSH_AUTH_SOCK
ExecStop=kill -15 $MAINPID

[Install]
WantedBy=default.target

Then we just need to enable the service and start the service:

systemctl --user daemon-reload
systemctl --user enable ssh-agent.service
systemctl --user start ssh-agent.service # just to check it is working

Step 4

Create a desktop entry to automatically add your keys:

touch ~/.config/autostart/ssh-add.desktop

with contents:

[Desktop Entry]
Exec=ssh-add -q ~/.ssh/key1 ~/.ssh/key2 ~/.ssh/key3 < /dev/null
Name=ssh-add
Type=Application

Step 5

Reboot and you should be prompted for your passwords at the next login:

sudo systemctl reboot

If there is no prompt after login, keys can also be stored in the wallet manually:

ssh-add -q /path/to/key < /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment