5

A password prompt is offered to unlock the GNOME Keyring when I login to my user account. I was wondering how to lock the keyring back in the same session it was unlocked? (I understand that I can logout and then login again for the same effect)

It may be helpful if, for some reason(s), I feel of some suspicious activity and want to first block all programs for further accessing keyring before I investigate the suspicious activity.

Also, is there anyway to just lock the GUI of GNOME Keyring (Seahorse)?

5 Answers 5

1

One thing you could do if some suspicious activity takes place, is to kill the gnome-keyring-daemon like so :

kill -9 $(pgrep gnome-keyring-d)

You could simply do a simple script to make it automatically:

#!/bin/bash
case $1 in
    hibernate)
        pkill gnome-keyring-d
        ;;
    suspend)
        pkill gnome-keyring-d
        ;;
    thaw)
        ;;
    resume)
        /usr/bin/gnome-keyring-daemon --daemonize --login
        ;;
    *)  echo "Somebody is calling me totally wrong."
        ;;
esac

source

1
  • I was hoping for more cleaner way to do this. Nonetheless your answer serves the purpose. Sorry about late reply.
    – cbun
    Commented Jan 20, 2014 at 19:42
4

You only need to call gnome_keyring_lock_all_sync().

Here's a simple program which does that:

lock-keyring.c:

#include <stdio.h>
#include <gnome-keyring.h>

int main() {
    GnomeKeyringResult lock_result = gnome_keyring_lock_all_sync();
    if (lock_result == GNOME_KEYRING_RESULT_OK) {
        printf("Successfully locked\n");
        return 0;
    } else {
        printf("Error locking keyring: %d\n", lock_result);
        return 1;
    }
}

Compile with cc lock-keyring.c -o lock-keyring -Wall $(pkg-config gnome-keyring-1 --cflags --libs)

8
  • This looks very useful indeed, thanks! This is slightly off-topic but how would I get the necessary dependencies so I can build this goodie? I tried sudo apt build-dep gnome-keyring and apt source gnome-keyring; this doesn't seem to bring the right headers etc. Cheers!
    – sxc731
    Commented Aug 12, 2017 at 13:39
  • @sxc731 Glad you find it useful! ☺ The packages needed will vary for each distro, in your Debian-based case, you are probably missing libgnome-keyring-dev
    – Ángel
    Commented Aug 14, 2017 at 19:12
  • @Ángel This works when executed as command on terminal, but it isn't working on cron/cronie. Does anyone know why or have similar issue? Commented May 9, 2019 at 18:38
  • 1
    @alexandre1985 cron is running the command out of another session. You will probably need to set an environment variable (DBUS_SESSION_BUS_ADDRESS?) so that it connects to the running keyring. You will have to either guess it (eg. looking on the path where it is created), or save the environment variables at the X session, and load them before locking the script.
    – Ángel
    Commented May 16, 2019 at 21:13
  • @Ángel Thank you soooo much! :) setting DBUS_SESSION_BUS_ADDRESS (with env) on my command inside of cron, did it! Thank you so much :D :D Commented May 18, 2019 at 15:10
4

The Gnome keyring can be locked via dbus:

dbus-send --dest=org.gnome.keyring --print-reply /org/freedesktop/secrets org.freedesktop.Secret.Service.LockService
# or with qdbus
qdbus org.gnome.keyring /org/freedesktop/secrets org.freedesktop.Secret.Service.LockService

Source: https://github.com/Intika-Linux-Apps/Gnome-Keyring-Tools/issues/1#issuecomment-443358508

1
  • This seems to be behaving in odd ways; LockService has been removed from latest specifications, replaces with a Lock call that takes the Collections to lock as parameter. Commented Feb 12, 2021 at 1:15
2

Gnome Keyring GUI application is called Seahorse and shows as "Passwords and Keys" in the menu. Seahorse can manage multiple collections and you can lock them directly from the GUI (they appear under Passwords in the sidebar) simply by clicking on the padlock icon next to each of them. By default you will have only the Login collection.

If you don't see the sidebar, click on View then check By Keyring to display it.


If you wish to do this from shell, ex. automatically, this python oneliner will lock all collections, loosely based on what seahorse is doing when locking keys:

python -c 'import dbus; bus=dbus.SessionBus(); bus.call_blocking("org.freedesktop.secrets", "/org/freedesktop/secrets", "org.freedesktop.Secret.Service", "Lock", "ao", [bus.call_blocking("org.freedesktop.secrets", "/org/freedesktop/secrets", "org.freedesktop.DBus.Properties", "GetAll", "s", ["org.freedesktop.Secret.Service"]).get("Collections", [])])'

Expanded, it would look like this:

#!/usr/bin/env python

import dbus

bus = dbus.SessionBus()

# Get Collections array
collections = bus.call_blocking(
    "org.freedesktop.secrets", "/org/freedesktop/secrets",
    "org.freedesktop.DBus.Properties", "GetAll",
    "s", ["org.freedesktop.Secret.Service"]
).get("Collections", [])

# Lock all Collections (return value is an array of locked collections)
bus.call_blocking(
    "org.freedesktop.secrets", "/org/freedesktop/secrets",
    "org.freedesktop.Secret.Service", "Lock",
    "ao", [collections]
)
1

In the main GUI of Gnome keyring (at least in the current version, which is seahorse 3.36), the user can right-click a keyring to get the context menu where an option to lock the keyring is available. It is also possible to create more keyrings in addition to the default one (called "login") and store passwords in different keyrings, so that the passwords are not available all at once.

You must log in to answer this question.

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