DEV Community

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

Posted on

13 5

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.

Requirements

  • 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)

Process

Open up a terminal...

Type the ssh connection command to ensure we have access

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

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

exit [PRESS ENTER]

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"'

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.

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (4)

Collapse
 
theinfradev profile image
TheInfraDev •

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

Collapse
 
david_j_eddy profile image
David J Eddy •

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

Collapse
 
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... :)

Collapse
 
david_j_eddy profile image
David J Eddy •

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more