'gpg list keys error trustdb is not writable

When I try to list my gpg keys I'm getting an error:

gpg --list-keys
gpg: NOTE: trustdb not writable
gpg: checking the trustdb
gpg: trustdb rec 56: write failed (n=-1): Bad file descriptor
gpg: trustdb: sync failed: Bad file descriptor

I tried doing a recursive chown on my user directory. But that didn't seem to solve the problem.

The volume is not read only:

  [web1:~] bluethundr% touch test
[web1:~] bluethundr% echo hi > test
[web1:~] bluethundr% cat test
hi

So I'm wondering why the trustdb is not writable.

How can I solve this?



Solution 1:[1]

That seems to have been reported in bug 771976

I found this when running the command apt-key list on a machine where the root filesystem is normally mounted read-only.
This results in running gpg with "--trustdb-name", "/etc/apt//trustdb.gpg", "--list-keys".
gpg in turn attempts to create "/etc/apt//trustdb.gpg.lock" in order to lock the trustdb, which fails.

Seeing how --list-keys is an operation which shouldn't require writing to anything, having it fail in this situation is bad.

Even though the volume is not read-only, check (with an strace) where gpg --list-key is attempting to create its trustdb.gpg.lock file.

 strace gpg --list-key | grep trustdb.gpg.lock

The OP bluethundr adds in the comments:

I changed to the .gnupg directory and did a list.
I saw that the files in there were owned by root! So I did a sudo chown -R myuser:myuser *.
Then I ran gpg --list-keys again and it worked!
The problem was that the files in .gnupg were owned by root for some reason.

Solution 2:[2]

I granted the permissions on the trustdb.gpg file present in the installation directory in C Drive, to the user I had logged on with. It worked thereafter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sidharth Patra