Running a Shell Script on a Remote Machine Through SSH

SSH (Secure Shell) is a network protocol that allows secure remote access to Linux-based machines. It enables users to execute commands and run shell scripts on remote servers without physically accessing them. The SSH protocol encrypts all data transmission between client and server, ensuring secure communication over potentially unsafe networks.

What is SSH

SSH stands for Secure Shell or Secure Socket Shell. It provides a secure channel for accessing remote servers by encrypting all data transmitted between the client and host. SSH operates on TCP port 22 by default and replaces insecure protocols like Telnet and rlogin.

Running Single Commands over SSH

SSH allows you to execute individual commands on remote machines without establishing an interactive session. The basic syntax is:

ssh username@hostname command

Example Getting Remote System Date

$ ssh webmaster@172.17.0.2 date
Mon June 15 08:55:40 IST 2022

Example Checking Disk Space Usage

$ ssh webmaster@172.17.0.2 'df -h'
Filesystem     Size  Used   Avail   Use%   Mounted on
overlay        875G   24G    807G    3%      /
tmpfs          64M     0     64M     0%      /dev
shm            64M     0     64M     0%      /dev/shm
/dev/nvme0n1p3 875G   24G    807G    3%     /home/cg/root
tmpfs          63G     0     63G     0%     /proc/acpi

Running Shell Scripts Remotely

You can execute local shell scripts on remote machines by specifying the script path after the SSH connection command. First, create a shell script on the remote machine.

Creating a System Information Script

Create a script named sys-info.sh with the following content:

#!/bin/bash
echo "User: $(whoami)"
echo "Time: $(date)"
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime)"

Making the Script Executable and Running It

$ chmod +x sys-info.sh
$ ssh webmaster@172.17.0.2 './sys-info.sh'
User: webmaster
Time: Tue Jan 28 07:18:55 IST 2022
Hostname: ubuntu-server
Uptime: 07:18:55 up 2 days, 14:32, 1 user, load average: 0.00, 0.01, 0.05

Running Local Scripts on Remote Machines

You can also execute a local script on a remote machine by piping it through SSH:

$ ssh webmaster@172.17.0.2 'bash -s' < local-script.sh

Alternatively, you can copy the script to the remote machine and then execute it:

$ scp local-script.sh webmaster@172.17.0.2:/tmp/
$ ssh webmaster@172.17.0.2 'chmod +x /tmp/local-script.sh && /tmp/local-script.sh'

Key Points

  • SSH commands are case-sensitive on Linux systems

  • Use single quotes around commands with special characters or pipes

  • Scripts must have executable permissions (chmod +x) before execution

  • SSH sessions terminate automatically after command completion

  • Use scp to transfer files before executing them remotely

Conclusion

SSH provides a secure and efficient way to execute commands and shell scripts on remote machines. Whether running single commands like date or complex shell scripts, SSH ensures encrypted communication while maintaining ease of use. This capability is essential for system administrators managing multiple remote servers and automating tasks across distributed systems.

Updated on: 2026-03-17T09:01:38+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements