Keeping access to SSH Agent using sudo
When sudo into another user, sudo will strip the environment for security reasons. When trying to do root actions that require ssh keys, all access has been lost.
For the root user, these can be kept without caveats because they have access to everything anyway :P So we add the following to /etc/sudoers using visudo (do not etc the /etc/sudoers file directly).
# .. other Defaults lines # Keep ssh agent access when sudo to root only Defaults>root env_keep+=SSH_AUTH_SOCK
It is also possible to maintain access when doing sudo into any user but requires setting up the the ssh agent socket to be read/write/exec to the target sudo user. Better to just stick to using root.