This error appears every time I want to use GPG such as when I want to encrypt messages, add git commit messages, etc.

error: gpg failed to sign the data:
[GNUPG:] KEY_CONSIDERED BF94AE714E606CCB56438D29035D9C314765A73C 2
[GNUPG:] BEGIN_SIGNING H10
[GNUPG:] PINENTRY_LAUNCHED 2412 curses 1.2.1 - xterm-256color :0 - 1000/1000 -
gpg: signing failed: Inappropriate ioctl for device
[GNUPG:] FAILURE sign 83918950
gpg: signing failed: Inappropriate ioctl for device
 
fatal: failed to write commit object

I can fix it with the command export GPG_TTY=$(tty) but that only fixes it temporarily, when I create a new session on the terminal/shell the problem will appear again

Root Cause

gpg: signing failed: Inappropriate ioctl for device
fatal: failed to write commit object

The error is caused because GnuPG (GPG) can’t interact properly with the terminal (TTY) when it tries to prompt you for your passphrase to sign the commit.

GPG needs to prompt you for your passphrase securely, and it does this via a helper program called pinentry (e.g. pinentry-tty, pinentry-curses, pinentry-gtk, etc.).

But when GPG doesn’t know which terminal (TTY) to use — especially in cases like:

  • you’re using a new shell session
  • you’re inside a GUI or tool without a proper TTY, like a VS Code Git panel
  • or the shell environment hasn’t set GPG_TTY

…then GPG fails to communicate with pinentry, leading to:

gpg: signing failed: Inappropriate ioctl for device

This literally means:

“Hey, GPG tried to talk to the terminal, but this isn’t the kind of device I expected — I can’t ask for a password here!”

Why export GPG_TTY=$(tty) Fixes It

This tells GPG:

“Hey, use this terminal (TTY) for the pinentry prompt.”

Now it knows where to securely show the password prompt.

Solution

Add to Your Shell Config

Depending on the shell you use (bash, zsh, etc.), add the following line to your shell’s config file:

Bash

echo 'export GPG_TTY=$(tty)' >> ~/.bashrc

ZSH

echo 'export GPG_TTY=$(tty)' >> ~/.zshrc

Then reload the config:

source ~/.bashrc   # or source ~/.zshrc

(Optional) Add to Git Config (User-Level)

If you haven’t already set the GPG key in your Git config:

git config --global user.signingkey YOUR_GPG_KEY_ID
git config --global commit.gpgsign true

(Optional) Use gpg-agent with TTY Awareness

Make sure you’re using gpg-agent, and if you’re on a modern system using GnuPG 2.x, check ~/.gnupg/gpg.conf and ~/.gnupg/gpg-agent.conf:

~/.gnupg/gpg.conf:

use-agent

~/.gnupg/gpg-agent.conf:

pinentry-program /usr/bin/pinentry-curses

Replace pinentry-curses with pinentry-gtk-2, pinentry-qt, or other appropriate program for your desktop/terminal environment.

Then reload gpg-agent:

gpg-connect-agent reloadagent /bye

(Optional) Start gpg-agent Automatically

If needed, you can also add this to your shell config:

export GPG_TTY=$(tty)
gpgconf --launch gpg-agent

Summary:

  • Cause: GPG doesn’t know which terminal to use → can’t ask for passphrase → fails with IOCTL error.
  • Temporary fix: export GPG_TTY=$(tty)
  • Permanent fix: Set that line in your shell config file.

Reference


Troubleshooting