Table of Contents
Remote access and automation are critical in modern system administration. SSH (Secure Shell) allows you to connect securely to remote machines, making it easy to run tasks or scripts from anywhere.
This guide will show you how to run a shell script on a remote machine using SSH. You’ll learn how to automate tasks, transfer scripts, and troubleshoot common issues.
Why Use SSH for Running Shell Scripts?
Running shell scripts on a remote machine is useful for:
- Automating tasks like backups, system updates, or deployments.
- Managing remote servers without physically accessing them.
- Centralizing workflows, making it easy to manage multiple machines from a single location.
Setting up SSH Key-Based Authentication
To avoid entering your password every time, use SSH keys:
1. Generate SSH Key Pair:
ssh-keygen -t rsa -b 4096
Output:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/your_username/.ssh/id_rsa):
2. Copy the Public Key to the Remote Server:
ssh-copy-id user@remote-server
Once this is set up, you can log in without a password.
Writing and Preparing the Shell Script
Start by creating your shell script on your local machine. For example, a simple script like backup.sh could look like this:
#!/bin/bash
# Backup script to archive home directory
tar -czf /backup/home_backup.tar.gz /home/user/
Make the script executable:
chmod +x backup.sh
Running a Shell Script on a Remote Machine via SSH
To run a shell script on a remote machine directly through SSH, use this command:
ssh user@remote-server 'bash -s' < ./backup.sh
Output:
Compressing /home/user/ directory...
Backup saved at /backup/home_backup.tar.gz
Here’s a breakdown:
- user@remote-server: The username and remote host.
- ‘bash –s‘: Tells the remote machine to interpret the script as standard input.
- < ./backup.sh: Sends the script through SSH for execution.
Transferring and Running Shell Scripts with SCP
Another way to run a script is to transfer it first and then run it. You can use scp to copy the script to the remote server.
Step 1: Copy the Script to the Remote Machine
scp ./backup.sh user@remote-server:/home/user/
Output:
backup.sh 100% 234 12KB/s 00:00
Step 2: Run the Script on the Remote Machine
ssh user@remote-server 'bash /home/user/backup.sh'
Automating Remote Script Execution with SSH Config and Bash Aliases
You can set up an SSH config file or a Bash alias to simplify the process.
Setting up an SSH Config File
Create or edit the ~/.ssh/config file and add the following:
Host remote-alias
HostName remote-user
User user
Now, you can use the alias remote-alias to log in:
ssh remote-alias
Creating a Bash Alias
You can also create a Bash alias to simplify running the script:
alias run_backup="ssh user@remote-server 'bash /home/user/backup.sh'"
Now, simply type run_backup to execute the script.
Conclusion
Running shell scripts on remote machines via SSH simplifies automation and system management. You can either run the script directly using SSH or transfer it and execute it later. Use SSH config and Bash aliases to streamline repetitive tasks. Keep experimenting with more complex scripts to automate your workflow on VPS hosting from Atlantic.Net!