Knowledge
How to run a command on a remote server over SSH
#CommandLine
Run one-off commands on a remote server over SSH without an interactive session, including quoting, sudo, capturing output and running local scripts remotely.
Published by Mark van Eijk on June 23, 2026 · 1 minute read
- Running a command without logging in
- Quoting matters
- Running multiple commands
- Using sudo over SSH
- Capturing output locally
- Running a local script on the server
Running a command without logging in
Most of the time when I SSH into a server I just want to run a single command and get out. Instead of starting an interactive session, you can pass the command straight to ssh and it runs there, prints the output locally and disconnects. This builds on a working SSH setup, so if you can connect to the server over SSH, this works out of the box.
The pattern is ssh user@host 'command':
ssh deploy@example.com 'uptime'
Quoting matters
Always wrap the command in single quotes. Without them, your local shell may expand variables and globs before they ever reach the server. Compare these two:
ssh deploy@example.com 'echo $HOME' # prints the server's $HOME
ssh deploy@example.com "echo $HOME" # prints your local $HOME
Use single quotes when you want the remote shell to interpret the command.
Running multiple commands
Chain commands with && (stop on failure) or ; (always continue), all inside the same quotes:
ssh deploy@example.com 'cd /var/www/app && git pull && composer install'
Using sudo over SSH
Running sudo non-interactively needs a terminal for the password prompt. Add -t to force one to be allocated:
ssh -t deploy@example.com 'sudo systemctl restart nginx'
The -t flag gives sudo a proper TTY so it can ask for your password.
Capturing output locally
Because the output comes back over the connection, you can redirect or pipe it on your own machine like any other command:
ssh deploy@example.com 'cat /var/log/syslog' > server-syslog.txt
ssh deploy@example.com 'df -h' | grep '/dev/sda1'
Running a local script on the server
A neat trick: you can feed a script from your machine into a remote shell without copying it over first. Pipe it into bash -s:
ssh deploy@example.com 'bash -s' < ./provision.sh
The remote bash -s reads the script from standard input, which ssh pipes from your local file. You can even pass arguments to the script after bash -s:
ssh deploy@example.com 'bash -s' -- arg1 arg2 < ./provision.sh
This is great for ad-hoc provisioning or maintenance scripts you keep locally but want to run on several servers without uploading them each time.
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!
Related articles
Argument list too long (Bash: /bin/rm)
Run one-off commands on a remote server over SSH without an interactive session, including quoting, sudo, capturing output and running local scripts remotely.
How to install Composer packages locally
Run one-off commands on a remote server over SSH without an interactive session, including quoting, sudo, capturing output and running local scripts remotely.