Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to run a command on multiple servers at once  (Read 1455 times)

0 Members and 1 Guest are viewing this topic.

Amal John Ronkha

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
How to run a command on multiple servers at once
« on: July 14, 2018, 01:02:40 pm »
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:

Code: [Select]
$ 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.

Code: [Select]
$ sudo yum install clusterssh
To install ClusterSSH on Fedora, simply run:

Code: [Select]
$ 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.

Code: [Select]
$ 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.

Code: [Select]
$ 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.

Code: [Select]
$ 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.