user GPG key not able to be used by SUDO

Solution: Added the following to the bash script:

HOME=/home/user/

Fin


Have you tried the --homedir option?


I believe the default configuration of sudo is to preserve $HOME. So if you were logged in as user1, and used sudo scriptname where scriptname did echo $HOME, you should expect to see "/home/user1" echoed back, not "/root".

I'll assume BassKozz hasn't changed this. Perhaps he's not logged in as his desired user1, running the script as sudo. Perhaps he's really just running the script as root, for example through root's cronjob. In that case, his $HOME would never have been /home/user1 in the first place, so even if sudo preserves the value of $HOME it's not helping. In this case, any of the other answers that tell you how to set $HOME to the right value, or to inform gpg where your homedir is, should work.

If however, it's true as he says that he can't even run his duplicity script "as sudo"---that is, when logged in as user1 and typing sudo duplicity_script, then the problem is not going to be a wrongly-set $HOME. As we've seen, $HOME should in that case have the right value. So the problem is something else. I haven't heard enough, and don't know duplicity+gpg well enough, to speculate what it might be.


If you use the "--preserve-env" option to sudo, then GPG in the sudo session will be able to find the gpg-agent running in the native session.

Example:

sudo --preserve-env YOUR_COMMAND...