TNS
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%
NEW! Try Stackie AI
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
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.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.