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.

SSH: Execute Remote Command

Execute a remote command on a host over SSH:

$ ssh USER@HOST 'COMMAND'

Examples

Get the uptime of the remote server:

$ ssh root@192.168.1.1 'uptime'

Reboot the remote server:

$ ssh root@192.168.1.1 '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:

$ ssh USER@HOST 'COMMAND1; COMMAND2; COMMAND3'

– or –

$ ssh USER@HOST 'COMMAND1 | COMMAND2 | COMMAND3'

– or –

$ ssh USER@HOST << EOF
COMMAND1
COMMAND2
COMMAND3
EOF

Examples

Get the uptime and the disk usage:

$ ssh root@192.168.1.1 'uptime; df -h'

Get the memory usage and the load average:

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

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

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

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

Example

Execute the local script.sh on the remote server:

$ ssh root@192.168.1.1 'bash -s' < script.sh
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Start a GUI Application on a Remote Computer using SSH

This article describes how to log into the remote computer (server) using SSH and run a GUI...

SSH Login Slow — Removing Delay

Problem: When I’m trying to log into the remote server via SSH, after I enter the UserName, it...

SSHPass: SSH Login With Password – Command Line

A password-based authentication is often a default way to connect to a remote host over SSH. But...

Signing Failed: Agent Refused Operation [SOLVED]

While attempting to connect to some server over SSH, you may get the error as follows:...

SSH Fingerprint: Get Fingerprint of SSH RSA Key

The fingerprint is a unique sequence of letters and numbers used to identify the SSH RSA key. It...