Operations 7 min read

Simplify Multi‑Server Management with SSH Config and Custom Scripts

Learn how to create an SSH configuration file and two Bash scripts that let you run commands across multiple Ubuntu and Rocky Linux servers with a single command, eliminating repetitive manual SSH sessions and reducing error risk.

21CTO
21CTO
21CTO
Simplify Multi‑Server Management with SSH Config and Custom Scripts

Linux offers countless ways to reach the same destination, and choosing the right path depends on you. Managing several servers manually via SSH can be time‑consuming and error‑prone.

Creating an SSH Config File

First, create ~/.ssh/config with entries for each host:

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.

Ubuntu Management Script

Create ~/ubuntu-cmd and paste the following Bash script:

#!/bin/bash
[[ -z ${@} ]] && exit || CMD_EXEC="${@}"
HOSTS=$(grep -Po 'Host\s\Kubuntu-.*' "$HOME/.ssh/config")
if [[ $CMD_EXEC =~ ^sudo ]]; then
  read -sp '[sudo] password for remote_admin: ' password; echo
  CMD_EXEC=$(sed "s/^sudo/echo '$password' | sudo -S/" <<< "$CMD_EXEC")
fi
while IFS= read -r host; do
  echo -e '
\033[1;33mHOST: ${host}\033[0m'
  ssh -n "$host" "$CMD_EXEC &>" | tee -a "/tmp/$(basename "${0}").${host}."
done <<< "$HOSTS"

Make it executable and move it to /usr/local/bin:

sudo mv ubuntu-cmd /usr/local/bin
sudo chown $USER /usr/local/bin/ubuntu-cmd
sudo chmod u+x /usr/local/bin/ubuntu-cmd

Rocky Linux Management Script

Create ~/rocky-cmd with a similar script that selects hosts prefixed by rocky-:

#!/bin/bash
[[ -z ${@} ]] && exit || CMD_EXEC="${@}"
HOSTS=$(grep -Po 'Host\s\Krocky-.*' "$HOME/.ssh/config")
if [[ $CMD_EXEC =~ ^sudo ]]; then
  read -sp '[sudo] password for remote_admin: ' password; echo
  CMD_EXEC=$(sed "s/^sudo/echo '$password' | sudo -S/" <<< "$CMD_EXEC")
fi
while IFS= read -r host; do
  echo -e '
\033[1;33mHOST: ${host}\033[0m'
  ssh -n "$host" "$CMD_EXEC &>" | tee -a "/tmp/$(basename "${0}").${host}."
done <<< "$HOSTS"

Deploy it the same way:

sudo mv rocky-cmd /usr/local/bin
sudo chown $USER /usr/local/bin/rocky-cmd
sudo chmod u+x /usr/local/bin/rocky-cmd

Using the Scripts

Run a command on all Ubuntu hosts with:

ubuntu-cmd sudo apt-get update && sudo apt-get upgrade -y

The script prompts for the sudo password, executes the command on each host, and logs the output.

Run a command on all Rocky hosts similarly: rocky-cmd sudo dnf update -y Both scripts iterate over the configured hosts, executing the supplied command remotely, thereby streamlining server management without relying on heavyweight tools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationLinuxScriptingSSHserver management
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.