Did You Know?
Got a tip? Let us know

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

Using DSH (Distributed Shell) to Run Linux Commands Across Multiple Machines

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators
CW

Systems Administrators know all too well the importance of being able to monitor and administer numerous machines in a short time, and preferably, with as little running around as possible. Whether it is a small cloud environment, or an enormous server cluster, the ability to centrally manage computers is essential.

DSH Commands

DSH Run Commands Across Multiple Servers

To partly accomplish this, I am going to show you how to use a nifty little tool called DSH that allows a user to run commands over multiple machines.

What is DSH?

DSH is short for “Distributed Shell” or “Dancer’s Shell” it is freely available on most major distributions of Linux, but can easily be built from source if your distribution does not offer it in its package repository. You can obtain the source at.

  1. http://www.netfort.gr.jp/~dancer/software/dsh.html.en

Install DSH (Distributed Shell) in Linux

We are going to assume a Debian / Ubuntu environment for the scope of this tutorial. If you are using another distribution, please substitute the appropriate commands for your package manager.

On Debian / Ubuntu

First, let’s install the package via apt:

$ sudo apt-get install dsh
On RHEL / CentOS / Fedora

This method is for those who are not using Debian, and want to compile it from source tar balls. First you need to compile “libdshconfig” and install.

# wget http://www.netfort.gr.jp/~dancer/software/downloads/libdshconfig-0.20.10.cvs.1.tar.gz
# tar xfz libdshconfig*.tar.gz 
# cd libdshconfig-*
# ./configure ; make
# make install

Then compile dsh and install.

# wget http://www.netfort.gr.jp/~dancer/software/downloads/dsh-0.22.0.tar.gz
# tar xfz dsh-0.22.0.tar.gz
# cd dsh-*
# ./configure ; make 
# make install

The main configuration file “/etc/dsh/dsh.conf” (For Debian) and “/usr/local/etc/dsh.conf” (for Red Hat) is pretty straightforward, but since rsh is an unencrypted protocol, we are going to use SSH as the remote shell. Using the text editor of your choice, find this line:

remoteshell =rsh

And change it to:

remoteshell =ssh

There are other options you can pass in here, if you choose to do so, and there are plenty of them to find on the dsh man page. For now, we are going to accept the defaults and have a look at the next file, /etc/dsh/machines.list (for Debian).

For Red Hat based systems you need to create a file called “machines.list” in “/usr/local/etc/” directory.

The syntax here is pretty easy. All one has to do is to enter in a machine’s credentials (Hostname, IP Address, or FQDN) one per line.

Note: When accessing more than one machine simultaneously, it would behove you to set up key-based password-less SSH on all of your machines. Not only does this provide ease of access, but security wise, it hardens your machine as well.

My “/etc/dsh/machines.list” or “/usr/local/etc/machines.list” file says:

172.16.25.125
172.16.25.126

Once you have entered in the credentials of the machines you wish to access, let’s run a simple command like `uptime` to all of the machines.

$ dsh –aM –c uptime
Sample Output
172.16.25.125: 05:11:58 up 40 days, 51 min, 0 users, load average: 0.00, 0.01, 0.05
172.16.25.126: 05:11:47 up 13 days, 38 min, 0 users, load average: 0.00, 0.01, 0.05

So what did this command do?

Pretty simple. First, we ran dsh and passed the “–a” option to it, which says to send the “uptime” command to “ALL” of the machines listed in “/etc/dsh/machines.list“.

Next, we specified the “–M” option, which says to return the “machine name” (specified in “/etc/dsh/machines.list“) along with the output of the uptime command. (Very useful for sorting when running a command on a number of machines.)

The “–c” option stands for “command to be executed” in this case, “uptime“.

DSH can also be configured with groups of machines in the “/etc/dsh/groups/” file, where is a file with a list of machines in the same format as the “/etc/dsh/machines.list” file. When running dsh on a group, specify the groupname after the “-g” option.

For Red Hat based systems you need to create a folder called “groups” in “/usr/local/etc/” directory. In that “groups” directory you create a file called “cluster“.

For example, run the “w” command on all machines listed in the “cluster” group file “/etc/dsh/groups/cluster” or “/usr/local/etc/groups/cluster“.

$ dsh –M –g cluster –c w

DSH provides much more flexibility, and this tutorial only scratches the surface. Aside from executing commands, DSH can be used to transfer files, install software, add routes, and much more.

To a Systems Administrator tasked with the responsibility of a large network, it is invaluable.

Rob is an avid user of Linux and Open Source Software, with over 15 years experience in the tech geek universe. Aside from experimenting with the many flavors of Linux, he enjoys working with BSDs, Solaris, and OS X. He currently works as an Independent IT Contractor.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

12 Responses

  1. Sarfaraz Shaikh says:

    You can also use clusterssh for running single command to multiple machine at a same time.

  2. Omipenguin says:

    Good article, But im getting this error

    Pseudo-terminal will not be allocated because stdin is not a terminal.

    System info
    =========
    Distributor ID: elementary OS
    Description: elementary OS Luna
    Release: 0.2
    Codename: luna

  3. JGV says:

    On CentOS/RHEL it’s just as easy as in Debian, no need to compile anything. Just:

    yum –enablerepo=epel install pdsh

    On Fedora, you don’t even need EPEL, it’s included in the standard repository:

    yum install pdsh

  4. Rob Krul says:

    @Omipenguin:

    What command are you trying to run over DSH?

  5. zcat says:

    Pretty similar to the script I’m using here.

    for host in {list of hosts}; do ssh $host “$@” &; done

  6. niraj says:

    How to add port other than 22 port.

    I have added the machine but still its give the following error
    dsh: no machine specified

    • Rob Krul says:

      @niraj

      The easiest way to set up ssh connections to other hosts is to use a ~/.ssh/config file. The syntax is pretty simple:

      Host (host)
      HostName (host name)
      IdentityFile ~/.ssh/your_id_file_rsa
      Port (port number)
      User (the username you use with SSH to login)

  7. MACscr says:

    Do how about dcp for file transfer? I dont see the option on my debian 7 system.

  8. er0k says:

    the -c flag does not mean the “command to be executed” , it means to run the command concurrently on all servers.

  9. liu says:

    thanks very much

  10. domain says:

    Hi! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.