Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Parallel Command Execution on Multiple Servers Using parallel-ssh

Published

Linux and Commands

Managing multiple servers simultaneously can be time-consuming and tedious when done sequentially. The parallel-ssh tool revolutionizes server management by enabling system administrators to execute commands across numerous servers concurrently, dramatically reducing deployment and maintenance time.

What is parallel-ssh?

Parallel-ssh (pssh) is a powerful command-line utility that extends the capabilities of SSH by allowing you to run commands on multiple remote hosts in parallel. Unlike traditional SSH that connects to one server at a time, parallel-ssh opens simultaneous connections to all specified hosts, executing your commands concurrently and collecting the results.

Installation

Installing parallel-ssh is straightforward across different operating systems:

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install pssh

CentOS/RHEL:

sudo yum install pssh

Using pip (Python package manager):

pip install parallel-ssh

Basic Usage

The fundamental syntax for parallel-ssh is simple and intuitive:

parallel-ssh -h hosts.txt -l username -A "command to execute"

Here’s what each parameter means:

  • -h hosts.txt specifies a file containing the list of target hosts
  • -l username defines the username for SSH connections
  • -A prompts for password authentication

Creating a Hosts File

The hosts file is a simple text document with one server per line. You can specify hosts using IP addresses or domain names:

192.168.1.10
192.168.1.11
web-server-01.example.com
web-server-02.example.com
database-server.example.com

You can also specify different users per host:

[email protected]
[email protected]
[email protected]

Practical Examples

Example 1: System Updates Across All Servers

Update all your Ubuntu servers simultaneously:

parallel-ssh -h servers.txt -l ubuntu -A "sudo apt-get update && sudo apt-get upgrade -y"

Example 2: Checking Disk Space

Monitor disk usage across your server fleet:

parallel-ssh -h servers.txt -l admin -A "df -h"

Example 3: Deploying Configuration Files

Use parallel-scp to copy files to multiple servers:

parallel-scp -h servers.txt -l admin -A nginx.conf /etc/nginx/nginx.conf

Example 4: Restarting Services

Restart a service across all web servers:

parallel-ssh -h web-servers.txt -l admin -A "sudo systemctl restart nginx"

Advanced Options

Using SSH Keys Instead of Passwords

For better security and automation, use SSH key-based authentication:

parallel-ssh -h servers.txt -l admin -i ~/.ssh/id_rsa "uptime"

Setting Parallelism Level

Control how many connections run simultaneously with the -p option:

parallel-ssh -h servers.txt -l admin -p 10 "command"

This limits parallel connections to 10 at a time, useful for managing server load.

Timeout Configuration

Set a timeout for command execution to prevent hanging operations:

parallel-ssh -h servers.txt -l admin -t 30 "long-running-command"

Saving Output to Files

Store command output from each server in separate files:

parallel-ssh -h servers.txt -l admin -o output/ "hostname && uptime"

This creates individual output files in the output/ directory for each host.

Useful parallel-ssh Suite Tools

The parallel-ssh package includes several complementary utilities:

  • parallel-scp: Copy files to multiple hosts in parallel
  • parallel-slurp: Copy files from multiple hosts to local machine
  • parallel-nuke: Kill processes on multiple hosts
  • parallel-rsync: Efficiently sync files using rsync to multiple hosts

Best Practices

To maximize efficiency and minimize risks when using parallel-ssh:

  • Test commands first: Always test your commands on a single server or small subset before running them across your entire infrastructure
  • Use SSH keys: Implement key-based authentication for better security and automation capabilities
  • Organize host files: Maintain separate host files for different server groups (web servers, database servers, staging environments)
  • Set appropriate timeouts: Configure timeouts based on expected command execution duration to avoid indefinite waiting
  • Monitor output carefully: Review command output from all hosts to catch any failures or unexpected results
  • Implement error handling: Use the -e option to save error output separately for easier troubleshooting
  • Control parallelism: Adjust the number of parallel connections based on your network capacity and server load

Common Use Cases

System administrators commonly use parallel-ssh for:

  • Deploying application updates and patches across multiple servers
  • Collecting system metrics and logs for monitoring and analysis
  • Performing security audits and compliance checks
  • Managing configuration files across server clusters
  • Executing database migrations on multiple database nodes
  • Restarting services after configuration changes
  • Running scheduled maintenance tasks

Troubleshooting Tips

If you encounter issues with parallel-ssh:

  • Connection failures: Verify SSH connectivity to each host individually using standard ssh command
  • Permission errors: Ensure your user has appropriate sudo privileges if running privileged commands
  • Timeout issues: Increase timeout values for long-running operations
  • Authentication problems: Verify SSH keys are properly configured and added to authorized_keys on remote hosts
  • Output confusion: Use the -o option to save outputs to separate files for easier review

Conclusion

Parallel-ssh is an indispensable tool for modern system administrators managing multiple servers. By enabling simultaneous command execution across your infrastructure, it saves significant time and reduces the potential for human error in repetitive tasks. Whether you’re managing a handful of servers or hundreds of nodes, mastering parallel-ssh will dramatically improve your operational efficiency and streamline your deployment workflows.

Start small by testing parallel-ssh on a few servers, then gradually expand its use as you become more comfortable with its capabilities. With proper implementation and adherence to best practices, parallel-ssh becomes a cornerstone of efficient server management.

Related guides

Account services are unavailable until the production backend is configured.