SSH: Execute Remote Command or Script – Linux

This is quite a common task for Linux system administrators, when it is needed to execute some command or a local Bash script from a one Linux workstation or a server on another remote Linux machine over SSH.

In this article you will find the examples of how to execute a remote command, multiple commands or a Bash script over SSH between remote Linux hosts and get back the output (result).

This information will be especially useful for ones, who want to create a Bash script that will be hosted locally on a one Linux machine but would be executed remotely on the other hosts over SSH.

Cool Tip: Connect to a remote SSH server without typing a password! Configure a passwordless authentication! Only 3 easy steps! Read more →

SSH: Execute Remote Command

Execute a remote command on a host over SSH:



Get the uptime of the remote server:

$ ssh root@ 'uptime'

Reboot the remote server:

$ ssh root@ 'reboot'

SSH: Run Multiple Remote Commands

In the most cases it is not enough to send only one remote command over SSH.

Much more often it is required to send multiple commands on a remote server, for example, to collect some data for inventory and get back the result.

There are a lot of different ways of how it can be done, but i will show the most popular of them.

Run multiple command on a remote host over SSH:


– or –


– or –

$ ssh USER@HOST << EOF

Cool Tip: SSH login is too slow? This can be fixed easily! Get rid of delay during authentication! Read more →


Get the uptime and the disk usage:

$ ssh root@ 'uptime; df -h'

Get the memory usage and the load average:

$ ssh root@ 'free -m | cat /proc/loadavg'

Show the kernel version, number of CPUs and the total RAM:

$ ssh root@ << EOF
uname -a
lscpu  | grep "^CPU(s)"
grep -i memtotal /proc/meminfo

SSH: Run Bash Script on Remote Server

The equally common situation, when there is some Bash script on a Linux machine and it needs to connect from it over SSH to another Linux machine and run this script there.

The idea is to connect to a remote Linux server over SSH, let the script do the required operations and return back to local, without need not to upload this script to a remote server.

Certainly this can be done and moreover quite easily.

$ ssh USER@HOST 'bash -s' < SCRIPT

Cool Tip: Want to ROCK? Start a GUI (graphical) application on a remote Linux workstation over SSH! Read more →


Execute the local on the remote server:

$ ssh root@ 'bash -s' <

4 Comment

  1. Nice Post there..I was wondering what if want to execute the script with an argument.

  2. Try this:

    $ ssh root@ 'bash -s' < " --argument"
  3. Hi,
    How about executing a command remotely and making sure that it will be killed once i kill my ssh session. or getting of PID of either SSH or the actual command on remote-node via a script.
    Something like this I want to achieve:
    Example: collecting pcap on remote addr and storing it locally.

     $ssh -t -f   "tcpdump -w -" > /tmp/tcp.pcap

    I was unable to get the proper Pids in this case. Any reply would be helpful.

    – Thanks.

Leave a Reply