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.
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] 220.127.116.11 18.104.22.168 22.214.171.124
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.