Execute Commands on Multiple Linux or UNIX Servers

This is Part I in a series – ‘Execute Commands on Multiple Linux or UNIX Servers Simultaneously’. The full series is Part I, Part II, and Part III. Some time it is necessary to execute commands on Multiple Linux or UNIX Servers, for example you would like to find out who is logged on and what they are doing on three Linux or UNIX boxes or better find out system utilization, disk space and much more. With the help of ssh you can easily setup such nice system.

Our Sample Setup

Admin Linux workstation -> Server # 1 with ssh
adm.my.com              -> server1.my.com

SSH client is a program for logging into a remote machine and for executing commands on a remote machine. If command is specified, command is executed on the remote host instead of a login shell.

$ ssh  user@server1.my.com  w

Above command will gather up all logged in users information. However if you put this command in script to gather information from three server as follows it will prompt for a password:

ssh user@server1.my.com w
ssh user@server2.my.com w
ssh user@server3.my.com w

To get rid of password you can setup ssh key based login. Once ssh keys are in place you can simply create a script as follows:

# Linux/UNIX box with ssh key based login
# SSH User name
# Email
SUBJECT="Server user login report"
# create new file
# connect each host and pull up user listing
for host in $SERVERS
echo "--------------------------------" >>$EMAILMESSAGE
echo "* HOST: $host " >>$EMAILMESSAGE
echo "--------------------------------" >>$EMAILMESSAGE
ssh $USR@$host w >> $EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

You need to setup your hostname and email id and then execute shell script. This is very simple yet powerful method to execute commands simultaneously on multiple Linux/UNIX servers. If you are really interested to see application output then visit here, it was produced by this script (see our forum for more). This is just small script but you are only limited by your own imagination. Few more advanced tools do exist I will cover them some time later.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Anonymous Dec 28, 2005 @ 19:15
  • Michael Dec 28, 2005 @ 20:31

    If you want to fully parallelize the script you can wrap the ssh command in parentheses in order to execute in a subshell, I believe. That way the script doesn’t wait for the program to finish. I use this to dynamically test all the workstations in my lab (~150) to see which ones are up and running, so I can generate a machine list for a distributed MPICH program.

  • Anonymous Dec 28, 2005 @ 20:55

    Very nice script. I use(d?) to manually write a “for” loop every time I need to run the same command on multiple hosts.
    I’ve just modified your sample script, declaring another variable (COMMAND=$@) to use instead of “w”, so I can run
    ./dsh cat /etc/hosts to get the content of all servers’ /etc/hosts in my mailbox.

  • WikiGeeiki Dec 23, 2006 @ 23:29

    Excellent article! Thank you.

  • nfo Sep 10, 2008 @ 8:14

    Hi, nice script, simple and functionally!
    another simialar solution is http://freshmeat.net/projects/dsh/
    dsh can start a command parallel and serial, witch is a small advantage, but never mind.

    best regards

  • Albert Dec 30, 2008 @ 19:47

    Great script, easy to use and understand. Thanks.

  • Boby Thomas Mar 24, 2009 @ 10:49

    Nice script….thanks

  • Nitn G Oct 9, 2009 @ 17:30

    i tested this script found not working when one of host is down ssh command does not proceed to next host

    please help


  • Jidnesh I. Bhogare Jan 12, 2010 @ 6:43

    Hi, the script is really help ful!! Thanks for this. I want to add one small things for the administrators / developers who want to execute a command only on the remote machine via ssh + shell script e.g ssh test@ ‘service httpd status’ ….if some one want to change the single quoted parameter…he has to defined that much variable and called each variable one by one as per his desire to form expected command…check this code

    for I in $INPUT
    echo -e "----------------------------------------------n";
    for ip1 in $I2
    for ip2 in $I3
    ssh root@ $I $ip1 $ip2
    echo -e "----------------------------------------------n";
  • tepisi Dec 23, 2010 @ 12:14

    But, how to execute remote self made shell script via ssh?

  • drobban Jun 19, 2016 @ 20:06

    Nice article. Interesting subject.

    Just wanted to tip about fabric. Great tool for deployment / Installation / admin python scripts

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum