↑ ↓ select, Enter open, Esc close

Run a command on multiple servers one after another

Adjust the values, the command updates live
user@server
for h in web1.example.com web2.example.com web3.example.com; do echo "== $h"; ssh -o BatchMode=yes -o ConnectTimeout=5 "$h" 'uptime; df -h /'; done

Connects to each host in turn and runs the command, printing the hostname as a header for each one. BatchMode=yes prevents password prompts, so servers without key access are simply skipped with an error. ConnectTimeout limits the wait for unreachable hosts.

Note: The risk level applies to the default command. Commands that change something affect all servers at once, so test them on a single host first.

Also searched as

  • run command on multiple servers ssh
  • ssh loop over hosts
  • execute same command on many servers

Related one-liners

All in SSH

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.