Run Commands on a Remote Machine via SSH

Pheagey
Pheagey
Nov 30, 2018 · 3 min read
Image for post
Image for post

Ethereum Hacker Terminal — YouTube

What’s the Point?

Executing commands is one of the core tenants of computing. Sometimes, th desire to execute a command on a remove machine arises. Think ‘hey remote computer, run updates’ or ‘wake up office computer, I need a file’. Remotely connecting to, and executing commands on, remote machines is imperative to computing. Even more so the internet net activities. In this article we review how to connect via SSH (Secure Shell) to a Linux (or Unix) remote machine and run a command.

  • Side note, this article does NOT cover how to setup public/private key pairs, shell account creation, etc. The focus centrally how to pass a a command via SSH to a remote machine.

Requirements

  • Basic terminal usage

Process

Open up a terminal…

Image for post
Image for post

Type the ssh connection command to ensure we have access

ssh -i ./.ssh/my-key.pem ubuntu@54.147.254.33
Image for post
Image for post

Ok, looks like we can connect successfully. Lets exit now in preparation for the next step.

exit [PRESS ENTER]
Image for post
Image for post

Now let’s wrap a ‘Hello World’ echo command inside the SSH execution.

ssh -i ./.ssh/my-key.pem ubuntu@54.147.254.33 'echo "Hello World"'
Image for post
Image for post

Look at that! The command executed on the remote machine. To prove it press the up arrow on your keyboard. You will notice the most recent command is not ‘echo “Hello World”’, rather the parent SSH command just executed. To double check this, lets create a file on the remote machine, then log in to see if it is indeed present.

Image for post
Image for post

…and it is present! Nice.

Conclusion

Yes, it really is that simple. Being so simple, and knowing the ability exists, what are some of the possible uses you can think of? Do you know of a process that requires manual steps on a remote machine to complete? If so, here si the key to making a time consuming process a one line command.

Additional Resources


Originally published at David J Eddy.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store