ssh-agent forwarding to sudo (root user)

terminal

I use ssh-agent to store my ssh key so that I don’t have to enter the key passphrase every time I access one of my servers over ssh. When I run commands using my regular user (non-root), ssh-agent works as expected, stores the key and issuing commands accessing the server(s) doesn’t request the passphrase. However, when I run commands as a root user via sudo, the key forwarding is lost because itsĀ  mechanism relies on the SSH_AUTH_SOCKĀ  environment variable. However, environment variables are removed when the user is switched to root via sudo. How do we prevent the SSH_AUTH_SOCK variable from being removed when issuing sudo, thereby passing on key forwarding to the root user?

Continue reading