How to Run Multiple Commands Through SSH Command in Linux

This post will guide you how to run multiple commands through SSH command on the same shell session in your Linux system. How do I run multiple command on the remote Linux server using ssh command.

Run Multiple Commands Through SSH Command in Linux1

SSH Command


SSH (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to provide secure encrypted communications between two untrusted hosts over an insecure network. X11 connections, arbitrary TCP ports and UNIX-domain sockets can also be forwarded over the secure channel.

The syntax of the SSH command is as follows:

ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J destination] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] destination [command]

Run Multiple Commands Through SSH Command


If you want to run multiple commands on the remote Linux server through SSH Command on your Linux terminal, and you can use the below command:

$ ssh username@remoteServer "command1 && command2"

For example, you want to run two commands “date” and “df -h” on the remote Linux server(192.168.3.58), just type:

$ ssh root@192.168.3.58 "date && df -h"

outputs:

[devops@mydevops ~]$ ssh root@192.168.3.58 "date && df -h"
The authenticity of host '192.168.3.58 (192.168.3.58)' can't be established.
ECDSA key fingerprint is SHA256:U9vvdfLOGB1u/AREBn6H2HRgIXN28QLmef7yPvHmy0E.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.58' (ECDSA) to the list of known hosts.
root@192.168.3.58's password:
Mon Oct 14 03:42:40 EDT 2019
Filesystem Size Used Avail Use% Mounted on
devtmpfs 649M 0 649M 0% /dev
tmpfs 663M 0 663M 0% /dev/shm
tmpfs 663M 9.1M 654M 2% /run
tmpfs 663M 0 663M 0% /sys/fs/cgroup
/dev/mapper/cl_mydevops-root 41G 4.8G 36G 12% /
/dev/sda1 976M 184M 726M 21% /boot
/dev/mapper/cl_mydevops-home 20G 406M 20G 3% /home
tmpfs 133M 28K 133M 1% /run/user/42
tmpfs 133M 4.0K 133M 1% /run/user/1000
tmpfs 133M 4.0K 133M 1% /run/user/0
[devops@mydevops ~]$

You can also use HERE document syntax to run multiple commands through SSH command, type:

$ ssh root@192.168.3.58 << HERE
uptime
uname -a
HERE

Or

$ ssh root@192.168.3.58 <<EOF
uptime
uname -a
EOF

Outputs:

[devops@mydevops ~]$ ssh root@192.168.3.58 << HERE
> uptime
> uname -a
> HERE
Pseudo-terminal will not be allocated because stdin is not a terminal.
root@192.168.3.58's password:
Activate the web console with: systemctl enable --now cockpit.socket

03:49:18 up 20 min, 1 user, load average: 0.00, 0.00, 0.04
Linux mydevops.com 4.18.0-80.7.1.el8_0.x86_64 #1 SMP Sat Aug 3 15:14:00 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

[devops@mydevops ~]$ ssh root@192.168.3.58 <<EOF
> uptime
> uname -a
> EOF
Pseudo-terminal will not be allocated because stdin is not a terminal.
root@192.168.3.58's password:
Activate the web console with: systemctl enable --now cockpit.socket

03:50:11 up 20 min, 1 user, load average: 0.00, 0.00, 0.03
Linux mydevops.com 4.18.0-80.7.1.el8_0.x86_64 #1 SMP Sat Aug 3 15:14:00 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

There is another method to run multiple commands through SSH Command on the remote Linux server. and you can put commands into a file named clist.txt(one command per line), and then pass the content of this file to SSH command through pipe line, like below:

$ cat clist.txt
uptime
uname -a
free -h
$ cat clist.txt | ssh root@192.168.3.58

Outputs:

[devops@mydevops ~]$ cat clist.txt | ssh root@192.168.3.58
Pseudo-terminal will not be allocated because stdin is not a terminal.
root@192.168.3.58's password:
Activate the web console with: systemctl enable --now cockpit.socket

04:00:41 up 31 min, 1 user, load average: 0.00, 0.00, 0.00
Linux mydevops.com 4.18.0-80.7.1.el8_0.x86_64 #1 SMP Sat Aug 3 15:14:00 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
total used free shared buff/cache available
Mem: 1.3Gi 497Mi 401Mi 9.0Mi 426Mi 673Mi
Swap: 3.2Gi 0B 3.2Gi

Conclusion


You should know that how to run multiple commands on the remote server through ssh command in your CentOS/RHEL/Ubuntu Linux.

You might also like:

Sidebar



back to top