I am attempting to develop a script that upgrades my VMs on a local network. The goal is for each VM to download the script and then execute it.

I have been able to use curl to download the script and execute, but I still receive a prompt to enter the password for sudo.

This works:

sshpass -p 'password' ssh -t -t [email protected] 'curl -s | sudo sh'

But I still have to enter a password again when it reaches the sudo command.

I've attempted:

sshpass -p 'password' ssh -t -t [email protected] 'curl -s | echo 'password' | sudo sh'

Which prompts the following:

[sudo] password for aaron: (23) Failed writing body

What can I do to pass the password to the sudo password prompt?

  • You can use sudo -S to take a password from sudo, but your pipe as it currently is set will not do the right thing. You are piping the output of curl to echo not the sudo command.
    – jsbillings
    Oct 18, 2020 at 15:08
  • If these are VMs and on a local network, can't you just ssh into them as root to run the script?
    – terdon
    Oct 18, 2020 at 15:08
  • Also, using sudo -S is a terrible idea because it has the password viewable in the output of ps and in your shell’s history.
    – jsbillings
    Oct 18, 2020 at 15:09
  • username ALL=(ALL) NOPASSWD: ALL in sudoers wil also prevent asking for a password. Oct 18, 2020 at 15:45
  • @LjmDullaart that means anyone who gets access to the machine can run any command as root. It is a really bad idea. If you must go that route, at least make it specific to the command that must be run.
    – terdon
    Oct 18, 2020 at 16:40

1 Answer 1


Basically there are a number of options, each with their pro's and con's. It depends a bit on how "local" your network is and how durable your solution should be.

  1. Password-less sudo. Allow the user passwordless sudo on the target machine. Either for everything (put "nameofuser ALL=(ALL) NOPASSWD: ALL in /etc/sudoers using visudo on the target machines) or allow only specific commands. Note that everyone with access to nameofuser on the target machine practically has root-access on thet machine (not everyone on the machine as @terdon suggested in the comments).

  2. Allow the user on the controlling machine to ssh as root to the target machine. Put the public key of the controlling user in the authorized_keys file on the target machine. That means that the specific user on the controlling machine has root-access on all the VM's.

  3. Look at Ansible (or Puppet, Chef etc). They already more or less solved this problem for you. Ansible has a gentle learning curve (in less than a day you'll be able to actually use it).

If it is anything serious for a longer duration, choose option 3. If it is just your own local network where you are experimenting, 1 or 2 will do.

  • I think I'll look at ansible. Makes sense to use and learn something like that.
    – cinemafunk
    Oct 19, 2020 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .