Run Commands on a Remote Machine via SSH
Tags: devops, howto, terminal, ssh
Published: 2018-11-30 03:42:44

---
title: Run Commands on a Remote Machine via SSH
published: true
description: 
tags: #devops, #howto, #terminal, #ssh
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2018/11/EtheriumHAckerTerminal.png
---

{% youtube T_-pm_ur8jg %}

<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">

<!-- /wp:core-embed/youtube -->

<!-- wp:heading -->
<h2>What's the Point?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>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.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Requirements</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Basic terminal usage</li><li>Linux local machine (or WSL for you Windows people)</li><li>SSH client on local machine, SSH server on remote machine (<a href="https://en.wikipedia.org/wiki/OpenSSH">OpenSSH </a>is a popular one)</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Process</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Open up a terminal...</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1238,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/remote_command_via_ssh_1.png" alt="" class="wp-image-1238"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Type the ssh connection command to ensure we have access</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">ssh -i ./.ssh/my-key.pem ubuntu@54.147.254.33</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1239,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/remote_command_via_ssh_2.png" alt="" class="wp-image-1239"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Ok, looks like we can connect successfully. Lets exit now in preparation for the next step.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">exit [PRESS ENTER]</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1240,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/remote_command_via_ssh_3.png" alt="" class="wp-image-1240"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now let's wrap a 'Hello World' echo command inside the SSH execution.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">ssh -i ./.ssh/my-key.pem ubuntu@54.147.254.33 'echo "Hello World"'</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1241,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/remote_command_via_ssh_4.png" alt="" class="wp-image-1241"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1242} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/remote_command_via_ssh_5.png" alt="" class="wp-image-1242"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>...and it is present! Nice.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Additional Resources</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><a href="https://en.wikipedia.org/wiki/Secure_Shell">https://en.wikipedia.org/wiki/Secure_Shell</a></li><li><a href="https://stackoverflow.com/questions/18502945/how-to-execute-a-remote-command-over-ssh-with-arguments">https://stackoverflow.com/questions/18502945/how-to-execute-a-remote-command-over-ssh-with-arguments</a></li><li><a href="https://askubuntu.com/questions/282319/how-to-use-sshpass">https://askubuntu.com/questions/282319/how-to-use-sshpass</a></li><li><a href="https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/">https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/</a></li><li><a href="https://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-run-command-on-remote-machine-server/">https://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-run-command-on-remote-machine-server/</a><a href="https://en.wikipedia.org/wiki/Secure_Shell"></a></li></ul>
<!-- /wp:list -->