Run a Local Shell Script on a Remote SSH Server


In this tutorial, How to Run a Local Shell Script on a Remote SSH Server. You can pass entire scripts over SSH without having the .sh file on the remote Linux server.

To run a local shell script on a remote SSH server, you can use the ssh command with the following syntax:

ssh user@remote_host 'bash -s' <

Here’s a breakdown of the command:

  • user: Replace this with the username for the remote SSH server.
  • remote_host: Replace this with the IP address or hostname of the remote SSH server.
  • Replace this with the path to your local shell script that you want to run on the remote server.

Pass The Script Over to Standard Input

ssh user@remotehost 'bash -s' <
  • The bash -s command means “execute the following commands in a new bash session.”
  • The -s flag makes it read from the standard input
  • The < bit will read a local script file into standard input.

The output terminal is as follows:

[vagrant@localhost ~]$ cat
echo "Target server"
cd /home
[vagrant@localhost ~]$
[vagrant@localhost ~]$ ssh vagrant@ 'bash -s' <
vagrant@'s password:
Target server
 14:00:46 up 21 min,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0         13:40   17:54   0.00s  0.00s -bash
[vagrant@localhost ~]$

Running Remote Commands Inside a Script

ssh vagrant@ 'bash -s' <<'ENDSSH'
  # The following commands run on the remote host
  lsb_release -a
  ls -l

The <<‘ENDSSH’ directive makes a “here-document” structure

The output as the picture below

Run a Local Shell Script on a Remote SSH Server


You have to Run a Local Shell Script on a Remote SSH Server. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.