How To Run Multiple SSH Command On Remote Machine And Exit Safely

in Categories , , , , , , , , , last updated February 20, 2018

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?

There are various ways to run multiple commands on a remote Unix server. 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"
Run date and hostname commands:
$ ssh user@host "date && hostname"
You can run sudo command as follows on a remote box called
$ ssh -t "sudo /sbin/shutdown -h now"
And, finally:
$ ssh "sync && sync && /sbin/shutdown -h now"
Run Multiple SSH Command On Remote Linux or Unix Machine

Bash here document syntax

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 << EOF
 cat /etc/resolv.conf

With sudo command type:

ssh -t << 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

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

A better option is to use the ansible tool.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 10 comments/add one below):

Start the discussion at

Historical Comment Archive

10 comment

  1. 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.
  2. 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
  3. For more complicated tasks use Ansible.

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

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

    Have a question? Post it on our forum!