How to Run Command Using SSH On Remote Machine?

Harish Kumar · · 481 Views

There are different ways to run multiple commands on a remote Unix server using SSH. This article shows the most straightforward and easy approach to SSH and runs multiple commands in using the bash shell.

Single-line command

Executing a single command:

Executing multiple commands, inlined, isolated with ;

ssh [email protected] ls; pwd; apt update

Executing a command with sudo

ssh [email protected] sudo apt update
sudo: no tty present and no askpass program specified

sudo requires interactive shell, it can be enabled with -t option.

ssh -t [email protected] sudo ls /root
[sudo] password for user:

Multi-line command with variables expansion

Let's say you have a variable VAR1 that you want to run on the remote server. For example, let's consider the following snippet:

VAR1="Variable 1"
ssh [email protected] '
ls
pwd
if true; then
    echo "True"
    echo $VAR1      # <-- it won't work
else
    echo "False"
fi
'

Here SSH will not allow us to run that variable with this method. To make variables expansion work, use bash -c.

VAR1="Variable 1"
ssh [email protected] bash -c "'
ls
pwd
if true; then
    echo $VAR1
else
    echo "False"
fi
'"

Multi-line command from local script

Use stdin redirection to run a local script using SSH.  Let's say you have a script.sh, and it has the following commands.

ls
pwd
hostname

Now to run it using SSH, use the following snippet:

ssh [email protected] < script.sh

Multi-line command using Heredoc

Heredoc is presumably the most helpful approach to execute multi-line commands on a remote machine. Likewise, variables extension works out-of-the-box.

VAR1="Variable 1"
ssh -T [email protected] << EOSSH
ls
pwd
if true; then
  echo $VAR1
else
  echo "False"
fi
EOSSH

If you need to assign variables inside the heredoc block, put the opening heredoc in single quotes.

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

VAR2=$(uname -a)
echo $VAR2

EOSSH

Using the above snippet, you may get this below warning message:

Pseudo-terminal will not be allocated because stdin is not a terminal.

You can disable by adding -T parameter to the SSH command.

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

VAR2=$(uname -a)
echo $VAR2

EOSSH
0

Please login or create new account to add your comment.

0 comments
You may also like:

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

10 Things to Do After Installing Ubuntu Operating System

In this article, I will show you 40 things you can do after installing Ubuntu on your system. This isn't restricted to a specific version of Ubuntu; you can follow these on any (...)
Harish Kumar

Ubuntu Installation step by step guide with disk partitioning

Ubuntu is the most loved OS for many desktop users, particularly for developers. Canonical releases new Ubuntu versions every six months with free support for nine months and every (...)
Harish Kumar

How to Write/Append Multiple Lines to a File from terminal?

Sometimes you might be needed to write or append some text to a file from the terminal. You can use different strategies to write multiple lines to a record file through the command (...)
Harish Kumar

How to install OpenSSH server on Ubuntu Linux?

sshd (OpenSSH Daemon) is the daemon program for SSH client. SSH replaces unreliable rlogin and rsh, and give securely encrypted communications between two untrusted hosts over (...)
Harish Kumar