DEV Community

vast cow
vast cow

Posted on

Unlocking GNOME Keyring on a Remote Machine via X11 Forwarding

A memo on unlocking the GNOME Keyring on a Linux server connected via SSH, using Seahorse through X11 Forwarding.

Also documented are how to unlock it with secret-tool, and how to use the SSH agent provided by GNOME Keyring / GCR from an SSH session.

Environment

Using SSH's X11 Forwarding, display the Seahorse and GNOME Keyring password prompts launched on the remote side on the local side.

First, establish the SSH connection.

ssh -XY server
Enter fullscreen mode Exit fullscreen mode

After connecting, also reflect the X11 environment variables into the D-Bus / systemd user session.

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

Unlocking the GNOME Keyring

Using Seahorse

Launch Seahorse.

seahorse
Enter fullscreen mode Exit fullscreen mode

Once Seahorse is displayed, select

Passwords → Login → Unlock

and enter the GNOME Keyring password.

This unlocks the Login keyring.

Using secret-tool

Instead of launching Seahorse, you can also request an unlock from secret-tool.

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

If the keyring is locked, a password prompt for unlocking will appear; enter the password there.

The search results themselves are not needed, so standard output is discarded to /dev/null.

To display the prompt via X11 Forwarding, run the following first.

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

Therefore, if you do not need to open the GUI Seahorse, just the following steps suffice.

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

Using the SSH agent

To use the SSH agent on the GNOME Keyring / GCR side, point SSH_AUTH_SOCK to the GCR socket.

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

After setting this, verify the keys recognized by the agent.

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

A series of operations looks, for example, like this.

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

If using Seahorse, replace the unlock part as follows.

seahorse
# Passwords → Login → Unlock
Enter fullscreen mode Exit fullscreen mode

If you use the GCR SSH agent every time, you may add the following to your shell's configuration file.

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

However, if you are also using another ssh-agent or agent forwarding, note that this will overwrite the existing SSH_AUTH_SOCK.

Why dbus-update-activation-environment is needed

When using SSH's X11 Forwarding, a DISPLAY like the following is set in the SSH session.

localhost:10.0
Enter fullscreen mode Exit fullscreen mode

On the other hand, GUI prompts around GNOME Keyring may be launched via D-Bus or the systemd user session.

Therefore, even if DISPLAY is correctly set in the SSH shell, processes launched via D-Bus may not recognize the SSH X11 display.

So, run

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

to pass the current SSH session's DISPLAY and XAUTHORITY to the activation environment as well.

Procedure Summary

When using Seahorse.

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

seahorse
# Passwords → Login → Unlock

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

When using secret-tool.

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Notes

Check whether the GNOME Keyring daemon is running.

pgrep -af gnome-keyring-daemon
Enter fullscreen mode Exit fullscreen mode

If the behavior is off, restart the user service.

systemctl --user stop gnome-keyring-daemon.service
systemctl --user start gnome-keyring-daemon.service
Enter fullscreen mode Exit fullscreen mode

To watch the logs in real time, use the following.

journalctl --user-unit gnome-keyring-daemon -fe
Enter fullscreen mode Exit fullscreen mode

Running this log in another terminal while operating Seahorse or secret-tool makes it easier to see what is happening on the daemon side.

For checking the SSH agent side, the following also works.

echo "$SSH_AUTH_SOCK"
ls -l "$XDG_RUNTIME_DIR/gcr/ssh"
ssh-add -l
Enter fullscreen mode Exit fullscreen mode

The expected socket is:

$XDG_RUNTIME_DIR/gcr/ssh
Enter fullscreen mode Exit fullscreen mode

Supplementary Notes

ssh -X is regular X11 Forwarding, while ssh -Y is trusted X11 Forwarding.

In this case, operation was confirmed with

ssh -XY server
Enter fullscreen mode Exit fullscreen mode

Trusted X11 Forwarding grants remote applications broader permissions than regular -X, so it is assumed to be used only with trusted servers.

Also,

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

updates the activation environment of that user's D-Bus / systemd user session.

In environments where the same user is also using a local GUI session concurrently, be aware that this may affect where GUI applications are displayed.

Similarly,

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

switches the SSH agent used from the current shell to the GCR side.

If OpenSSH's ssh-agent or SSH agent forwarding is already in use, the GCR-side agent will be used instead of that socket.

Conclusion

To unlock the GNOME Keyring on a remote machine over SSH, first set up

ssh -XY server
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

then either run

Passwords → Login → Unlock

from Seahorse, or request an unlock with

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

If you also want to use the GCR SSH agent, set

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
ssh-add -l
Enter fullscreen mode Exit fullscreen mode

If problems occur around the GNOME Keyring daemon, restarting it with systemctl --user and checking logs with journalctl proved effective.

リモートマシンのGNOME KeyringをX11 ForwardingでUnlockする

SSHで接続したLinuxサーバー上のGNOME Keyringを、X11 Forwarding経由でSeahorseを使ってUnlockしたときのメモ。

あわせて、secret-toolでUnlockする方法と、GNOME Keyring / GCRが提供するSSH agentをSSHセッションから利用する方法も記載する。

環境

SSHのX11 Forwardingを使い、リモート側で起動したSeahorseやGNOME Keyringのパスワードプロンプトをローカル側に表示する。

