SSH
Remote Computing
Shell Script
Networking
Tech Tips

How to use SSH to run a local shell script on a remote machine?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Typical applications include remote command-line login and remote command execution, but any network service can be secured with SSH.

The Basics of SSH

SSH allows you to connect to a remote machine and execute commands securely. The most typical use case is logging into a remote server to perform administrative tasks. SSH uses various forms of encryption to ensure that all communication between the client and the server remains secure and private.

Running Local Shell Scripts on a Remote Machine

Sometimes, you may need to run a script stored on your local machine on a remote server. This can be done effectively using SSH. Here's a step-by-step guide on how to do it:

Step 1: Write Your Script

First, ensure your shell script on the local machine is ready. For this example, we will use a simple script that creates a directory.

mkdir.sh:

bash
#!/bin/bash
mkdir -p new_directory

Ensure that the script is executable:

bash
chmod +x mkdir.sh

Step 2: Copy the Script to the Remote Machine

You can use SCP (Secure Copy Protocol) to copy the script from your local machine to the remote server where it needs to be executed. Here's how you can do it:

bash
scp mkdir.sh username@remotehost:/path/to/destination

Replace username with your actual username on the remote system, remotehost with the hostname or IP address of the remote server, and /path/to/destination with the path where you want to store the script on the remote server.

Step 3: SSH into the Remote Machine and Execute the Script

Once the script is on the remote server, you can log into the server via SSH and execute the script:

bash
ssh username@remotehost

Navigate to the script's location:

bash
cd /path/to/destination

Run the script:

bash
./mkdir.sh

Step 4: Executing the Script Directly via SSH

Instead of copying the script to a remote machine, you can execute it directly using SSH. This can be achieved by combining cat and SSH:

bash
cat mkdir.sh | ssh username@remotehost bash

This command uses cat to read the script and then pipes it directly through SSH where it's executed by the bash shell on the remote machine.

Tips and Considerations

  • Permissions: Ensure your script has the right permissions on both your local machine and the remote server.
  • Shebang Line: Always include a shebang line (#!/bin/bash) at the top of your scripts to specify which interpreter should execute the script.
  • Error Handling: Consider adding error handling into your scripts to manage any potential issues that arise during execution.
  • Security: Be cautious with the scripts you execute remotely. Ensure they come from trusted sources and do not inadvertently expose sensitive information or access.

Summary Table

ActionCommandNotes
Make script executablechmod +x script.shPrepare script for execution
Copy script to remote via SCPscp script.sh username@remote:~/path/Securely copies script to remote
Execute script on remote via SSHssh username@remote 'bash -s' < ./script.shDirect execution without copying
Direct execution with pipe`cat script.sh \ssh username@remote bash`Uses local script contents remotely

By following these steps, you will be able to run a local shell script on a remote machine securely and efficiently using SSH. This enhances your capabilities in managing remote servers and automating tasks across multiple machines.


Course illustration
Course illustration