How To Remotely Execute Commands Using SSH?

SSH or Secure Shell is a cryptographic network protocol for securely getting access to a remote computer using a terminal. It provides a secure channel over the unsecured network to execute commands and operate a system remotely.

There are different ways using which you can execute a command or script remotely. To execute a command on the remote system first you need to authenticate the remote system and then you can execute the command.

In this article, we will discuss how to execute a command or script on a remote system through SSH.

Prerequisites

In order to follow this guide, you should have the following.

  • You should have ssh client install on your system
  • The remote computer to which you want to connect should have ssh server installed
  • You should have user access to the remote system

How to connect a remote system using SSH

To connect to a remote system using ssh first open your terminal by pressing ctrl+alt+t and then execute the following command –

ssh [email protected]_ip_address

For example-

ssh [email protected]

This will authenticate you through the configured authentication method.

As you can see local hp-pc is now connected with remote acer-pc.

OR you can use the hostname instead of using IP address this will look something like this –

ssh [email protected]

Executing a single command remotely using SSH

Once you are connected to the remote system you can start executing commands.

For example, you can check the current working directory on the remote system using the given command.

uname -a

Alternatively, you can use the given command to connect remote system and execute the command in one go –

ssh [email protected] uname -a

As you can see in the image it shows the same output but in the local computer’s shell or terminal.

Executing multiple commands remotely using SSH

To execute multiple commands remotely first connect to the remote system and then execute commands in the group.

For example –

uname -mrs; date; uptime

OR you can use the alternate method to connect and execute commands in one go –

ssh [email protected] uname -mrs; date; uptime

Executing a local script remotely using SSH

Using a bash script you can execute multiple or multiline commands on a system. A local script can be executed against a remote machine with a simple stdin redirection.

For example –

VAR1="Variable 1"
ssh [email protected] '
uptime
date
uname -rms
if true; then
    echo "True"
    echo $VAR1
else
    echo "False"
fi
'

Let’s say you save this file with the name test.sh then you can use the given command to execute it remotely.

ssh [email protected] < test.sh

If you get a warning message like Pseudo-terminal will not be allocated because stdin is not a terminal. It can be disabled using the option -T with the given command.

Executing multiline command using Heredoc

In Bash and other shells such as zsh, ksh, etc a Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command.

The syntax of writing heredoc is –

[COMMAND] <<[-] 'DELIMITER'
        HERE-DOCUMENT
DELIMITER

You can use any string as a delimiting identifier, the most commonly used are EOF or END.

Using Heredoc is one of the most convenient and easiest ways to execute multiple commands on a remote system over SSH.

ssh -T [email protected] <<'EOSSH'
VAR1=`pwd`
echo $VAR1

VAR2=$(uname -a)
echo $VAR2

EOSSH

You can see the output in the given image.

Conclusion

I hope now you understand how to execute commands remotely using ssh. Now if you have any query then write us in the comments below.

Leave a Comment