How to run a command on a remote server over SSH - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to run a command on a remote server over SSH
================================================

### [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [Running a command without logging in](#content-running-a-command-without-logging-in)
2. [Quoting matters](#content-quoting-matters)
3. [Running multiple commands](#content-running-multiple-commands)
4. [Using sudo over SSH](#content-using-sudo-over-ssh)
5. [Capturing output locally](#content-capturing-output-locally)
6. [Running a local script on the server](#content-running-a-local-script-on-the-server)

[\#](#content-running-a-command-without-logging-in "Permalink")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](/connect-to-server-ssh-command), this works out of the box.

The pattern is `ssh user@host 'command'`:

 ```
ssh deploy@example.com 'uptime'

```

[\#](#content-quoting-matters "Permalink")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.

[\#](#content-running-multiple-commands "Permalink")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'

```

[\#](#content-using-sudo-over-ssh "Permalink")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.

[\#](#content-capturing-output-locally "Permalink")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'

```

[\#](#content-running-a-local-script-on-the-server "Permalink")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'
