4 tools for simultaneous execution of commands on multiple Linux servers

Original author: Aaron Kili
  • Transfer
The article, the translation of which we are publishing today, is devoted to technologies of simultaneous execution of commands on several Linux servers. Here we will talk about several well-known tools that implement such functionality. This material is useful for system administrators who, for example, regularly have to check the status of many remote systems. It is assumed that the reader already has several servers to which access via SSH is organized. In addition, when working simultaneously with several machines, it is very useful to configure SSH access to them by key, without a password . This approach, on the one hand, increases server security, and on the other, it facilitates working with it.

1. PSSH - Parallel SSH

PSSH is an open source command line toolkit written in Python and designed for parallel execution of SSH commands on a variety of Linux systems. It works quickly and is easy to learn. PSSH includes means such as parallel-ssh, parallel-scp , parallel-rsync, parallel-slurpand parallel-nuke(Details about these media can be seen in man).

Before installing parallel-sshon a Linux system, you must first install it pip. Here is how it is done in different distributions:

$ 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 parallel-sshset using pip:

$ sudo pip installparallel-ssh

Next, you need to enter the host names or IP addresses of remote Linux servers and port information in the file hosts(in fact, you can call it whatever you like). Here we need this command:

$ vim hosts

Here is an example of the contents of such a file:

After everything necessary is entered into the file, it is time to start parallel-sshby transferring the file name to this utility using the option -h, as well as the commands that need to be executed on all servers whose addresses are in the file hosts. The -iutility flag is used to display what goes into the standard output and error streams after completing the execution of commands on the servers.

The startup command parallel-sshmay look like this:

$ parallel-ssh -h hosts "uptime; df -h"

The following figure shows the use of the utility when working with three servers.

The parallel-ssh utility executes commands on multiple servers

2. Pdsh - Parallel Remote Shell Utility

Pdsh is, again, an open source solution, which is a shell for simultaneously executing commands on several Linux servers.

Here's how to install pdshin different distributions:

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

In order to execute commands on several servers, the addresses of these servers, as with use parallel-ssh, must be added to the file, which can also be called hosts. Then you need to run pdshin the following form:

$ pdsh -w ^hosts -R ssh "uptime; df -h"

Here, the flag -wis used to specify a file with a list of servers, the flag -Ris used to specify the module of remote commands (among the available modules are remote commands ssh, rsh, exec, is the default rsh). Note the icon ^in front of the file name with the list of servers.

This is what working with this team looks like.

Execution of commands on several servers using pdsh

If youpdshdid not specify a list of commands to be executed on serverswhen you called, this utility will run interactively. Detailspdshcan be found on the corresponding man page.

3. ClusterSSH

ClusterSSH is a command line tool designed to administer server clusters. It launches the administrator console and, for each server, a separate window xterm. After that on all these servers it is possible to simultaneously execute the same commands.

Install clusterssh:

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

Now, to connect to the servers, you need to run the following command:

$ clusterssh linode cserver contabo

You can also use this design:

$ clusterssh username@server1 username@server2 username@server3

After this, you will see something similar to what is shown in the following figure.

Working with multiple servers using clusterssh

Commands entered in the admin console are executed on all servers. To execute commands on a separate server, you need to enter them in a window open for it.

4. Ansible

Ansible is a popular open source tool for automating IT processes. It is used to configure and manage systems, to install applications and to solve other problems.

Install ansible:

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

After that you need to add server addresses to the file /etc/ansible/hosts.

$ sudo vim /etc/ansible/hosts

Here is an example of a fragment of a similar file with several systems grouped together webservers:

# Ex 2: A collection of hosts belonging to the 'webservers'group

Now, in order to get the command information uptimeand find out which users are connected to the hosts belonging to the group webservers, you can use the following construction:

$ ansible webservers -a "w " -u admin

Here the option is -aused to specify the arguments passed to the module, and the flag -uallows you to specify the default user name used to connect to remote servers via SSH.

Please note that the command line interface ansibleallows you to execute commands only one by one.

Interaction with multiple servers by means of ansible


In this article, we talked about tools that are designed to simultaneously execute commands on multiple servers running Linux. If you are thinking about automating tasks for managing multiple servers, we hope you will find something here that suits you.

Dear readers! Do you know of any useful utilities that simplify the administration of a large number of servers?

Also popular now: