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
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"
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"
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
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]
For more information, see the clusterssh man page:
$ man clusterssh
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] 126.96.36.199 188.8.131.52 184.108.40.206
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.
-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
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.
18 thoughts on “4 Useful Tools to Run Commands on Multiple Linux Servers”
I have been using ClusterSSH for a while now. It’s nothing short of terrific.
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.
Ansible, the most popular configuration management, and application-deployment tool.
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?
You can try gsh: https://github.com/globocom/gsh, mentioned by Jay Z.
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.
Thanks for updating us about Pssh, but we already covered a detailed article on same here: How to Run Multiple Commands on Multiple Linux Servers Using Pssh
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.
I’m using terminator and can broadcast commands to multiple modes/separate terminals
Thanks for the useful feedback.
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?
Some of these tools allow you to add the hosts on the command line, without necessarily using a hosts file. You can read their man pages for more information.
Xcat project spawned psh and dsh. Older version of psh had an option to use nodelist.tab file that contained:
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.
Many thanks for the useful feedback.
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.
Many thanks for sharing this seful information.