How to Run Command Using SSH On Remote Machine?

Harish Kumar · · 4745 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:

ssh user@server ls

Executing multiple commands, inlined, isolated with ;

ssh user@server ls; pwd; apt update

Executing a command with sudo

ssh user@server 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 user@server 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 user@server '
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 user@server 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 user@server < 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 user@server << 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 user@server <<'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 user@server <<'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:

How to Grant Passwordless `Sudo` for Specific Commands on Linux 🚀

Managing a self-hosted server or Virtual Private Server (VPS) often involves repetitive administrative tasks. Continuously entering your sudo password can quickly become tedious. (...)
Harish Kumar

Essential Linux Command Line Cheatsheets for Beginners and Advanced Users

The Linux command line is a powerful tool for managing your system efficiently. Whether you're a beginner exploring the terminal or an advanced user handling complex tasks, cheatsheets (...)
Harish Kumar

Understanding Linux File Permissions and Ownership

Linux file permissions are a critical cornerstone in the architecture of Linux systems, serving as a fundamental aspect of their security model. They meticulously define who can (...)
Harish Kumar

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