Linux: Run Command in Many Servers Simultaneously

Server administrators usually have many servers to manage. There will be some time when we need to run the same command in every server we have. In my case, I need to update MySQL version on all 6 database servers we have to the latest version via yum. It just need to run simple command but I need to repeat the process 6 times for each server. Furthermore, we will keep doing this in the future over and over again.

To achieve this, I am going to use Webmin cluster. So all servers must have been installed with Webmin and we need to integrate all of them and joining the Webmin cluster. In this tutorial, I will only use 2 servers as example of setup.

Variables as below:

OS: CentOS 6.2 64bit
Webmin (master)/MySQL server #1: 192.168.0.160
Webmin (node1)/MySQL server #2: 192.168.0.163
Webmin username: root
Webmin password: Gn&Pe42#e

1. Download and install Webmin for all servers. You need to repeat step number 1 to 3 on all servers:

$ cd /usr/local/src
$ wget http://cdnetworks-kr-2.dl.sourceforge.net/project/webadmin/webmin/1.580/webmin-1.580-1.noarch.rpm
$ rpm -Uhv  webmin-1.580-1.noarch.rpm

2. Configure firewall to open port 10000 for Webmin communication. Add following line into /etc/sysconfig/iptables before any REJECT line (-j REJECT):

-A INPUT -p tcp -m tcp --dport 10000 -j ACCEPT

Save the file and restart firewall:

$ service iptables restart

3. Start Webmin:

$ service webmin start

4. In master node, access the Webmin via http://192.168.0.160:10000 and login as root. Go to Webmin > Webmin Server Index > and click “Register a new server“. Enter the server information as screenshot below:

And click “Save“.

5. The server should now register in the Webmin but not joining the cluster yet. Go to Webmin > Cluster > Cluster Webmin Servers > and click Add Server for “this server” and “192.168.0.163“.

6. If the joining is completed, you should see all servers in the managed list like below:

7. Now we can start to run any command on all servers. What I need to do now is go to Webmin > Cluster > Cluster Shell Commands > and enter the command that I need to run and which server you want it to run. I will choose “<All hosts>” and click “Run Command Now”  as screenshot below:

The output will then appeared from both of the servers. In my real case, I need to repeat step 4,5,6 to add another 4 servers and finally just execute the command as step 7. This simple setup is just a great way for your server administration in the future. Cheers!