DEV Community

Cover image for Run Commands on a Remote Machine via SSH
David J Eddy
David J Eddy

Posted on

Run Commands on a Remote Machine via SSH

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.


  • Basic terminal usage
  • Linux local machine (or WSL for you Windows people)
  • SSH client on local machine, SSH server on remote machine (OpenSSH is a popular one)


Open up a terminal...

Type the ssh connection command to ensure we have access

ssh -i ./.ssh/my-key.pem ubuntu@

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


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

ssh -i ./.ssh/my-key.pem ubuntu@ 'echo "Hello World"'

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.

...and it is present! Nice.


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

Top comments (4)

theinfradev profile image

Ah cool, I really need to rewrite a few scripts now!

david_j_eddy profile image
David J Eddy

Happy it was helpful. Feel free to reach out to me if you have questions.

pbouillon profile image
Pierre Bouillon

Great article, thank you, it's a lot clearer for me !

Reminds me of an old script I have... I may use ssh a lot more in my school... :)

david_j_eddy profile image
David J Eddy

Haha! Your script reminds me of the 'administrative' tool called Sub7.