Home Linux Tips & Tricks How To Run Single Command On Multiple Remote Systems At Once

How To Run Single Command On Multiple Remote Systems At Once

By sk
Published: Updated: 3.5K views

A while ago, we published a guide to "copy a file to multiple directories in command line on Linux". In that article, we taught you various methods to copy a single file to multiple directories at once. Today, we are going to see how to run a single command on multiple remote systems at once in Unix-like operating systems. As you already know, we can access and communicate with remote system using ssh. openSSH allows us to do all sorts of administration tasks in a remote system. One limitation with openSSH is we can't run the single command on multiple remote systems at once. No problem. Here is where PSSH comes to fill this gap.

What is PSSH?

PSSH, or Parallel SSH, is a command line suite that helps you to ssh in parallel on a number of hosts. PSSH suite consists of  the following commands:

  • pssh - SSH to multiple remote systems in parallel,
  • pscp - Copy files in parallel to a number of hosts,
  • prsync : Copy files in parallel to a number of hosts,
  • pnuke : Kill processes in parallel on a number of hosts,
  • pslurp : Copy files in parallel from a number of hosts.

In this tutorial, we will see how to execute a single command on multiple hosts at once using PSSH.

Install PSSH in Linux

We can easily install PSSH using PIP, a python package manager.

To install PIP on Arch Linux and its derivatives, run:

$ sudo pacman -S python-pip

On RHEL, Fedora, CentOS:

$ sudo yum install epel-release
$ sudo yum install python-pip

On latest RHEL and its clones (AlmaLinux and Rocky Linux), run:

$ sudo dnf install epel-release
$ sudo dnf install python-pip

On Debian, Ubuntu, Linux Mint, Pop OS:

$ sudo apt-get install python-pip

For more details about managing python packages using PIP, refer the following link.

Once PIP installed, run the following command to install PSSH.

$ sudo pip install pssh

PSSH has been installed! Let us go ahead and see how to use it.

Run Single Command On Multiple Remote Systems At Once Using PSSH

Important: In order to use PSSH (for the purpose of this tutorial only), all your remote systems must have a common username with same password. Otherwise, this method won't help. Say for example, I have already created an user named sk with password ostechnix on all my remote hosts. You should have a same user with same password on all your remote systems as well.

Now, let us see how to run a single command on multiple remote hosts using PSSH. Go to your local system where you want to run the command and create a text file called remotehosts.txt. You can name it as you wish.

$ vi remotehosts.txt

Add IP addresses of your remote hosts with port numbers one by one as exactly shown below.

192.168.1.103:22
192.168.1.104:22

Where, 192,168.1.103 and 192.168.1.104 are the IP addresses of my remote systems. 22 is the ssh port number. You need to mention the correct port number if you've already changed it. Also, make sure you can be able to access all remote hosts from your local system via ssh.

Now, let us check the uptime of both remote hosts from our local system. To do so, run:

$ pssh -h remotehosts.txt -l sk -A -i "uptime"

Here,

  • remotehosts.txt - Contains the IP addresses of both remote systems.
  • sk - the username of both remote systems

Enter the password of the user "sk".

Sample output:

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 20:51:15 [SUCCESS] 192.168.1.103:22
 20:50:50 up 8 min, 1 user, load average: 0.05, 0.11, 0.10
[2] 20:51:15 [SUCCESS] 192.168.1.104:22
 20:50:52 up 12 min, 1 user, load average: 0.00, 0.07, 0.12

As you see above, we have run the "uptime" command on two remote hosts and got the result in one go.

What about the kernel version? To check the installed version of both remote hosts, run:

$ pssh -h remotehosts.txt -l sk -A -i "uname -r"

Sample output:

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 20:53:09 [SUCCESS] 192.168.1.103:22
3.10.0-327.22.2.el7.x86_64
[2] 20:53:09 [SUCCESS] 192.168.1.104:22
4.4.0-21-generic

Very cool, isn't? Can we create a directory on both remote hosts at once? Yes, of course! To do so, run the following command:

$ pssh -h remotehosts.txt -l sk -A -i "mkdir dir1"

Similarly, you can do anything you want to do on multiple remote hosts from your local system using PSSH.

Important: Please be very careful while using PSSH. One bad command will perform simultaneously on multiple hosts and damage all hosts. So, be very careful while using this method in production. I suggest you to test this in a virtual machines. Once you're familiar with PSSH, you can use it on production if you like to.


Suggested read:


Hope this helps.

You May Also Like

4 comments

Brian Tague February 5, 2017 - 2:34 pm

In the directions it is stated that the user name and password has to be the same for all hosts but has the following text: (for the purposes of this tutorial only). So do the user names and passwords have to be the same to use this utility or no?

Reply
SK February 5, 2017 - 2:47 pm

Yes, the user name and password has to be the same for all hosts.

Reply
rpodgorny February 8, 2017 - 10:23 am

be sure to check omnirun ( https://github.com/rpodgorny/omnirun ) for far more flexibility (including parallel execution, sudo support, script support, flexible usage of usernames/passwords, tags, …)

Reply
Rsutton February 7, 2020 - 7:55 pm

Or, you can use cssh which brings up each host in an xterm window so that you can see what’s going on.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More