linux ssh执行命令
It is common to execute commands on many nodes/hosts via SSH for managing a cluster of Linux servers. On Linux, there are many choices for this task. Generally, to run commands on many nodes, there are two modes: serial mode and parallel mode. In serial mode, the command is executed on the node one by one. In parallel mode, the command is executed on many nodes together. The serial mode is easy to reason about with and debug while the parallel mode is usually much faster.
通常会通过SSH在许多节点/主机上执行命令来管理Linux服务器集群。 在Linux上,此任务有很多选择。 通常,要在许多节点上运行命令,有两种模式:串行模式和并行模式。 在串行模式下,该命令在节点上一个接一个地执行。 在并行模式下,命令将在多个节点上一起执行。 串行模式易于使用和调试,而并行模式通常要快得多。
In this post, I will introduce 3 methods of executing commands on many nodes on Linux: using Bash, clustershell and pdsh. The methods are introduced by an example: execute the command hostname
on nodes “lnode31 lnode6 cluster1-1 cluster1-2 … cluster1-8” as user “root”.
在本文中,我将介绍在Linux的许多节点上执行命令的3种方法:使用Bash,clustershell和pdsh。 通过示例介绍这些方法:以用户“ root”的身份在节点“ lnode31 lnode6 cluster1-1 cluster1-2…cluster1-8”上执行命令hostname
。
使用bash执行命令∞ (Execute commands using Bash ∞)
Run commands in serial order (one by one) using Bash over SSH
使用SSH上的Bash以串行顺序(一对一)运行命令
for h in lnode31 lnode6 cluster1-{1..8} ; do
ssh root@$h hostname
done
Run commands in parallel using Bash over SSH
使用SSH上的Bash并行运行命令
for h in lnode31 lnode6 cluster1-{1..8} ; do
ssh root@$h hostname &
done
wait
Pros: Bash is almost always available on Linux nodes. You can do certain checking logic after each ssh invoking.
优点:Bash在Linux节点上几乎总是可用。 您可以在每次ssh调用后执行某些检查逻辑。
Cons: The length of the command is a little bit long.
缺点:命令的长度有点长。
使用执行命令clustershell
∞ (Execute commands using clustershell
∞)
clustershell
/clush
is a program for executing commands in parallel on a cluster. clush
can also gather the commands’ results. If you haven’t installed it on the managing node, you can install the package clustershell
(on Fedora Linux).
clustershell
/ clush
是用于在集群上并行执行命令的程序。 clush
还可以收集命令的结果。 如果尚未在管理节点上安装它,则可以安装软件包clustershell
(在Fedora Linux上)。
Run commands in parallel using clustershell over SSH
通过SSH使用clustershell并行运行命令
$ clush -l root -w lnode31,lnode6,cluster1-[1-8] hostname
Pros: clush
is designed for parallel execution. clush
can also execute commands interactively.
优点: clush
专为并行执行而设计。 clush
还可以交互执行命令。
Cons: You will need to install the software on the managing node.
缺点:您将需要在管理节点上安装软件 。
使用执行命令pdsh
∞ (Execute commands using pdsh
∞)
pdsh
is a variant of the rsh command while pdsh
can run multiple remote commands in parallel. pdsh
can also run in interactive mode. If you haven’t installed it on the managing node, you need to install the package pdsh
abd pdsh-rcmd-ssh
(on Fedora Linux) first.
pdsh
是rsh命令的变体,而pdsh
可以并行运行多个远程命令。 pdsh
也可以在交互模式下运行。 如果尚未在管理节点上安装它,则需要首先安装软件包pdsh
abd pdsh-rcmd-ssh
(在Fedora Linux上)。
Run commands in parallel using pdsh over SSH
通过SSH使用pdsh并行运行命令
$ pdsh -R ssh -l root -w lnode31,lnode6,cluster1-[1-8] hostname
For more usage of pdsh
, check the pdsh
manual page.
有关pdsh
更多用法,请查看pdsh
手册页 。
Pros and Cons: similar to those of clush
. In addition, pdsh
support other rcmd modules other than ssh such as rsh and exec.
优点和缺点:类似于clush
。 另外, pdsh
支持除ssh之外的其他rcmd模块,例如rsh和exec。
These 3 methods should help managing a cluster of Linux nodes easier. Having other favorite tools to execute ssh commands? Share it with us by commenting.
这三种方法应有助于更轻松地管理Linux节点集群。 还有其他喜欢的工具来执行ssh命令吗? 通过评论与我们分享。
翻译自: https://www.systutorials.com/three-methods-execute-commands-many-nodes-parallel-ssh-linux/
linux ssh执行命令
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-01-11 mysql 5.7 安装 (压缩包方式 .tar.gz)
2019-01-11 C/C++程序CPU问题分析
2012-01-11 List<T>.Contains(T item)判断是否包含的根据是什么