Ubuntu – Key Authentication Issues with SSH or Rsync as Sudo

passwordrsyncsshsudoUbuntu

I have ssh RSA key authentication setup between a client and an ubuntu server (following this procedure):
-the RSA key authentication for ssh and rsync work fine (no need for password).
-the RSA key authentication for sudo rsync and sudo ssh fails (I'm prompted for a password).
-I need to use sudo rsync to write the remote files locally.

I'm running the command as follows:

user1@server:/$ sudo su user2
user2@server:/$ rsync 192.168.1.2:... # ok
user2@server:/$ sudo rsync 192.168.1.2:... # remote host password prompt

FYI both user1 and user2 are part of sudoers

Any idea where that could be coming from?

Thanks.

updated: to clarify, the password prompt is not coming from sudo but from the server

Best Answer

You need to tell it to use the correct public key. I suspect ssh is still using user1's key, not user2. Use this command:

sudo -u user2 ssh -i ~user2/.ssh/id_rsa remoteserver

I tested this on my machine and it worked.