Skip to content

Running a Local Shell Script on a Remote Server Using SSH

Steps
3
Covers
SSH
On this page 3 steps

Running a shell script from your computer on a remote server using SSH is very useful. This is especially true for system administrators and developers who need to manage servers far away.

For example, imagine you are a system administrator. You need to update software on many servers that are not in the same location as you. Instead of logging into each server one by one, you can use a shell script to do this job for all servers at once. This saves you a lot of time and effort.

Here is a simple guide to show you how to do it step-by-step:

Prerequisites

  • SSH access to the remote server.
  • A local shell script that you want to execute remotely.
  • SSH client installed on your local machine.

Step 1 of 3Ensure SSH Access

Before proceeding, make sure you have SSH access to the remote server. You can test this by running:

ssh username@remote-server-ip

Replace username with your remote username and remote-server-ip with the IP address or hostname of the remote server.

Step 2 of 3Create Your Local Script

Create a shell script on your local machine. For example, let’s name it check_mem.sh.


#!/bin/bash

echo "Current date and time: $(date)"
echo "Free memory on the server:"
free -h

#echo "Memory usage summary:"
#vmstat -s

Make sure the script is executable:

chmod +x check_mem.sh

Step 3 of 3Execute Script Directly Over SSH

Instead of copying the script to the remote server, you can execute it directly using SSH. Use this command:

ssh username@remote-server-ip 'bash -s' < check_mem.sh

This command will run check_mem.sh directly on the remote server without needing to copy it first.

Running Shell Script on a Remote Using SSH
Running Shell Script on a Remote Using SSH

Conclusion

Running a shell script from your computer on a remote server using SSH is a powerful skill. It saves you time and makes managing multiple servers much easier. Just write your script, make it executable, and use SSH to run it on the remote server. This simple guide helps you do all this step-by-step.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top