Search code examples
linuxbashpasswordssudosu

sudo su with user name, password and command


I am trying to do something like

echo 'my_password' | sudo -S su -c some-user ./some_command

I have looked at

How can I apply password to sudo in one line command and execute su root?

and

run a sudo command, specifying the password on the same line

But neither of these really answer my question above.

I am still getting the password prompt for my user.


Solution

  • I've got two answers for you.

    The first answer is "don't do it". There is, almost certainly, a better way. You can specify to sudo that certain users can perform certain commands without entering a password. In all likelihood, that is what you want to do.

    Having failed to convince you, however, I will let you in on a little secret. sshpass works on sudo, so:

    sshpass -p 'my password' sudo -S su -c some-user ./some_command
    

    Of course, while there, we can cut the su middle man:

    sshpass -p 'my password' sudo -S -u some-user ./some_command