UP | HOME

Execute command over SSH a quick way

You can execute any command over SSH, and if you setup your SSH key for the SSH access, it would be more safe and quicker. For example, I have a dokku command that I run regularly over SSH.

First, add your public key to a remote server with:

cat ~/.ssh/id_rsa.pub | ssh <user>@<server> 'cat >> ~/.ssh/authorized_keys'

Then, write a wrapper for your command, I usually put the -remote suffix for remote commands like this:

/usr/local/bin/<command>-remote

#!/bin/bash
ssh -t <user>@<server> <command> $1

In case it's dokku:

/usr/local/bin/dokku-remote

#!/bin/bash
ssh -t <user>@<server> dokku $1

From now on, every time I want to run dokku command, I just run it locally, for example, create a new app on my dokku server:

dokku-remote apps:create awesome-app

Date: 2019-02-01 Fri 00:00

Author: Huy Tran

Created: 2019-04-22 Mon 14:41