4 Useful Tools to Run Commands on Multiple Linux Servers

In this article, we will show how to run commands on multiple Linux servers at the same time. We will explain how to use some of the widely known tools designed to execute repetitive series of commands on multiple servers simultaneously. This guide is useful for system administrators who usually have to check the health of multiple Linux servers everyday.

For the purpose of this article, we assume that you already have SSH setup to access all your servers and secondly, when accessing multiple servers simultaneously, it is appropriate to set up key-based password-less SSH on all of your Linux servers. This above all enhances server security and also enables ease of access.

Read Also: How to Configure Custom SSH Connections to Simplify Remote Access

1. PSSH – Parallel SSH

Parallel-SSH is an open source, fast and easy-to-use command line based Python toolkit for executing ssh in parallel on a number of Linux systems. It contains a number of tools for various purposes such as parallel-ssh, parallel-scp, parallel-rsync, parallel-slurp and parallel-nuke (read the man page of a particular tool for more information).

To install parallel-ssh, you need to first install PIP on your Linux system.

$ sudo apt install python-pip python-setuptools 	#Debian/Ubuntu 
# yum install python-pip python-setuptools	        #RHEL/CentOS 
# dnf install python-pip python-setuptools	        #Fedora 22+

Then install parallel-ssh using pip as follows.

$ sudo pip install parallel-ssh

Next, enter the hostnames or IP addresses of remote Linux server with SSH Port in a file called hosts (you can name it anything you want):

$ vim hosts
pssh hosts file
192.168.0.10:22
192.168.0.11:22
192.168.0.12:22

Save the file and close it.

Now run parallel-ssh, specify the hosts file using the -h option and a command(s) that will be executed on all the specified servers. The -i flag means display std output and std error as execution of the command on each server completes.

$ parallel-ssh -h hosts "uptime; df -h"
Pssh Run Commands on Multiple Linux Servers

Pssh Run Commands on Multiple Linux Servers

You should also check out: How to Run Multiple Commands on Multiple Linux Servers

2. Pdsh – Parallel Remote Shell Utility

Pdsh is an open source, simple parallel remote shell tool for executing commands on multiple Linux servers at the same time. It employs a sliding window of threads to execute remote commands.

To install Pdsh on your Linux machines, run the appropriate command below.

$ sudo apt install pdsh 	#Debian/Ubuntu 
# yum install pdsh	        #RHEL/CentOS 
# dnf install pdsh              #Fedora 22+

To run commands on multiple servers, add the servers to a hosts file as explained before. Then run pdsh as shown; the flag -w is used to specify the hosts file, and -R is used to specify the remote command module (available remote command modules include ssh, rsh, exec, the default is rsh).

Take note of the ^ before the hosts file.

$ pdsh -w ^hosts -R ssh "uptime; df -h"
pdsh - Execute Commands in Multiple Linux Servers

pdsh – Execute Commands in Multiple Linux Servers

In case you do not specify a remote command to be executed on the command line as shown above, pdsh runs interactively, prompting you for commands and running them when terminated with a carriage return. For more information, see the pdsh man page:

$ man pdsh 

3. ClusterSSH

ClusterSSH is a command line tool for administering clusters of multiple servers at the same time. It launches an administration console and an xterm to all specified servers enabling you to run the same command on all of them.

To use clusterssh, start by installing it on your local Linux computer as shown.

$ sudo apt install clusterssh    #Debian/Ubuntu 
# yum install clusterssh         #RHEL/CentOS 
$ sudo dnf install clusterssh    #Fedora 22+

Now that you have it installed, open an admin console and an xterm on remote servers at once, as follows. To run a command on all the servers, click in the xterm input bar, and type your command; to manage a single host, use its admin console.

$ clusterssh linode cserver contabo
OR
$ clusterssh [email protected] [email protected] [email protected] 
Clusterssh - Administer Multiple SSH Sessions

Clusterssh – Administer Multiple SSH Sessions

