4 Useful Tools to Run Commands on Multiple Linux Servers

Linux

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 AlsoHow to Configure Custom SSH Connections to Simplify Remote Access

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-rsyncparallel-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.

Then install parallel-ssh using pip as follows.

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):

pssh hosts file

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.

Pssh Run Commands on Multiple Linux Servers

Pssh Run Commands on Multiple Linux Servers

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.

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 - 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:

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.

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 - Administer Multiple SSH Sessions

Clusterssh – Administer Multiple SSH Sessions

For more information, see the clusterssh man page:

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:

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

Specify them in groups, e.g webservers.

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 - 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.

Source

Share: