Skip to content

Instantly share code, notes, and snippets.

@Ronmi
Created September 6, 2025 01:36
Show Gist options
  • Select an option

  • Save Ronmi/7cf9486d77167556edc9be8484619766 to your computer and use it in GitHub Desktop.

Select an option

Save Ronmi/7cf9486d77167556edc9be8484619766 to your computer and use it in GitHub Desktop.
A shell script to select pinentry implementation according to envvar

Only useful if you

  • use pinentry (like gpg signing, manage credentials with pass)
  • ssh to your desktop sometimes

TL; DR

  • Install pinentry-qt or pinentry-gnome.
  • Install pinentry-curses or pinentry-tty.
  • Create the installer script and run it with root:
#!/usr/bin/bash

function _confirm_shell() {
    local ans
    read -N 1 -p "$1 [y/N]" ans
    [[ "$ans" == "y" || "$ans" == "Y" ]]
}

if [[ $UID != "0" ]]
then
    echo "This script requires root priviledge to run"
    exit 1
fi

echo "This script will create a shell script 'pinentry-auto' in /usr/local/bin "
echo "and install it with 'update-alternatives' as default option."
echo

_confirm_shell "Continue?" || exit $?

echo '/Td6WFoAAATm1rRGBMD1AoBQIQEWAAAAAAAAAFD9XCTgJ/8BbV0AOpzKiwmA42IcXErbUViw5kCv5HPxTfqE7Nwcuxp176NFdK7dws/n2Ax+r54Qsq5kQjfm22PJr5BO66PJiQTFFiK6CFGlsitUHXOIFfN3jm6jpxtX5kwgh7hdFlXKicKX84KFzJ9yQGPOm5lW/FxMc59l4NZAxf9tTUYhBvAQNuDmcGLWrCOqHEzbgKcDVc4nBvfWiPI4RlqmUlw9jx+RdclutXz0YCQrGDomD4NCUAQ/9sy+bF2CnKE920sA/F3qbmxwrO1pt08JeSX8VmswS1bbc2kcI61z2j8PrN4lmubbVPjTu3HzinOvRxGc/cXfsagRS8Sw0AY3PqfZyCEeUtLstChCDN54EPk8ob5ctfLo/HZUo7twL4leE1PcEUlRFPiO4lvcIOQ1rCY+7l8j2FkAVr41Pu1V6smjEy5GahphyjU1mgJw7Dj+a55sIRjexNLdQs/JVdGFLF6/ARMttXBfLLosiT/k34cJ8sAAAAAASwDP/LjItKEAAZEDgFAAAGDzKtOxxGf7AgAAAAAEWVo=' | base64 -d | tar Jxvf - -C /

update-alternatives --install /usr/bin/pinentry pinentry /usr/local/bin/pinentry-auto 90
update-alternatives --auto pinentry

Manual install (and how it works)

  1. Only following implementations are supported (you can modify the script to add more):
    • pinentry-gnome
    • pinentry-qt
    • pinentry-curses
    • pinentry-tty
  2. Install pinentry implementations for both GUI and CLI, qt+cueses for example.
  3. Create /usr/local/bin/pinentry-auto:
#!/usr/bin/bash

function _has_prog() {
  which "$1" > /dev/null 2>&1
}

if [[ -n "$DISPLAY" ]]
then
  if _has_prog pinentry-gnome ; then
    # use pinentry-gnome only when gnome
    [[ "$DESKTOP_SESSION" == "gnome" ]] && exec pinentry-gnome "$@"
  fi
  # try pinentry-qt if not gnome, fallback to CLI (curese/tty)
  if _has_prog pinentry-qt ; then
    exec pinentry-qt "$@"
  fi
fi

if _has_prog pinentry-curses ; then
  exec pinentry-curses "$@"
fi

if _has_prog pinentry-tty ; then
  exec pinentry-tty "$@"
fi

# fallback
"$(update-alternatives --list pinentry | grep -vF pinentry-auto | head -n 1)" "$@"
  1. Register it with sudo update-alternatives --install /usr/bin/pinentry pinentry /usr/local/bin/pinentry-auto 90
  2. You might want to run sudo update-alternatives --auto pinentry for sure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment