在TTY或SSH上使用GPG,即没有GUI界面时

当通过ssh远程连接到服务器,或者在纯字符界面的tty上操作时,总之就是在无GUI情况下操作时。
使用gpg签名(或者通过git commit -S等调用gpg),会报错:

1
2
gpg: signing failed: Inappropriate ioctl for device
gpg: [stdin]: clear-sign failed: Inappropriate ioctl for device

原因是无法调用默认的/usr/bin/pinentry-gtk-2,如果你没安装GTK的话也会出现这个报错。

根据Arch Wiki介绍[1],修改.bashrc即可(zsh同理):

.bashrc
1
export GPG_TTY=$(tty)

  1. (2023) ArchWiki: GnuPG§9.12 Invalid IPC response and Inappropriate ioctl for device

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    === Invalid IPC response and Inappropriate ioctl for device ===

    The default pinentry program is {{ic|/usr/bin/pinentry-gtk-2}}. If {{Pkg|gtk2}} is unavailable, pinentry falls back to {{ic|/usr/bin/pinentry-curses}} and causes signing to fail:

    gpg: signing failed: Inappropriate ioctl for device
    gpg: [stdin]: clear-sign failed: Inappropriate ioctl for device

    You need to set the {{ic|GPG_TTY}} environment variable for the pinentry programs {{ic|/usr/bin/pinentry-tty}} and {{ic|/usr/bin/pinentry-curses}}.

    $ export GPG_TTY=$(tty)
    ↩︎