Pssh – Execute Commands on Multiple Remote Linux Servers

No doubt, that OpenSSH is one of the most widely used and powerful tools available for Linux, which allows you to connect securely to remote Linux systems via a shell and allows you to transfer files securely to and from remote systems.

But the biggest disadvantage of OpenSSH is that you cannot execute the same command on multiple hosts at one go and OpenSSH is not developed to perform such tasks.

This is where the Parallel SSH or PSSH tool comes in handy, is a Python-based application, that allows you to execute commands on multiple hosts in parallel at the same time.

PSSH tool includes parallel versions of OpenSSH and related tools such as:

  • pssh – is a program for running ssh in parallel on multiple remote hosts.
  • pscp – is a program for copying files in parallel to a number of hosts.
  • prsync – is a program for efficiently copying files to multiple hosts in parallel.
  • pnuke – kills processes on multiple remote hosts in parallel.
  • pslurp – copies files from multiple remote hosts to a central host in parallel.

These tools are good for System Administrators who find themselves working with large collections of nodes on a network.

Install PSSH or Parallel SSH on Linux

In this guide, we shall look at steps to install the latest version of the PSSH (i.e. version 2.3.1) program on RHEL-based distributions such as Fedora, CentOS Stream, Rocky & AlmaLinux and Debian-based distributions such as Ubuntu and Linux Mint using the pip command.

The pip command is a small program (replacement of easy_install script) for installing and managing Python software packages index.

On RHEL-Based Distributions

On RHEL distributions, you need to first install the pip package (i.e., python-pip) on your system to install the PSSH program using yum or dnf.

yum install python-pip
OR
dnf install python-pip

Once you’ve installed the pip tool, you can install the pssh package using the pip command, as shown below.

pip install pssh  

On Debian-Based Distributions

On Debian-based distributions, it takes a minute to install pssh using the pip command.

sudo apt install python-pip
sudo pip install pssh

How Do I Use pssh in Linux?

When using pssh you need to create a host file with the number of hosts along with the IP address and port number that you need to connect to remote systems using pssh.

The lines in the host file are in the following form and can also include blank lines and comments.

pssh hosts file
192.168.0.10:22
192.168.0.11:22

Run Single Command On Multiple Linux Servers

You can execute any single command on different or multiple Linux hosts on a network by running a pssh command. There are many options to use with pssh as described below:

We shall look at a few ways of executing commands on a number of hosts using pssh with different options.

  • To read host file, include the -h host_file-name or --hosts host_file_name option.
  • To include a default username on all hosts that do not define a specific user, use the -l username or --user username option.
  • You can also display standard output and standard error as each host completes. By using the -i or --inline option.
  • You may wish to make connections time out after the given number of seconds by including the -t number_of_seconds option.
  • To save standard output to a given directory, you can use the -o /directory/path option.
  • To ask for a password and send it to SSH, use the -A option.

Let’s see a few examples and usage of pssh commands:

Run Commands on Multiple Linux Hosts

1. To execute echo “Hello TecMint” on the terminal of the multiple Linux hosts by the root user and prompt for the root user’s password, run this command below.

Important: Remember all the hosts must be included in the host file.

pssh -h pssh-hosts -l root -A echo "Hello TecMint"

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 15:54:55 [SUCCESS] 192.168.0.10:22
[2] 15:54:56 [SUCCESS] 192.168.0.11:22

Note: In the above command “pssh-hosts” is a file with the list of the remote Linux server’s IP address and SSH port number that you wish to execute commands.

Find Disk Usage of Multiple Linux Hosts

2. To find out the disk space usage on multiple Linux servers on your network, you can run a single command as follows.

pssh -h pssh-hosts -l root -A -i "df -hT"

Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 16:04:18 [SUCCESS] 192.168.0.10:22
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    38G  4.3G   32G  12% /
tmpfs          tmpfs  499M     0  499M   0% /dev/shm
/dev/sda1      ext4   190M   25M  156M  14% /boot

[2] 16:04:18 [SUCCESS] 192.168.0.11:22
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        30G  9.8G   20G  34% /
devtmpfs                devtmpfs  488M     0  488M   0% /dev
tmpfs                   tmpfs     497M  148K  497M   1% /dev/shm
tmpfs                   tmpfs     497M  7.0M  490M   2% /run
tmpfs                   tmpfs     497M     0  497M   0% /sys/fs/cgroup
/dev/sda1               xfs       497M  166M  332M  34% /boot

Find Uptime of Multiple Linux Hosts

3. If you wish to know the uptime of multiple Linux servers at one go, then you can run the following command.

pssh -h pssh-hosts -l root -A -i "uptime"
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 16:09:03 [SUCCESS] 192.168.0.10:22
 16:09:01 up  1:00,  2 users,  load average: 0.07, 0.02, 0.00

[2] 16:09:03 [SUCCESS] 192.168.0.11:22
 06:39:03 up  1:00,  2 users,  load average: 0.00, 0.06, 0.09

You can view the manual entry page for the pssh command to get many other options to find out more ways of using pssh.

