Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Run Multiple Commands on Multiple Linux Servers

Improve Article
Save Article
  • Last Updated : 07 Nov, 2022
Improve Article
Save Article

In this article, we’ll learn how to run multiple commands on multiple Linux servers. It was a manual task completed by System Administrators. Let’s say, they made some changes to a server and if they need to make the same changes to many servers they would need to log into each server and repeatedly make changes to each of them. But we can run multiple commands on multiple servers at the same time and automate them.

There are many ways to do that, but we’ll use Ansible which is a free and open-source tool widely used to automate IT processes.

Install Ansible

Follow the below steps to install Ansible:

Step 1: Use pip in your selected Python environment to install the Ansible package for the current user:

python3 -m pip install --user ansible

installing ansible

Step 2: Verify that you’ve installed Ansible.

ansible --version


Run commands on servers

Step 1: Generate public-private key pair for your servers so that you or ansible can connect to your servers. We need to generate these because ansible uses an ssh connection to login into the server(s).

ssh-keygen -t rsa

generating public-private key pair

Step 2: Copy your public key to all of your servers so that they can verify that you’re the authorized user. 

ssh-copy-id -f -i ~/.ssh/ anurag@


Step 3: Create a host file /etc/ansible which will contain your servers’ IP addresses or hostname. Ansible needs this file to know where it needs to perform actions.

sudo touch /etc/ansible


Step 4: Add your server’s hostnames or IP addresses on which you want to run multipin the /etc/ansible file

e.g: For demo purposes, We have 2 application servers and 1 database server.



  • [app] block is a group of servers that runs our application
  • [db] block is a group of servers that runs our database
  • [servers: children] is a group of our application and database servers
  • [servers: vars]  add variables to the servers group that will be applied to all servers within servers and all of its children.

Step 5: Run commands on servers

Example 1: Ping all servers to check whether we’re able to connect to our servers or not.

ansible servers -i /etc/ansible -m ping

where the -i  flag is used for specifying the host file which contains the IP addresses or hostname of your servers and -m  for the module.


As you can see we get pong as the response back which means that we’re able to connect to our servers.

Example 2: Check the hostname of each server.

ansible servers -i /etc/ansible -a "hostname"

where -i  flag is used for specifying the host file which contains IP addresses or hostname of your servers and -a for specifying arguments that we want to run on our server(s)

checking hostname

Look we get the hostname of our all servers by running a single command. 

Example 3: Check free memory on servers

ansible servers -i /etc/ansible -a "free -m"

checking memory

Here, we get the memory status which is how much the servers have in total memory, used memory, free memory, etc.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!