まずSSH接続する。

ssh -XY server
Enter fullscreen mode Exit fullscreen mode

接続後、X11用の環境変数をD-Bus / systemd user session側にも反映する。

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

GNOME KeyringをUnlockする

Seahorseを使う方法

Seahorseを起動する。

seahorse
Enter fullscreen mode Exit fullscreen mode

Seahorseが表示されたら、

Passwords → Login → Unlock

を選択し、GNOME Keyringのパスワードを入力する。

これでLogin keyringをUnlockできる。

secret-toolを使う方法

Seahorseを起動せず、secret-toolからUnlockを要求することもできる。

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

Keyringがロックされている場合はUnlock用のパスワードプロンプトが表示されるので、そこでパスワードを入力する。

検索結果そのものは不要なので、標準出力は/dev/nullへ捨てている。

X11 Forwarding経由でプロンプトを表示するため、先に以下を実行しておく。

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

したがって、GUIのSeahorseを開く必要がなければ、次の手順だけでもよい。

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

SSH agentを使う

GNOME Keyring / GCR側のSSH agentを利用する場合は、SSH_AUTH_SOCKをGCRのソケットに向ける。

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

設定後、agentに認識されている鍵を確認する。

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

一連の操作は例えば以下になる。

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

Seahorseを使う場合は、Unlock部分を次のように置き換える。

seahorse
# Passwords → Login → Unlock
Enter fullscreen mode Exit fullscreen mode

毎回GCRのSSH agentを使う場合は、利用しているshellの設定ファイルなどに次を追加しておいてもよい。

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

ただし、別のssh-agentやagent forwardingを併用している場合は、既存のSSH_AUTH_SOCKを上書きすることになるため注意する。

なぜ dbus-update-activation-environment が必要なのか

SSHのX11 Forwardingを使うと、SSHセッションには例えば以下のようなDISPLAYが設定される。

localhost:10.0
Enter fullscreen mode Exit fullscreen mode

一方、GNOME Keyring周辺のGUIプロンプトはD-Busやsystemdのuser session経由で起動されることがある。

そのため、SSHシェル上ではDISPLAYが正しく設定されていても、D-Bus経由で起動されたプロセス側ではSSHのX11 displayを認識できない場合がある。

そこで、

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

を実行して、現在のSSHセッションのDISPLAYXAUTHORITYをactivation environment側にも渡しておく。

手順まとめ

Seahorseを使う場合。

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

seahorse
# Passwords → Login → Unlock

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

secret-toolを使う場合。

ssh -XY server

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"

ssh-add -l
Enter fullscreen mode Exit fullscreen mode

不具合時のメモ

GNOME Keyring daemonが動いているか確認する。

pgrep -af gnome-keyring-daemon
Enter fullscreen mode Exit fullscreen mode

挙動がおかしい場合は、user serviceを再起動する。

systemctl --user stop gnome-keyring-daemon.service
systemctl --user start gnome-keyring-daemon.service
Enter fullscreen mode Exit fullscreen mode

ログをリアルタイムで確認する場合は以下。

journalctl --user-unit gnome-keyring-daemon -fe
Enter fullscreen mode Exit fullscreen mode

別ターミナルでこのログを流しながらSeahorseやsecret-toolを操作すると、daemon側で何が起きているか確認しやすい。

SSH agent側の確認には以下も使える。

echo "$SSH_AUTH_SOCK"
ls -l "$XDG_RUNTIME_DIR/gcr/ssh"
ssh-add -l
Enter fullscreen mode Exit fullscreen mode

期待するソケットは以下。

$XDG_RUNTIME_DIR/gcr/ssh
Enter fullscreen mode Exit fullscreen mode

補足

ssh -Xは通常のX11 Forwarding、ssh -Yはtrusted X11 Forwardingになる。

今回は、

ssh -XY server
Enter fullscreen mode Exit fullscreen mode

で動作を確認した。

trusted X11 Forwardingは通常の-Xよりリモートアプリケーションに広い権限を与えるため、信頼できるサーバーに対して使用するのが前提。

また、

dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

は、そのユーザーのD-Bus / systemd user sessionのactivation environmentを更新する。

同じユーザーでローカルのGUIセッションも並行して利用している環境では、GUIアプリケーションの表示先に影響する可能性があるため注意する。

同様に、

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
Enter fullscreen mode Exit fullscreen mode

は現在のshellから利用するSSH agentをGCR側に切り替える設定になる。

すでにOpenSSHのssh-agentやSSH agent forwardingを利用している場合、そのソケットではなくGCR側のagentを利用することになる。

結論

SSH越しにリモートマシンのGNOME KeyringをUnlockする場合は、まず

ssh -XY server
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY
Enter fullscreen mode Exit fullscreen mode

とした上で、Seahorseから

Passwords → Login → Unlock

を実行するか、

secret-tool search --unlock --all xdg:schema org.freedesktop.Secret.Generic >/dev/null
Enter fullscreen mode Exit fullscreen mode

でUnlockを要求する。

さらにGCRのSSH agentを利用する場合は、

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
ssh-add -l
Enter fullscreen mode Exit fullscreen mode

を設定する。

GNOME Keyring daemon周辺で問題が起きた場合は、systemctl --userでの再起動とjournalctlでのログ確認が有効だった。

Top comments (0)