pssh --help
pssh commands and usages
pssh commands and usages

Summary

Parallel SSH or PSSH is a good tool to use for executing commands in an environment where a System Administrator has to work with many servers on a network. It will make it easy for commands to be executed remotely on different hosts on a network.

Hope you find this guide useful and in case of any additional information about pssh or errors while installing or using it, feel free to post a comment.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

42 Comments

Leave a Reply
  1. This is great. Would I be able to execute scripts that are physically present on multiple remote systems such that they execute locally?

    Example: Tell the remote systems to simultaneously execute a script that reboots the system.

    Reply
  2. Firstly thanks for sharing this valuable post, but if it possible please elaborate on how to use pssh with customizing ssh ports no.

    Reply
  3. In CentOS 7 PSSH is available from the epel repo. All you have to do is yum install epel-release then yum install pssh. That’s it!

    Anyway thanks for the article.

    Reply
  4. I see there is a " -l " option to pass userid. How do I pass password of the host pro-grammatically. That is, the password must be sent from a script based on password entered in some web page.

    Reply
  5. I want to run multiple commands on servers instead of single command or is there any way that pssh read commands from a file one by one and print the output on terminal ?

    Reply
    • @Jk

      Yes, you specify the commands in a shell script, eg. commands.sh and make it executable. Then use the -I flag to run commands from it, like this:

      #pssh -h hosts.txt -l ec2-user -P -I<./commands.sh

      The flag:
      -h – specifies host file
      -l – specifies username
      -P – displays output as it arrives.
      -I – reads and sends commands from a script to ssh processes

      Try this out, thanks.

      Reply
  6. Good morning, is it possible to execute a command on different server using root user but every server has got a different root password and I don’t want install a key on every servers? If I use the following command:

    # pssh -h pssh-hosts -l root -A -i "uptime"
    

    the command ask to me the password, I enter the password of the first server, the command work for that server but failed on the others, I suppose pssh use the same password I insert for every server.

    Thank you in advance for any help. Regards

    Reply
    • @Marco

      That would be complicated, i suppose the easiest way to use Pssh is to install a key on every servers. I can not certainly tell if you can use it with different root passwords on the different servers.

      Reply
    • @Jose,

      PSSH is for execute commands on multiple servers in parallel at the same time.

      MPSSH – Mass Parallel Secure Shell executes multiple parallel ssh binary instances on a list of hosts.

      Reply
  7. I am try the following command

    # pssh -h /root/hosts.txt -l root -i -A -O StrictHostKeyChecking=no 'echo -e "NewPass\nNewPass" | passwd'
    

    And I am receiving this error message , any idea ?

    # [1] 10:11:31 [FAILURE]   Exited with error code 1
    

    And no password was changed.

    Reply
  8. Stderr:
    —————————————————————–
    This network/computer system is for the use of authorized
    users only. Users (authorized or unauthorized) should have
    no explicit or implicit expectations of privacy. Individuals
    using this network/computer system without authority, or in
    excess of their authority, are subject to having all of their
    activities on this network/computer system monitored, recorded,
    and disclosed by system personnel. In the course of monitoring
    individuals improperly using this network/computer system, or
    in the course of system operation or maintenance or for the
    purpose of protecting the rights or property of the system
    provider, the activities of authorized users maybe monitored,
    recorded, and disclosed. Anyone using this network/computer
    system expressly consents to such monitoring, recording, and
    disclosure and is advised that if such monitoring reveals
    possible evidence of criminal activity, system personnel may
    provide the resulting evidence to law enforcement officials
    —————————————————————–
    ============
    how to exclude the stderr for this

    Reply
  9. Am trying to copy file from my local machine to remote servers , i need it to copy using pscp i have tried above command in my linux terminal. can you suggest me

    Reply
    • @Deepak,

      Have you created PSSH host file with IP address of remote host? If yes, try to run the following command to copy file from local to remote location.

      # pscp -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
      

      If you still unable to do so, please post the output of the command here, so that we can help you out..

      Reply
  10. Hi this is a very good article, but i think it is only works when you have root password.
    And also one it will not work if root having different passwords on other servers.
    Am i right??
    Do you know any other commands for sudo users for the same purpose?

    Reply
  11. How to solve this when connecting?

    “Are you sure you want to continue connecting (yes/no)?
    Host key verification failed.”

    and then moved on to the next IP.

    Reply
    • If you are using private and public key connection method, try to check that they exist on the hosts you are connecting to.

      Reply
  12. Just out of curiosity, how is this different from pdsh, the parallel distributed shell which has been running ssh commands in parallel for at least 15 years or more.
    -mark

    Reply
      • A slight difference could be in the way you specify hosts. When using pssh you can write host details in a file and issue commands to the listed hosts, you can also exclude certain hosts when using pdsh by using the -x option which is not a common functionality of pssh.

        Reply
    • @

      @Ham,
      Thanks for telling us about that tsshbatch tool, we will try to write on separate article on this tool in our upcoming article..

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.