If I have a server A into which I can login with my ssh key and I have the ability to "sudo su - otheruser", I lose key forwarding, because the env variables are removed and the socket is only readable by my original user. Is there a way I can bridge the key forwarding through the "sudo su - otheruser", so I can do stuff on a server B with my forwarded key (git clone and rsync in my case)?
The only way I can think of is adding my key to authorized_keys of otheruser and "ssh otheruser@localhost", but that's cumbersome to do for every user and server combination I may have.
In short:
$ sudo -HE ssh user@host
(success)
$ sudo -HE -u otheruser ssh user@host
Permission denied (publickey).
user2above isroot! Otherwise,user2will have SSH_AUTH_SOCK set up correctly, but theuser2won't be able to access e.g. /tmp/ssh-GjglIJ9337/.rootdoes have that access.So this may solve part of the problem, but not the OPs: "and the socket is only readable by my original user"
– Peter V. Mørch Nov 04 '11 at 21:00Defaults>root env_keep+=SSH_AUTH_SOCKShould make sure it only forwards when sudoing to root. You don't want to do that anyways to other users for security reasons. Better run a separate ssh-agent for otherother, and add the appropriate keys. – Paul Schyska Aug 24 '14 at 10:32sudo su -doesn't work for me, presumably it can't preserve environment because it's notsudoat shell startup time.sudo suseems to work. – Alex Fortuna May 19 '16 at 11:25sudo suanyway. If you need a root shell, that's whatsudo -sorsudo -iis for. – eaj Nov 16 '16 at 14:46sudo -ididn't exist originally, it was added later. So a lot of people got into the habit ofsudo su, and lots of docs were written with the old way. Though it has available for ~15 years now. – Zoredache Oct 09 '19 at 21:20sudo. Just run it assudo --preserve-env=SSH_AUTH_SOCK. – greatvovan May 01 '20 at 23:30