If you are managing multiple Linux servers, and you want to run multiple commands on all the Linux servers, but you have no idea about how to do it. There is no need to worry, in this simple server management guide, we will show you how to run multiple commands on multiple Linux servers simultaneously.
To achieve, this you can use the pssh (parallel ssh) program, a command line utility for executing ssh in parallel on a number of hosts. With it, you can send input to all of the ssh processes, from a shell script.
Requirements
- Install Pssh to Run Commands on Multiple Remote Linux Servers
- You must be using SSH passwordless authentication for all remote servers.
Create a Shell Script
Therefore, you need to start by preparing a script which contains the Linux commands you want to execute on the different servers. In this example, we will write a script that will collect the following information from multiple servers:
- Check uptime of servers
- Check who is logged on and what they are doing
- List top 5 running processes according to memory usage.
First create a script called commands.sh with your favorite editor.
# vi commands.sh
Next, add the following commands to the script as shown.
#!/bin/bash ############################################################################### #Script Name : commands.sh #Description : execute multiple commands on multiple servers #Author : Aaron Kili Kisinga #Email : [email protected] ################################################################################ echo # show system uptime uptime echo # show who is logged on and what they are doing who echo # show top 5 processe by RAM usage ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head -n 6 exit 0
Save the file and close it. Then make the script executable as shown.
# chmod +x commands.sh
Create PSSH Hosts File
Next, add the list of servers that you want to run the commands on, in a hosts.txt file, in the format [user@]host[:port]
or simply give the server IP addresses.
But we suggest you use ssh aliases which can be specified in .ssh/config file as explained in how to configure custom ssh connections to simplify remote access.
This method is more efficient and reliable, it allows you to specify configuration options (such as host name, identify file, port, username etc..) for each remote server.
Following is our sample ssh hosts aliases file a.k.a user specific ssh configuration file.
# vi ~/.ssh/config
Next, create a hosts.txt file, here you can simply specify the aliases (names defined using Host keyword in .ssh/config file) as shown.
# vi hosts.txt
Add the server aliases.
server1 server2 server3
Run Commands via a Script on Multiple Linux Servers
Now run the following pssh command by specifying hosts.txt file along with the script that contains multiple commands to run on multiple remote servers.
# pssh -h hosts.txt -P -I<./commands.sh
Meaning of the flags used in the above command:
-h
– reads the hosts file.-P
– tells pssh to display output as it arrives.-I
– reads input and sends to each ssh process.
That’s It! In this article, we showed how to execute multiple commands on multiple servers in Linux. You can share any thoughts relating to this topic via the comment section below.
Hi from the above how can I generate the webserver.pem and centos.pem. Please guide me to generate
.epm
file.@upen
Here is an example, where
.crt
is the certificate and.key
is the key:Hi Aaron, the command “pssh -h hosts.txt -P -I<./commands.sh" doesn't work for me. Always get "[FAILURE] server1 Exited with error code 255" ERROR. any assistance would be useful.
Me to also getting the same please help me on this.
thanks a lot for this! very useful and handy
@Joseph
Welcome, thanks for liking this article and for the feedback.
be sure to also check omnirun (https://github.com/rpodgorny/omnirun)!
@Radek
Many thanks for sharing this link, we will surely check it out.