Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have about 100 remote servers. I want to manage all my servers using one terminal server to update , run commands with sudo privileges.

I need to have one main script that takes another test script as an argument. The main script run through a with loop using a hosts file with remote server names.

The test script has all the actual sudo commands to be run on the remote servers at once

./mainscript hostfile testscript for example, test script may have "sudo yum -y update" or a string of commands.

This seems easy if I login as root user. But I want to run this as user 'admin' who has sudo privileges on all the remote servers. All these scripts are located locally on terminal server.

Please suggest me any ideas you may have to achieve this?

share|improve this question
possible duplicate of Copying protected files between servers in one line? – Gilles Jan 9 '13 at 0:38
@Gilles I'm missing something, how is this a duplicate? It seems unrelated, but has three close votes – Michael Mrozek Jan 9 '13 at 19:17
@MichaelMrozek On second reading, you're right. That earlier question addresses ssh sudo …, but here there's the additional difficulty of scripting over many servers. – Gilles Jan 9 '13 at 19:22
What is your sudo configuration? Will admin have to type a password to run sudo? If so, is this password stored in a file somewhere, or do you want to enter it interactively? – Gilles Jan 9 '13 at 19:22

One idea, though it might not be the best out there, is for you to configure ssh to use key based authentication instead of password authentication. This instructions should be sufficient in case you are not aware of it SSH Key Authentication

I don't know what your test script contains nor the complexity of it. let's assume that it is a fair complex script and it saves the output under /tmp/script_output/ and in this case I suggest the following:

while read -r i
   scp $2 admin@remote_server:/tmp/
   ssh -t admin@remote_server /tmp/$2
   ssh admin@remote_server rm /tmp/$2
   scp -r admin@remote_server:/tmp/script_output/ /tmp/
   ssh admin@remote_server rm -rf /tmp/script_output
done < "$1"

By the way, from the sudoers

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo <cmd>".
Defaults    requiretty
share|improve this answer
I seriously doubt anyone wants to rm -rf /tmp. – jordanm Jan 8 '13 at 19:55
This is bad, it needs to setup a SSH session five times which takes quite some time for 100 servers. You'd better combine the commands like ssh admin@server 'cat > file && chmod +x file && ./file;rm -f file' < script >output or something like that (for chaining). Also, this answer does not answer the question with sudo. – Lekensteyn Jan 8 '13 at 20:32
Hello all, I am using key based authentication for all the logins. But I still need to know how to use sudo in this ssh command. I am using ssh -tt admin@server , it does what it need to do ,but spits out an error "tcgetattr: Inappropriate ioctl for device" . For time being , I am redirecting the error to a log error file. Also, Lekensteyn, do you think the following commmand will be helpful instead of what alexandre suggested. ssh admin@server 'cat > file && chmod +x file && ./file;rm -f file' < testscript >output – Shashi Jan 8 '13 at 20:56
Answering all, Corrected the rm -rf /tmp as I what I wanted was clear the script output. Add the -t to the ssh as per sudoers specification. The sudo requires tty. – BitsOfNix Jan 9 '13 at 8:12

I had the same problem some time ago. That's my solution:

  • Download this script.
  • Create CSV file with names of servers and passwords (I assume that login is the same on each server).
  • Create script that you want to execute.
  • Adjust variables in my Ruby script, run it and let it do all the work for you. :-)
share|improve this answer

The following will allow you to execute sudo commands on multiple hosts while only having to enter your sudo password once at the beginning.

What is missing is the for loop or something that sets the HOSTS variable to the hosts you have. The script creates a file in the temp folder that contains the sudo password, but that file is deleted immediately. This allows the command to use the sudo password without it being displayed in the terminal window or in your script.


if "something" 


cat > /tmp/$HOSTS-pw.sh <<EOS
ssh user@$HOSTS sudo "your command here" <<EOC


chmod 700 /tmp/$HOSTS-pw.sh
/tmp/$HOSTS-pw.sh >/dev/null
if [ -f /tmp/$HOSTS-pw.sh ]; then rm -f /tmp/$HOSTS-pw.sh; fi

echo "Enter SUDO password:"
read -s SUDOPW

for loop here!

unset SUDOPW
exit 0
share|improve this answer

Strange way to do this.

I do some things at many servers using clusterssh (type once, do it on every server).

For bigger changes we use our own repositories that are activated on every server. That way we use the OS patch-mechanism to distribute specific changes to a large number of servers.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.