Fixing GnuPG 'unsafe permissions' Error

A guide to fixing an annoying GnuPG error I occasionally get.

Introduction

This is a quick guide on how to fix the annoying error that gnupg gives when you try to use it.

The error is something like:
gpg: WARNING: unsafe permissions on homedir '/home/username/.gnupg'.

Solution

  1. Specifying ownership of the gnupg folder:
    • Run this command: chown -R $(whoami) ~/.gnupg/
    • This command makes it so that your current user is the owner of the gnupg folder, ensuring your user account to be able to access it freely.
  2. Limiting read, write and execution commands for files:
    • find ~/.gnupg -type f -exec chmod 600 {} \;
  3. Limiting read, write and execution for directories:
    • find ~/.gnupg -type d -exec chmod 700 {} \;

If you mess something up

If you somehow mess up the commands or you do something else and you are unable to access/ modify the files or keys in that directory, then you can easily fix it with these two commands:

  1. sudo find ~/.gnupg -type f -exec chmod 777 {} \;
  2. sudo find ~/.gnupg -type d -exec chmod 777 {} \;
  • These two commands will change the permissions of the files and directories so that everybody can read, write and execute these files.
  • After you’ve done this, you want to retry the commands mentioned above that will restrict permissions.
  • Ensure that the permissions are correctly set by running a command that will access your keys (such as pass).