How to run a command on multiple servers at once

Last updated on July 15, 2020 by Dan Nanni

If you maintain multiple Linux servers or VPS instances, there are cases where you want to run the same command(s) on all the servers. For example, you may want to install/upgrade packages, patch the kernel, and update configurations, etc. It will be a tedious job if you have to log in to each server and run the same commands manually. This post is about an administrative tool that allows you to run the same commands on many different machines at once.

ClusterSSH enables you to make the same change on multiple hosts at once. It provides a special console interface where anything you type into the console is automatically sent to as many hosts as you want.

Install ClusterSSH on Linux

For Ubuntu, Debian or Linux Mint:

To install ClusterSSH on Ubuntu, Debian or Linux Mint:

$ sudo apt-get install clusterssh

For CentOS or RHEL:

To install ClusterSSH on CentOS or RHEL, first you need to set up EPEL repository, and then run the following.

$ sudo yum install clusterssh

For Fedora:

To install ClusterSSH on Fedora, simply run:

$ sudo yum install clusterssh

Configure ClusterSSH

After installation, the first step is to define a cluster of hosts that you want to run commands on. To do that, create a system-wide ClusterSSH configuration file as follows.

$ sudo vi /etc/clusters
clusters = my_cluster my_cluster2
my_cluster = host1 host2 host3 host4
my_cluster2 = host5 host6

If you want a user-specific ClusterSSH configuration, simply use ~/.csshrc instead of /etc/clusters. In the above example configuration, I define two clusters: my_cluster consisting of four hosts, and my_cluster2 with two hosts. A cluster is a group of hosts which you want to log in to, and run commands on.

When you launch ClusterSSH with any user-defined cluster, it will use ssh to log in to individual hosts in the cluster, and run any user-typed commands on the hosts.

Launch ClusterSSH

To launch ClusterSSH, run cssh command as follows.

$ cssh -l dev my_cluster

In the above, dev is a login ID for all the hosts in the cluster, and my_cluster is the cluster name.

If you want, you can specify individual hostnames instead of the cluster name.

$ cssh -l dev host1 host2 host3

Once cssh command is executed, it will pop up XTerm windows for individual hosts, as well as a small window labeled "CSSH [2]", which is ClusterSSH console window. Whatever you type in the console window will simultaneously appear in the XTerm windows of individual hosts. Essentially, you control all XTerm windows via the single console window.

If you want to run some commands to a specific XTerm window, you can simply switch focus to the Xterm window, and type the commands as you usually would.

The following screenshot shows ClusterSSH in action, where there are five hosts in the cluster, and the console window in the upper left corner is where you are supposed to type the commands to run on all five hosts.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean