How to run a command on multiple servers at once

If you maintain multiple Linux servers, 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

To install ClusterSSH on Ubuntu, Debian or Linux Mint:

$ sudo apt-get install clusterssh

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

$ sudo yum install clusterssh

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.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

13 thoughts on “How to run a command on multiple servers at once

  1. Or get SecureCRT for windows/linux/mac
    Open as many hosts as you want go to view - chat windows
    a small windows will appear on the bottom - right click on it and select "send to all windows"
    Start entering commands and they will appear in all tabs within that window :)

  2. In 4 cases out of 5, by the time you reach for cssh, you should really be investing your time in proper automation of that environment (cfengine, chef, puppet, etc). I myself can't think of a situation when cssh is actually useful.?

  3. And for all the lazy typers (like me):
    Instead of:
    cssh -l dev host1 host2 host3
    you can also write:
    cssh -l dev host{1..3}

  4. Does anyone know configure cssh connect to a cluster if the hosts are accessible only through a jump server (central ssh gateway )?

    It seems nobody writes a tutorial about the use case scenario when to connect to machine X you need to first connect to machine Y

    Cannot find any info on that anywhere..

Leave a comment

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