VOOZH about

URL: https://thenewstack.io/linux-run-a-single-command-across-multiple-servers-with-ssh/

⇱ Linux: Run a Single Command across Multiple Servers with SSH - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-03-23 06:00:57
Linux: Run a Single Command across Multiple Servers with SSH
Linux / Operations / Security

Linux: Run a Single Command across Multiple Servers with SSH

You don't always need third-party software to control multiple Linux servers — not when a bit of scripting magic can get the job done.
Mar 23rd, 2024 6:00am by Jack Wallen
👁 Featued image for: Linux: Run a Single Command across Multiple Servers with SSH
Feature image by petterijokela from Pixabay.

With Linux, there are always numerous routes to wind up at the same location. That’s actually part of the strength of this operating system: It’s up to you how to get from point A to point B. Think of it as the “Mad Libs” of operating systems.

Take, for instance, the ability to run commands on remote machines. There are plenty of options, such as Red Hat Ansible, Puppet and Chef, which are all amazing platforms but may be a bit overkill for what you need.

Consider this: You have a workstation and 5 or 10 servers you need to manage. You can use that workstation and Secure Shell into each server, run the command necessary, exit from the server, and rinse, wash, and repeat until you’ve taken care of everything.

That’s time-consuming and can lead to problems. Imagine if you ran the wrong command on the wrong machine. You don’t want that.

You also want to simplify as much as possible, which is why this handy SSH tip can make your life a bit easier.

Let’s say you have a number of Ubuntu Servers and Rocky Linux servers, each of which you want to ensure are always up to date. As I mentioned earlier, you can log into each Ubuntu server and issue the command:

Then, you can log into each Rocky Linux server and issue the command:

sudo dnf update -y

That may take a bit more time than you probably have to spare. So, how do we make Secure Shell (SSH) do the heavy lifting for us? Thanks to SSH config files, it’s actually fairly easy.

Let me show you how.

Creating Your SSH Config File

The first thing we’ll do is create an SSH config file. We’ll stick with our example of Ubuntu and Rocky Linux servers. Create the config file with the command:

nano ~/.ssh/config

Remember, you’ll want to create the file as the user who will run the command. We’re going to create four entries in this file (you can create as many as needed) — two for Ubuntu Servers and two for Rocky Linux servers — with the following information:

  • ubuntu-web at 192.168.1.100
  • ubuntu-db at 192.168.1.101
  • rocky-web at 192.168.1.102
  • rocky-db at 192.168.1.103

Those can be named anything you like, but it’s important that the names start with either Ubuntu or Rocky. The config entries will look like this:

Host ubuntu-web
Hostname 192.168.1.100

Host ubuntu-db
Hostname 192.168.1.101

Host rocky-web
Hostname 192.168.1.102

Host rocky-db
Hostname 192.168.1.103

Save and close the file.

Next, we have to create two scripts that will accept user input for the command to be run, get the hosts from the config file, test to see if the command run requires sudo permission, and then loop over the hosts to execute the commands.

Create the first script with the command:

nano ~/ubuntu-cmd

You can name that file anything you’d like. In the file, paste the following content:

Save and close the file.

Next, we’re going to move the scripts to /usr/local/bin:

sudo mv ubuntu-cmd /usr/local/bin

In that file, paste the following:

Next, we’re going to move the scripts to /usr/local/bin:

sudo mv ubuntu-cmd /usr/local/bin
sudo mv rocky-cmd /usr/local/bin

Next, change the ownership of the scripts:

sudo chown $USER /usr/local/bin/ubuntu-cmd
sudo chown $USER /usr/local/bin/rocky-cmd

Finally, give the files executable permissions:

sudo chmod u+x /usr/local/bin/ubuntu-cmd
sudo chmod u+x /usr/local/bin/rocky-cmd

How to Use the Scripts

Let’s say you want to send the sudo apt-get update && sudo apt-get upgrade -y command to your Ubuntu servers. For that, you would issue the following command:

You would be prompted for your sudo password on the remote machine, and, once you’ve successfully authenticated, it would run the commands and then iterate to the next host.

You’d then do the same thing with the Rocky hosts, like so:

rocky-cmd sudo dnf update -y

Again, it would iterate through all of your configured hosts and run the command on each.

Congratulations! You’ve just made your Linux server management life a bit easier … without having to rely on more complicated, third-party platforms to do the heavy lifting.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
The Linux Foundation and Red Hat are sponsors of The New Stack. 
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.