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).
As you mentioned, the environment variables are removed by
sudo
, for security reasons.But fortunately
sudo
is quite configurable: you can tell it precisely which environment variables you want to keep thanks to theenv_keep
configuration option in/etc/sudoers
.For agent forwarding, you need to keep the
SSH_AUTH_SOCK
environment variable. To do so, simply edit your/etc/sudoers
configuration file (always usingvisudo
) and set theenv_keep
option to the appropriate users. If you want this option to be set for all users, use theDefaults
line like this:man sudoers
for more details.You should now be able to do something like this (provided
user1
's public key is present in~/.ssh/authorized_keys
inuser1@serverA
anduser2@serverB
, andserverA
's/etc/sudoers
file is setup as indicated above):This will give you a root shell with the original keys still loaded.
Allow otheruser to access
$SSH_AUTH_SOCK
file and it's directory, for example by correct ACL, before switching to them.The example assumes
Defaults:user env_keep += SSH_AUTH_SOCK
in/etc/sudoers
on 'host' machine:More secure and works for non-root users as well ?
I have found that this also works.
As others have noted, this won't work if the user you are switching to doesn't have read permissions on $SSH_AUTH_SOCK (which is pretty much any user besides root). You can get around this by setting $SSH_AUTH_SOCK and the directory it is in to have the permissions 777.
This is pretty risky though. You are basically giving every other user on the system permission to use your SSH Agent (until you log out). You may also be able to set the group and change the permissions to 770, which could be more secure. However, when I tried changing the group, I got "Operation not permitted".
You can always just ssh to localhost with agent forwarding instead of using sudo:
Disadvantage is that you need to log in again, but if you're using it in a screen/tmux tab, that's only a one time effort, however, if you disconnect from the server, the socket will (of course) be broken again. So it's not ideal if you can't keep your screen/tmux session open at all times (however, you could manually update your
SSH_AUTH_SOCK
env var if you're cool).Also note that when using ssh forwarding, root can always access your socket and use your ssh authentication (as long as you're logged in with ssh forwarding). So make sure you can trust root.
If you are authorized to
sudo su - $USER
, then you would probably have a good argument for being permitted to do assh -AY $USER@localhost
instead, with your valid public key in $USER's home directory. Then your authentication forwarding would be carried through with you.Combining information from the other answers I came up with this:
I like this because I don't need to edit
sudoers
file.Tested on Ubuntu 14.04 (had to install
acl
package).Don't use
sudo su - USER
, but rathersudo -i -u USER
. Works for me!I think there is a problem with the
-
(dash) option aftersu
in your command:If you read the man page of su, you may find that the option
-, -l, --login
starts the shell environment as login shell. This will load the environment forotheruser
regardless of the env variables where you runsu
.Simply put, the dash will undermine anything you passed from
sudo
.Instead, you should try this command:
As @joao-costa pointed out,
-E
will preserve all variables in the environment where you ransudo
. Then without the dash,su
will use that environment directly.Unfortunately when you su to another user (or even use sudo) you'll lose the ability to use your forwarded keys. This is a security feature: You don't want random users connecting to your ssh-agent and using your keys :)
The "ssh -Ay ${USER}@localhost" method is a little cumbersome (and as noted in my comment on David's answer prone to breakage), but it's probably the best you can do.