Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

How to run a command on the remote server using SSH command?

Posted on July 9, 2021July 9, 2021 by admin

Overview

Using the ‘ssh’ command with the ‘-t’ parameter, we can run commands on a remote server.

Syntax:

ssh user@server – t “Command to be executed at server”

Examples:

#Running chef-solo on remote server

CHEF_TARGET = "ssh -v ec2-user@ec2-52-24-147-93.us-west-2.compute.amazonaws.com"
CHEF_ROLE="my-app"
ssh ec2-user@ec2-55-124-347-93.us-west-2.compute.amazonaws.com -t "sudo chef-solo --node-name $CHEF_TARGET -o role[$CHEF_ROLE]"
#Performing list command on remote server
ssh  ec2-user@10.24.256.11 - t "ls -la"
#Creating a directory at remote server
ssh  -i pemfile.pem ec2-user@10.24.256.11 - t "sudo mkdir -p /root/home/test"

Note: – For authentication on the server, you can use the following two ways.

  • Public-Private keys based authentication
  • Password-based authentication
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme