Systems Administrators are well aware of the importance of being able to monitor and administer numerous machines in a short amount of time, preferably with minimal physical movement. Whether it’s a small cloud environment or a large server cluster, the ability to centrally manage computers is essential.
To partly accomplish this, I am going to show you how to use a nifty little tool called DSH (dancer’s shell/distributed shell) that allows a user to run commands across multiple machines.
What is DSH?
DSH is short for ‘Distributed Shell‘ or ‘Dancer’s Shell‘, which is a freely available tool on most major distributions of Linux but can be easily built from a source if your distribution does not include it in its package repository.
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-based distributions, you can install DSH using the following apt command in the terminal.
sudo apt install dsh
On RHEL-based distributions, you need to compile it from source tar balls, but before doing so, make sure to compile and install the ‘libdshconfig‘ library.
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 sudo make install
Then compile dsh and install.
wget https://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 sudo make install
How to Use DSH in Linux
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, 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 the “/usr/local/etc/” directory.
The syntax here is pretty easy. All you have to do is enter in a machine’s credentials (Hostname, IP Address, or FQDN) one per line.
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 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 Does the ‘dsh’ 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 the “/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.
The -c command does not mean “command to be executed”. According to the manual, it means “concurrent shell”.
–concurrent-shell | -c
Executes shell concurrently.
See http://pwet.fr/man/linux/commandes/dsh
If you have 1000 servers, It will ask 1000 times to put yes/no to known hosts and for first time to add ssh-key again it will ask 1000 times for password, then what is the use of this???
@Nakul,
Then you should go for Pscp tool to achieve the same
Well if you set config for ssh to auto accept new known_hosts and establish pre-shared ssh keys then there’s no prompting at all!
echo “StrictHostKeyChecking no” >> ~/.ssh/config
and then connect to your thousand servers.
They’ll end up in known hosts
and then remove that line from ~/.ssh/config
you can use “StrictHostKeyChecking=no” in your /etc/ssh/sshd.config and restart the sshd and run the dsh command.
dsh is the ancestor, pdsh improved it and is much better, and clustershell is now a better pdsh. Available in EPEL (CentOS , Fedora) and Debian and ubuntu http://clustershell.readthedocs.io/
@Aurelien,
Thanks for informing us about clustershell, we will definitely test run right away and write a detailed article on this, till then stay tuned..
For those of you who are asking about ports, I can suggest setting up config file in ~/.ssh/config. The file should contain something like:
Host server1.net
HostName 192.168.0.100
user root
Port 22
IdentityFile ~/.ssh/your_id
Host server2.net
HostName 192.168.0.101
User root
Port 12345
IdentityFile ~/.ssh/your_id
Host server3.net
HostName 192.168.0.102
User root
Port 54321
IdentityFile ~/.ssh/your_id
There is also tool , called cssh — it will work on any distro without “modern” libraries
An alternative that come standard with Red Hat and Fedora is pdsh, installable with yum/dnf
yum install pdsh
https://pdsh.googlecode.com/
@Norm,
Thanks for informing us about that pdsh took, let me give a try and see how it is useful than dsh, will test it and write a detailed article on this, till then stay tuned..
-c does not mean “command”:
–concurrent-shell | -c
Executes shell concurrently.
err: username@host:port (not IP at the end… No undo)
Try putting username@host:ip in the group or list file and see if it will find the proper port for your instance.
Hi,
I have 3 machines in machinelist. but i can’t enter ssh key phrase for first and second, first and second asking key
dsh -aM -c uptime
[email protected]’s password: [email protected]’s password: [email protected]’s password:
only can enter 3rd server password
How can use this with other SSH ports than 22. this only works with ssh port 22. How can use this with other ports
thanks , very usefully ,
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?
thanks very much
the -c flag does not mean the “command to be executed” , it means to run the command concurrently on all servers.
Do how about dcp for file transfer? I dont see the option on my debian 7 system.
How to add port other than 22 port.
I have added the machine but still its give the following error
dsh: no machine specified
@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)
Pretty similar to the script I’m using here.
for host in {list of hosts}; do ssh $host “$@” &; done
@Omipenguin:
What command are you trying to run over DSH?
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
Dear Jorge,
Thanks for the findings, we really not aware of such pdsh tool. Will update the article.
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
You can also use clusterssh for running single command to multiple machine at a same time.