For more information, see the clusterssh man page:

$ man clusterssh

4. Ansible

Ansible is an open source and popular tool to automate IT processes. It is used for configuring and managing systems, deploying applications and so much more.

To install Ansible on Linux systems, run the appropriate command below:

$ sudo apt install ansible       #Debian/Ubuntu 
# yum install ansible            #RHEL/CentOS 
$ sudo dnf install ansible       #Fedora 22+

Once you have installed ansible, you can add your server’s hostnames or IP addresses in the file /etc/anasible/hosts.

$ sudo vim /etc/anasible/hosts

Specify them in groups, e.g webservers.

# Ex 2: A collection of hosts belonging to the 'webservers' group
[webservers]
139.10.100.147
139.20.40.90
192.30.152.186

Save the file and close it.

Now to check the uptime and users connected to all the servers specified in the group webserver, in hosts config file above, simply run the ansible command line tool as follows.

The -a options is used to specify the arguments to pass to the module and -u flag specifies the default username to connect to the remote servers via SSH.

Note that the ansible CLI tool only allows you to execute at most only one command.

$ ansible webservers -a "w " -u admin
Ansible - Run Command on Multiple Linux Servers

Ansible – Run Command on Multiple Linux Servers

That’s all! In this article, we have explained how to run commands on multiple remote Linux servers at the same time using widely used tools. If you know of any tools out there for the same purpose, that we have not included in this article, let us know via the comment form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

16 Responses

  1. JhZ says:

    Which one do you suggest is the best among these four? I have tried PSSH to capture images from multiple raspberry pi’s successfully but the problem is it waits for around 7-10 sec if the number of nodes are more than 30. It hangs on camera 30 and starts again after around 10 sec.

  2. Vimal says:

    Thanks Aaron for this post!

    I tried PSSH, but it’s getting timed out, killed by signal 9. With PDSH, it’s getting timed out, ssh exited with exit code 255. Also, with Ansible, I’m not able to put my hosts in etc/ansible/hosts file (just readable), or create any other file in that directory. Can you please suggest any solution?

  3. I just wanted to add on here that this is now available for Ubuntu in several native repo’s (xenail and bionic) and the package name is pssh. So for your Ubuntu and Debian users just do apt install pssh.

  4. Jay Z says:

    I’ve been using “gsh” for a couple years. Its a simple parallel SSH app, but its nice because, I can maintain only one hosts file, but specify multiple categories for each host, and mix and match them with simple booleans to get the execution list of hosts I desire.

  5. Den Stark says:

    I’m using terminator and can broadcast commands to multiple modes/separate terminals

  6. Joe says:

    /etc/anasible/hosts

    ansible

  7. Sugar says:

    Is there a tool like these but with an additional feature: instead of manually editing a “host” file, other computers would notify a server that they are available for ssh?

  8. John west says:

    Xcat project spawned psh and dsh. Older version of psh had an option to use nodelist.tab file that contained:

    Host01 group1,webserver,rhel7
    Host02 group1,appserver,rhel6
    ....
    

    Psh. Group1 uptime

    Would run on both

    Psh rhel6 uptime

    would run only on host02

    Each server is listed once, not i. 10 different files.

    Later xcat switched to a db format for hosts, but was more complicated.

    Loved that nodelist.tab simplicity.

  9. Rick Maus says:

    Thanks for the article! Always looking for more options to perform similar tasks.

    When you want to interact with multiple hosts simultaneously, MobaXterm (mobaxterm.mobatek.net), is a powerful tool. You can even use your favorite text editor (vim, emacs, nano, ed) in real time.

    Each character typed is sent in parallel to all hosts and you immediately see the effect. Selectively toggling whether the input stream is sent to individual host(s) during a session allows for custom changes that only affect a desired subset of hosts.

    MobaXterm has a free home version as well as a paid professional edition. The company was highly responsive to issues reports that I provided and corrected the issues quickly.

    I have no affiliation with the company other than being a happy free edition customer.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.