Mastering SSH: Run Remote Commands, Scripts, and Interactive Sessions
Learn how to leverage SSH for remote command execution, including running single and multiple commands, handling interactive sessions with pseudo‑terminal allocation, executing multi‑line commands, and running both local and remote scripts with parameter passing, all illustrated with practical examples.
SSH is the fundamental tool for remote connections on Linux, and beyond simple logins it can automate a wide range of remote operations.
Remote Command Execution
You can run a command on a remote host without logging in, for example to check disk usage: ssh [email protected] "df -h" Multiple commands can be chained with a semicolon: ssh [email protected] "pwd; cat hello.txt" The first command returns the home directory ( /home/nick), and the second displays the contents of hello.txt. When using multiple commands, always enclose them in quotes to ensure they run remotely.
Executing Interactive Commands
Commands that require a TTY, such as sudo ls /root or top, fail without a pseudo‑terminal. Adding the -t option forces TTY allocation, keeping the session alive until the interactive command exits.
ssh -t [email protected] "sudo ls /root" ssh -t [email protected] "top"The official description of -t is: "Force pseudo‑terminal allocation. This can be used to execute arbitrary screen‑based programs on a remote machine…" Multiple -t options can be used for extra assurance.
Executing Multi‑Line Commands
Multi‑line commands can be sent by opening a single‑ or double‑quoted block and writing several lines before closing the quote. When the command itself contains quotes, mix single and double quotes to avoid conflicts.
Remote Script Execution
Running a Local Script on a Remote Host
Create a local script test.sh (e.g., containing ls and pwd) and pipe it to SSH: ssh [email protected] < test.sh To pass arguments, use bash -s on the remote side:
ssh [email protected] 'bash -s' < test.sh helloworldThe script receives bash as $0 and helloworld as $1.
Running a Script Stored on the Remote Server
If test.sh resides in the remote user's home directory, execute it by specifying the absolute path: ssh [email protected] "/home/nick/test.sh" Arguments can be passed similarly:
ssh [email protected] /home/nick/test.sh helloworldThe script outputs the script name as $0 and helloworld as $1.
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.
