Howto run and execute a command using SSH on a remote machine

undefined

In this mini post, I’ll show you how to  run and execute a command using ssh under UNIX, OS X, *BSD, and Linux operating systems on a remote machine. The SSH client program can be used for logging into a remote machine or server and for executing commands on a remote machine without logging into the remote server. When a command is specified, it is executed on the remote host/server instead of a login shell. The syntax is as follows for executing commands over ssh:

 

ssh user@<remote_server> 'command1'
ssh user@<remote_server> 'command2'
ssh user@<remote_server> 'command1 | command2'

Replace the user with the existing user on the remote server and replace the <remote_server> with your server IP or fqdn hostname. The ssh client will login to the remote server  and run the command you specified.

Here’s some examples on running commands over ssh:

  • Get remote server date and time:
ssh user@remote_server date
  • Find out remote server disk space usage:
ssh user@remote_server 'df -H'
  •  Find out remote server kernel version and Linux distro names:
ssh user@remote_server 'uname -mrs'

OR

ssh user@remote_server 'lsb_release -a'
  • Run a remote script called /scripts/backup.sh to perform backup:
ssh user@remote_server '/scripts/backup.sh'

Here, the backup script must exist on the remote server and is executable, we must use the full path of the remote script.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *