How to Run the Same Command on Multiple Linux Servers

When you manage several Linux servers, even a simple disk space check can become repetitive. Connecting to each machine, typing the same command, and comparing the results takes time.

Fortunately, you can run commands on multiple servers using SSH scripts, Parallel SSH, Ansible, or a graphical tool such as Server Explorer. The right method depends on whether you need a quick inspection, reusable automation, or an interactive administration workspace.

1. Run commands on multiple servers with a Bash loop

If you already have SSH access to your servers, a Bash loop is a straightforward starting point. Configure your hosts in ~/.ssh/config or replace the aliases below with valid SSH destinations such as deploy@192.0.2.10.

for host in web-01 web-02 worker-01; do
  printf 'n--- %s ---n' "$host"
  ssh -o ConnectTimeout=10 -o BatchMode=yes "$host" 'df -h' ||
    printf 'Command failed on %sn' "$host" >&2
done

This script connects to each server and displays its filesystem usage. The host label makes the output easier to identify, while the connection timeout limits how long SSH waits to establish a connection.

BatchMode=yes prevents interactive authentication prompts. Set up SSH keys, unlock them with an SSH agent if necessary, and verify each server’s host key before running the script.

The loop runs sequentially: the next server is contacted only after the previous command finishes. A connection timeout does not limit the runtime of the remote command itself.

For occasional checks on a handful of servers, this method requires little setup beyond an SSH client and Bash.

2. Execute commands simultaneously with Parallel SSH

Parallel SSH, also known as PSSH, lets you execute commands concurrently rather than waiting for each server in turn.

On Debian or Ubuntu, install the package on your administration machine:

sudo apt install pssh

Create a file named hosts.txt containing your SSH destinations:

deploy@web-01
deploy@web-02
deploy@worker-01

Then run:

parallel-ssh -h hosts.txt -p 3 -t 30 -i 'df -h'

The executable may be named pssh instead of parallel-ssh, depending on the package and operating system.

Here, -h specifies the host list, -p limits concurrent connections to three, -t sets a command timeout in seconds, and -i displays the remote output inline.

Parallel execution is useful when checking a larger fleet. However, you still need to manage your host list, SSH authentication, and command output. Review individual failures instead of assuming that every server returned a successful result.

3. Use Ansible to run commands across an inventory

Ansible is a better fit when occasional commands evolve into repeatable infrastructure management tasks. It supports inventory groups, reusable playbooks, and structured execution results.

For typical Linux hosts, Ansible connects over SSH. Most modules require Python on the remote machines.

Create an inventory file named inventory.ini:

[web]
web-01 ansible_user=deploy
web-02 ansible_user=deploy
[workers]
worker-01 ansible_user=deploy

After installing Ansible on your control machine, run the following command:

ansible all -i inventory.ini -m ansible.builtin.command -a 'df -h'

To check only the web servers, target the web group:

ansible web -i inventory.ini -m ansible.builtin.command -a 'uptime'

Ansible executes tasks concurrently within its configured fork limit and reports results for each host. The command module executes a program directly; shell features such as pipes and redirections require a different approach, such as the shell module.

For recurring configuration changes, prefer purpose-built Ansible modules and playbooks over arbitrary shell commands. They can make infrastructure changes more predictable and repeatable.

4. Run commands on multiple servers with Server Explorer

Server Explorer is a desktop SSH client for macOS and Windows that brings server administration tools into one workspace. Its Multi-CLI feature offers a graphical approach to executing the same command across multiple connected Linux servers.

Instead of maintaining a separate host file for a quick inspection, work with the servers you have already configured in the application.

Connect to the relevant servers and open Multi-CLI. Review the intended targets before submitting a command, especially if your workspace includes both staging and production machines.

Multi-server SSH command execution in Server Explorer.
Execute commands across multiple Linux servers from Server Explorer’s Multi-CLI workspace

Start with a read-only command such as:

hostname

Use the returned hostnames to confirm that you are working with the intended machines.

Next, check disk usage across those servers:

df -h

Review the output for each target and identify machines that need further investigation. A failed or disconnected server should not be treated as a successful check.

Run commands on multiple servers simultaneously with Server Explorer.
Check disk usage across multiple Linux servers with a single command

The advantage of this workflow is that command execution sits alongside other administration tools, including file browsing, process monitoring, Docker management, and system services. You can investigate an individual server without switching to a separate administration application.

Multi-CLI is suited to interactive operations. For scheduled automation, reproducible deployments, or configuration management, a dedicated automation tool such as Ansible remains a complementary choice.

Practical commands to run across Linux servers

Start with commands that inspect system state rather than modify it.

CommandWhat it checks
hostnameServer identity
uptimeUptime and load averages
df -hFilesystem capacity and available space
free -hMemory and swap usage
uname -rRunning Linux kernel version
docker --versionInstalled Docker CLI version
systemctl is-active nginxCurrent Nginx service state

Some commands depend on installed software or the operating system. For example, systemctl requires systemd, and docker --version checks the CLI version, not whether the Docker daemon is healthy.

To check whether Nginx is active on several web servers, run:

systemctl is-active nginx

An inactive or missing service can return a nonzero exit code. Inspect the output before deciding whether any intervention is necessary.

Check systemd service status across multiple SSH servers.
Check the status of Nginx across multiple Linux servers simultaneously

Which method should you choose?

MethodExecutionBest for
Bash and SSHSequential in the example aboveSmall, occasional checks and simple scripts
Parallel SSHConcurrentAd hoc command execution across a fleet
AnsibleConcurrent, inventory-basedRepeatable infrastructure automation
Server ExplorerGraphical multi-server executionInteractive server administration

A Bash loop is often sufficient for a few machines. Parallel SSH reduces waiting time when commands are independent, while Ansible is better suited to managing infrastructure consistently over time.

Server Explorer is an option for developers who want multi-server commands alongside graphical administration tools rather than a separate command-line workflow.

Safety tips for running commands on multiple servers

Executing a command across several machines increases its potential impact. A mistake that would affect one VPS can quickly become a fleet-wide incident.

Never disable SSH host key verification merely to make multi-server execution easier. Verify new host keys through a trusted channel instead.

Remember that parallel command execution does not guarantee that every host finishes at the same time or that all operations succeed. For changes that require ordering, rollback, or coordinated deployment, use an automation workflow designed for those requirements.

Conclusion

Running commands on multiple Linux servers eliminates repetitive SSH sessions and makes routine checks easier. Bash and Parallel SSH are practical for lightweight tasks, while Ansible offers a foundation for more structured automation.

If your daily work involves checking servers, inspecting files, monitoring processes, and managing Docker, Server Explorer combines interactive multi-server command execution with those administration tools in a single desktop workspace.

Server Explorer

Bring your servers into focus.

A desktop app for macOS and Windows. Connect over SSH.

Download

Leave a Reply

Your email address will not be published. Required fields are marked *