Ssh – pass a password to sudo su

bashremote-accessshellsshsudo

I wanted to sudo to different user after ssh'ing into remote server, for this i tried the following:

cat remote-test.sh
ssh -t -t abcuser@test-server.net 'bash -s' << EOF
  /tmp/test.sh
EOF

when i execute the remote-test.sh from development-server.net, I wanted to ssh to test-server.net as abcuser and then sudo as xyzuser .

cat /tmp/test.sh

echo "password" | sudo -S su - xyzuser
cd /tmp/some/directory

Can someone please advice how to pass a password while doing sudo su in a script.

Best Answer

You setup password-less ssh to localhost as user xyzuser for abcuser to achieve what you're trying. You'll need to add abcuser's public key as an authorized_key for xyzuser.

Then when you're logged in as abcuser, you can do:

ssh xyzuser@localhost do_something_as_xyzuser

If you've blocked ssh access to xyzuser, allow it only through loopback. If you're on linux, see your /etc/security/access.conf or equivalent on how to do that.