MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Use gsh to Run Linux Commands Across Multiple Servers

Originally published on 2015-08-12; fully revised and updated.

gsh-run-linux-commands

gsh (Group Shell) is a remote shell multiplexor: it runs commands across multiple machines at once while keeping remote shells interactive and persistent. This is useful for sysadmins and DevOps engineers who need to check state, gather data, or run repeatable operations across server fleets over SSH.

What gsh is and when to use it

In the original source, gsh is described as a pluggable Python implementation related to DSH (Distributed Shell). It requires remote shell access (commonly SSH) to each target host, then lets you select systems by tags/macros and run one command across that set.

  • Run the same read-only check on many servers (kernel, OS release, disk usage).
  • Target hosts by groups/tags instead of manually listing hostnames.
  • Keep output grouped by host for quick triage.

How host targeting works with /etc/ghosts

gsh uses /etc/ghosts to map hosts to tags. You then call gsh with a systems expression (macros/tags) and a command.

# Machines
# hostname OS-Version Hardware OS cp security
linuxbrigade.com debian6 baremetal linux plesk iptables
linuxbrigade.com centos5 vps linux cpanel csfcluster
linuxbrigade.com debian7 baremetal linux plesk iptables
linuxbrigade.com centos6 vps linux cpanel csfcluster
linuxbrigade.com centos6 vps linux cpanel csfcluster
linuxbrigade.com centos6 vps linux nocp denyhosts
linuxbrigade.com debian6 baremetal linux plesk iptables
linuxbrigade.com centos6 baremetal linux cpanel csf
linuxbrigade.com centos5 vps linux cpanel csfCode language: CSS (css)

The original article shows expressions like cpanel+vps and web-RHEL4 for combining/splitting groups. Exact behavior depends on your ghosts(1) macro rules and local configuration.

Execution workflow

flowchart TD
    A[Set up SSH access to hosts] --> B[Define hosts and tags in etc ghosts]
    B --> C[Run gsh with systems expression and command]
    C --> D[gsh opens remote shells]
    D --> E[Command runs on selected hosts]
    E --> F[Output is returned per host]
    F --> G[Review results and rerun as needed]

Command syntax and option reference

gsh [OPTIONS] SYSTEMS CMD...Code language: CSS (css)

From the source: SYSTEMS is a combination of ghost macros, and CMD is the command to run remotely.

OptionPurposeTypical use
-h, --helpDisplay helpCheck available flags on your installed version
-d, --debugEnable execution debugging reportsTroubleshoot host matching or SSH execution
--no-host-prefixDo not prefix output lines with hostnameUse only when host identity is not needed in output
-s, --show-commandsShow command before output reportImprove auditability of what was executed
-n, --open-stdinKeep stdin open while runningInteractive scenarios (use carefully)
-l, --user USERSSH as a specific userRun as an ops account instead of current local user
-r, --run-locallyRun command locally, replacing $host with hostTemplate-like local command generation
-o, --self-remoteRun locally instead of SSH for local hostAvoid SSH to self when local host is in target set
-V, --versionShow version and exitValidate binary version during troubleshooting

Practical command examples

# Example from the original post
gsh linux "cat /etc/redhat-release; uname -r"

# Show command before output for better traceability
gsh -s linux "uptime"

# Run as a specific user
gsh -l root linux "df -h /"

# Example tag expressions from the original post
gsh cpanel+vps "hostname"
gsh web-RHEL4 "uname -a"Code language: PHP (php)

Command pattern comparison

PatternExampleWhen to use
Single tag/groupgsh linux "uptime"Operate on one broad fleet segment
Combined tagsgsh cpanel+vps "hostname"Narrow to hosts that match combined criteria
Split/exclusion stylegsh web-RHEL4 "uname -a"Target one set while removing another (per ghosts rules)
Explicit remote usergsh -l root linux "df -h /"Use controlled privileged account context

References

Best practices and common pitfalls

  • Set up SSH keys before using gsh at scale to avoid repeated password prompts.
  • Start with read-only commands first (for example, uptime, uname, cat) before making changes.
  • Keep host prefixes enabled for easier debugging across many targets.
  • Validate group/tag expressions on a small subset before broad execution.
  • Review ghosts(1) and gsh --help on your installed version, because option details can vary.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Linux Tutorials: Enable SSH Password authentication in linux servers

To allow SSH logins with a password on a Linux server, set PasswordAuthentication yes for the OpenSSH server (sshd), make sure no drop-in file overrides it, give…

Read More

How to login linux server using ssh?

You log in to a Linux server over SSH by running ssh username@server-ip from an SSH client, then proving who you are with either a password or…

Read More

Wetty – How to Install & Access Wetty (Web-based SSH) on AWS EC2 linux to access using browser

Wetty is a web-based terminal. It runs a small Node.js server that opens an SSH session and streams it to your browser over WebSockets, so you can…

Read More

Complete Tutorial: Use Two GitHub Accounts on One Mac with SSH

This tutorial explains how to configure two GitHub accounts on the same MacBook using separate SSH keys, without repeatedly switching accounts or breaking your existing Git configuration….

Read More

Development Workflows with an FTP and SFTP Client for Mac: From Local Files to Staging

An FTP and SFTP client for Mac is best suited when it displays the project that you are working on in one window and the remote server…

Read More

Linux Commands and Shell Scripting Reference for Linux Admins and DevOps Engineers

A structured Linux command and shell scripting reference for operations and DevOps work, rebuilt from the original link collection. It includes a practical learning workflow, command comparison tables, ready-to-use examples, and curated source links.

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month ago

Running commands across many Linux servers at once can save time, but it also increases the impact of mistakes. Start by testing commands on one or two hosts before expanding to the full group. Check that server lists are accurate and use secure SSH keys with limited permissions. For risky changes, keep logs of each host’s result and plan a rollback. These steps make bulk administration safer and easier to troubleshoot.

Rajesh kumar
Rajesh kumar
1 month ago

Running commands across several Linux hosts is convenient, but the bigger challenge is ensuring that every machine reaches the intended state. A command may succeed on one server and fail on another because of differences in package versions, permissions, disk space, or running services. I would capture each host’s exit status separately, set connection and execution timeouts, and generate a clear summary of successful, failed, and unreachable systems. For changes with a large blast radius, a staged rollout—starting with a small group of servers before expanding—can prevent one incorrect command from disrupting the entire fleet. This approach makes parallel administration much safer and easier to troubleshoot.

2
0
Would love your thoughts, please comment.x
()
x