Linux: Run Multiple Commands via SSH

This post will guide you how to execute multiple commands via SSH in Linux. How do I execute multiple commands on the remote system using SSH tool under Linux operating system. How to ssh to remote host and run multiple commands on the remote server.

Run Multiple Commands via SSH


If you want to run multiple commands on a remote machine via ssh, you just need to see the syntax as followss:

# ssh user@remote_server "command1;command2"

or

# ssh user@remote_server "command1 && command2"

For example, run multiple command date and ifconfig in the remote host, type:

# ssh root@192.168.1.23 "date && ifconfig"

Outputs:

[root@os_x8664 ~]# ssh root@192.168.1.23 "date && ifconfig"
root@192.168.1.23's password:
Fri Sep 28 14:27:18 CST 2018
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.23 netmask 255.255.252.0 broadcast 192.168.1.255
inet6 fe80::210:e0ff:fe62:3914 prefixlen 64 scopeid 0x20<link>
ether 00:10:e0:62:39:14 txqueuelen 1000 (Ethernet)
RX packets 984418 bytes 567945529 (541.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 210886 bytes 17977201 (17.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

You can also use the redirection character to run multiple command in SSH session, type:

#ssh root@192.168.1.23 << EOF
date
ifconfig
hostname
EOF

Run Shell script in the Remote Host


You can also run a shell script on the remote host with SSH, and you just need to provide the absolute path of local shell script to SSH command.

Assuming that you have created a shell script called fio.sh with the following contents.

#!bin/bash
date
ifconfig
hostname

and you need to make the script executable and run it on the remote server over ssh command, type the following command:

# chmod u+x fio.sh
# ssh root@192.168.1.23 ./fio.sh

 

You might also like:

Sidebar



back to top