3

I have a bunch of keys added to the ssh-agent, keys to servers, repositories, etc.

Is there a way to remove all the ssh keys added to ssh-agent when the user locks the system?

My system right now is Ubuntu Desktop 16.04 with GNOME 3

1 Answer 1

1

You can watch for the events in DBUS and on lock call ssh-add -D to remove all keys from the agent:

#!/bin/bash
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
  while read x; do
    case "$x" in 
      *"boolean true"*) echo SCREEN_LOCKED; ssh-add -D;;
      *"boolean false"*) echo SCREEN_UNLOCKED;;  
    esac
  done

But you need to export the SSH_AUTH_SOCK environment variable to this script, so it would be able to access your agent.

You can store the above into the script clean_lock.sh, set execute bits (chmod u+x clean_lock.sh) and run the script in background ./clean_lock.sh &.

3
  • I have checked that answer. But how to run a script on background that keeps listening to the state changed.
    – Starx
    Nov 1, 2016 at 13:07
  • Oh hang on, that worked.
    – Starx
    Nov 1, 2016 at 13:09
  • I tried this answer as it said it was what worked of 16.04
    – Starx
    Nov 1, 2016 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .