0

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 ?

  • 1
    You are running echo <pwd>| sudo -S su; (which fails, try it on your local machine) and then whoami as a separate command, so that will never return root. 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 configure sudo to allow your user to run that command with sudo 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:56
  • @terdon this CMD has to be run in multiple / random test machine, which allowing root login or disable sudo's password is not feasible – hellojoshhhy Apr 13 '21 at 14:08
  • @terdon is not talking about disabling passwords, but about allowing your user to run specific binaries as root without having to supply a password to sudo. – Panki Apr 13 '21 at 14:14
  • Can you use ansible? – Natolio Apr 13 '21 at 14:15
  • @Panki as mentioned, the cmd is targeted to a large number of test machines, i prefer one line command instead of configuring each of the machine beforehand – hellojoshhhy Apr 13 '21 at 14:17
  • The entire point of sudo is to run a command as root. It doesn't make sense to run sudo su in any case, but here it makes even less sense. Why don't you just run ssh ... sudo -S command ? – terdon Apr 13 '21 at 14:24
  • @JeffPang if that's your preference, you'd best get used to typing in the password . You'll be doing it a lot. Otherwise, you'll need to either or both of: 1. configure sudo to allow your user to run certain commands as root without a password, or 2. install .authorized_keys for root allowing certain keys to either get a login shell or to to run particular commands/scripts, or both. There are several questions & answers on this site detailing how to do all of those things. – cas Apr 14 '21 at 02:38

0 Answers0