I am trying to SSH a remote machine and change user to root and run a series of command which need root
I tried the command below but seems it's not working
sshpass -p <pwd> ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null <user>@$IP "echo <pwd>| sudo -S su; whoami"
whoami
always return instead of root
Any idea how to get it done?
edit
echo <pwd> | sudo -S <some-command>
always work in this case, but not with sudo -S su
?
echo <pwd>| sudo -S su;
(which fails, try it on your local machine) and thenwhoami
as a separate command, so that will never returnroot
. What command do you really need to run as root on the remote machine? Since you have root access on the remote, can't you configuresudo
to allow your user to run that command withsudo
without a password? Would that be an acceptable solution? Also, since you don't seem to be very concerned about security, can you configure the remote to allow root login? – terdon Apr 13 '21 at 13:56sudo
. – Panki Apr 13 '21 at 14:14sudo
is to run a command as root. It doesn't make sense to runsudo su
in any case, but here it makes even less sense. Why don't you just runssh ... sudo -S command
? – terdon Apr 13 '21 at 14:24