Yes. It is possible using pam_ssh_agent_auth
package if your distribution provides. It can allow you to execute sudo based on pam module, which checks possession of ssh key in ssh-agent.
Short story long
Setup
- Install
pam_ssh_agent_auth
package from package manager
Modify /etc/sudoers
, preferably using visudo
and add line
Defaults env_keep += "SSH_AUTH_SOCK"
Edit /etc/pam.d/sudo
and add (as a second line after #%PAM-1.0
)
auth sufficient pam_ssh_agent_auth.so file=/etc/security/authorized_keys
and comment out line
#auth include system-auth
to disallow normal system authentication for sudo command
Create the "privileged" key-pair that should have access to the sudo
command and the store public part as /etc/security/authorized_keys
on server.
ssh-keygen -t rsa -b 2048
cat ~/.ssh/id_rsa > /etc/security/authorized_keys
Usage
On client open ssh-agent and add above mentioned key:
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
Connect to server with agent forwarding
ssh -K server
Run sudo as you wish
This works fine on Fedora/RHEL/CentOS systems, from what I tested so far.