If you can use SSH agent forwarding, there actually is a way:
pam_ssh_agent_auth.so (source here) is a PAM module that can do what you ask. It's available in Debian and Ubuntu as package
libpam-ssh-agent-auth and as CentOS package
You should evaluate the risks of using SSH agent forwarding, as the developer says:
There are caveats of course, ssh-agent forwarding has it’s own security risks
which must be carefully considered for your environment. In cases where there
are not untrustworthy intermediate servers, and you wish to retain traceability,
accountability, and required authentication for privileged command invocation,
the benefits should outweigh the risks.
If you make sure your KeeAgent has the option Always require confirmation when client program requests to use key set, this even offers you a degree of protection against someone else with root access on the remote host: if you get a SSH key request confirmation dialog with no obvious reason, you'll know that someone is trying to abuse your SSH agent connection.
If you also make sure you'll always lock your KeePass and/or workstation screen when you step away from it, I think this should offer pretty good security; it's certainly more secure than using
sudoers. It is also better than allowing
root logins with ssh keys only and adding everyone who is allowed to log in as root to root's
authorized_keys file because it maintains the advantages of sudo.
To use it, you basically add this as the first
auth line in
auth sufficient pam_ssh_agent_auth.so file=/etc/security/authorized_keys
Then add the public SSH keys of users that should be authorized to use SSH-authenticated
/etc/security/authorized_keys in the usual single-line OpenSSH compatible format.
Then configure sudoers to preserve the environment variable
SSH_AUTH_SOCK by editing the sudoers file (use
visudo). Add this line to the section with the other
Defaults env_keep += "SSH_AUTH_SOCK"
Then, you need to make sure that your ssh client allows agent forwarding. In PuTTY you need to check this:
While testing this, don't forget to terminate your sudo sessions with