Run ssh and immediately execute command
Categories:
Executing Commands Remotely with SSH: A Comprehensive Guide

Learn how to use SSH to connect to a remote server and immediately execute commands, enhancing automation and scripting capabilities for system administration and development.
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Beyond interactive shell access, one of its most powerful features is the ability to execute commands remotely without establishing a full interactive session. This article will explore the syntax, common use cases, and best practices for running commands via SSH.
The Basic Syntax: SSH and Remote Commands
The fundamental way to execute a command on a remote server via SSH is to append the command directly after the SSH connection details. SSH will establish the connection, run the specified command, and then terminate the connection (or return to your local shell if an interactive session was requested, though not in this specific use case).
ssh user@remote_host 'ls -l /tmp'
This command connects to remote_host as user and executes ls -l /tmp.
When you execute a command this way, the remote server's output (stdout and stderr) is typically redirected back to your local terminal. This makes it incredibly useful for scripting and automation, as you can capture the output for further processing.
|, redirection angle, or semicolons ;) are interpreted by the remote shell, not your local shell.Advanced Command Execution and Scripting
For more complex operations, you might need to execute multiple commands, use pipes, or even run entire scripts remotely. SSH handles these scenarios gracefully.
ssh user@remote_host 'cd /var/www && git pull && systemctl restart apache2'
Multiple commands chained with && for sequential execution.
cat local_file.txt | ssh user@remote_host 'cat > remote_file.txt'
Pipes the content of local_file.txt to remote_file.txt on the server.
You can also execute a local script directly on a remote machine without first copying it. This is done by piping the script's content to the SSH command, which then executes it using the remote shell.
ssh user@remote_host < local_script.sh
Executes local_script.sh on the remote host.
Key Considerations for Automation
When automating tasks with remote SSH commands, several factors are crucial for reliability and security.

Workflow for executing a remote command via SSH.
SSH Keys for Passwordless Authentication
For automation, using SSH keys is paramount. It allows you to connect and execute commands without manual password entry, which is essential for scripts that run unattended.
Non-Interactive Sessions
When executing commands this way, SSH operates in a non-interactive mode. This means no pseudo-terminal is allocated, and commands expecting user input will typically fail or hang. Ensure your remote commands are designed to run without interaction.
Environment Variables
Remote commands will execute within the shell environment of the remote user. Be aware of differences in PATH or other environment variables compared to an interactive session. You might need to explicitly set variables or use full paths to executables.
ssh -i ~/.ssh/my_automation_key user@remote_host 'do_automated_task.sh'
Specifying a private key for authentication.
1. Step 1
Generate an SSH key pair on your local machine if you haven't already: ssh-keygen -t rsa -b 4096
2. Step 2
Copy your public key to the remote server: ssh-copy-id user@remote_host (or manually add it to ~/.ssh/authorized_keys)
3. Step 3
Test passwordless login: ssh user@remote_host 'echo "Hello from remote!"'
4. Step 4
Integrate these commands into your automation scripts or CI/CD pipelines.