I have a backup sync program on local server. I have an ssh password less login set up, and I can run commands on an external server in bash script doing:
ssh root@server2 "sync; sync; /sbin/shutdown -h now"
How do I run multiple commands in bash on a remote Unix or Linux server? What is the best way to SSH in and run various unix commands in a bash shell?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements bash+ssh
Est. reading time 2m
[/donotprint]There are various ways to run multiple commands on a remote Unix server. This page show the easiest way to ssh and run multiple commands in using bash shell. The syntax is as follows.

Simple bash syntax to run multiple commands on remote machine

Simply run command2 if command1 successful on a remote host called foo:
$ ssh bar@foo "command1 && command2"
For example, run uptime and date commands on a box named ‘centos7’ as ‘vivek’ user::
ssh vivek@centos7 "uptime && date"
Sample outputs:

 18:36:17 up 1 day,  8:14,  0 users,  load average: 1.02, 1.06, 1.05
Fri Jun  7 18:36:17 IST 2019

How To Run Multiple SSH Command

Run date and hostname commands:
$ ssh user@host "date && hostname"
You can run sudo command as follows on a remote box called server1.cyberciti.biz:
$ ssh -t vivek@server1.cyberciti.biz "sudo /sbin/shutdown -h now"
And, finally:
$ ssh root@server1.cyberciti.biz "sync && sync && /sbin/shutdown -h now"
How To Run Multiple SSH Command On Remote Linux or Unix Machine

Bash here document syntax to run multiple SSH command

The here document syntax tells the shell to read input from the current source (HERE) until a line containing only word (HERE) is seen:

ssh server1 << HERE

Here is another example to run a multiple commands on a remote system:

ssh vivek@server1.cyberciti.biz << EOF
 cat /etc/resolv.conf

With sudo command type:

ssh -t vivek@server1.cyberciti.biz << EOF
 sudo /sbin/shutdown -h 0

Pipe script to a remote server

Finally, create a script called “remote-box-commands.bash” as follows on your local server:

cat /etc/resolv.conf

Run it as follows on a remote server called nas02nixcrafthomeserver:
$ cat remote-box-commands.bash | ssh user@nas02nixcrafthomeserver
cat remote-box-commands.bash | ssh -t vivek@nas02nixcrafthomeserver

How to ssh to multiple servers and run commands

One can use simple bash for loop as follows. In this example, run uptime command on three Linux server named box1, box2, and box3:

for s in box1 box2 box3
   ssh vivek@${s} uptime

Running local bash script on remote system

Create a shell script as follows:

# Name: test.sh 
# Purpose: Run multiple commands on a remote box 
# ----------------------------------------------------

Now run it as follows on host named server1.cyberciti.biz:
ssh vivek@server1.cyberciti.biz 'bash -s' < /path/to/test.sh
OR run it on four servers:

for s in server{1..4}.cyberciti.biz
   ssh vivek@${s} 'bash -s' < /home/vivek/bin/test.sh

Please note that the -t option will get rid of "Sorry, you must have a tty to run sudo/insert your-command-here" error message. You can also use pssh (parallel ssh) for executing ssh in parallel on a number of Linux/Unix/BSD servers. For more info see your ssh and bash command man page:
$ man ssh
$ man bash
$ man sudo

And there you have it, multiple bash shell command run using nothing but ssh client/sever model. A better option is to use the ansible tool.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • deepak Sep 17, 2015 @ 4:35

    SUDO will not work inside EOF unless it is set up with the NOPASSWD option in sudoers, because your STDIN is not a standard terminal.

    The correct format would be.

    ssh server sudo -S shutdown <<EOF
    -S will instruct sudo to get the password from STDIN (in this case the EOF) instead of a terminal. One disadvantage to this is your password will be captured in your session logs if it is set up. You can avoid that by running it inside a script.
    • carlitoregencia Mar 18, 2017 @ 1:41

      Just don’t forget to put newline at the end of the password

  • James Sep 17, 2015 @ 9:08

    For SSH connections where a password is needed, use sshpass.
    For example:

    SSHCOMMAND="ssh -p 22 -T -o StrictHostKeyChecking=no -o BatchMode=no"
    sshpass -p "$SSHPASS" $SSHCOMMAND $SSHACCOUNT << EOF_run_commands
  • Bruce Sep 17, 2015 @ 21:58

    For more complex tasks, put a script in a file, scp, ssh to execute the script , then remove the file

  • Ian T Sep 20, 2015 @ 7:11

    For more complicated tasks use Ansible.

    In fact even for non complicated tasks use Ansible; It is essentially Bash & SSH on steroids.

  • Mork Oct 22, 2015 @ 4:42

    Better way: https://code.google.com/p/parallel-ssh/

    pssh -h hosts.list -P apt-get update

  • Devrim baris acar Oct 22, 2015 @ 5:43

    Also you may check gnu parallel with –nonall option if you have more than one machine.

  • Kranthi Aug 22, 2016 @ 10:41

    Can I connect to multi remote host using SSH command in linux ? If not then how i connect to multi remote system using linux.?

  • Raja Gopalan Jun 17, 2017 @ 3:27

    Great site! Finding some answers and a lot more than i expected. Thank you! :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum