Master Remote Command Execution with SSH: From Simple Commands to Scripts
This guide demonstrates how to use SSH on Linux to run single commands, chain multiple commands, handle interactive commands with pseudo‑terminals, execute multi‑line scripts, and run both local and remote scripts, providing practical examples and tips for automation.
Remote Command Execution
SSH is the fundamental tool for remote connections on Linux, but its power goes far beyond simple login; it can automate many remote operations.
Running a Single Command
You can execute df -h on a remote host without logging in:
ssh [email protected] "df -h"Running Multiple Commands
Separate commands with a semicolon and wrap them in quotes: ssh [email protected] "pwd; cat hello.txt" The first command returns the home directory, and the second displays the file content. When using multiple commands, always quote the entire command string to ensure all commands run remotely.
Executing Interactive Commands
Commands that require a TTY (e.g., sudo ls /root or top) fail unless you allocate a pseudo‑terminal with -t: ssh -t [email protected] "sudo ls /root" Adding -t forces SSH to keep the session open until the interactive command finishes.
Running Multi‑Line Commands
You can embed several lines of commands inside a quoted string, using mixed single and double quotes when needed.
Executing Local Scripts Remotely
Create a script test.sh locally (e.g., containing ls and pwd) and pipe it to the remote host: ssh [email protected] < test.sh To pass arguments, use the -s option with bash:
ssh [email protected] 'bash -s' < test.sh helloworldExecuting Scripts Stored on the Remote Server
If the script resides on the remote machine, invoke it with its absolute path: ssh [email protected] "/home/nick/test.sh" Arguments can be supplied after the script path, and they appear as $0 and $1 inside the script.
Summary
This article demonstrates the basic SSH remote‑operation techniques—running single or multiple commands, handling interactive sessions, executing multi‑line commands, and running both local and remote scripts—laying the groundwork for more complex automation tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
