ssh

How to Execute Linux Commands on Remote System over SSH?

To work on the remote system, first, you log in to that system, execute commands to perform different tasks, and then logout from that session. Even to run a single command on a remote system, you need to pass through all steps. While working on the local and remote systems side by side, you need to switch between them multiple times. Logging in and out from a system multiple times can be annoying. There should be a way that allows you to execute commands on a remote system without logging in to it.Luckily, there is a way we can show you how to execute commands on a remote system over SSH right from your local machine.

Prerequisites

As prerequisites, you must have:

  • Ubuntu machine
  • User with sudo privileges

Note: The commands mentioned in this tutorial have been executed on Ubuntu 20.04 LTS (Focal Fossa) Terminal, which can be opened through the Ctrl+Alt+T shortcut. The same commands are also applicable for Linux Mint and Debian.

Execute Linux Commands on Remote System over SSH

To execute a command on a remote system from your local system, use the following syntax:

 $ ssh <User_name@Hostname/IP_Address > <Command/Script>

Where user_name is the user on the remote system, hostname/IP_Address is the hostname or the IP address of the remote system. The Command/Script is the command or script you want to run on the remote system.

If the SSH is running on some port other than the default port, make sure to mention it using the -p flag as follows:

 $ ssh -p <port-number> <User_name@Hostname/IP_Address > <Command/Script>

Execute Single Command on Remote System

Let’s say you need to find the hostname of the remote system. In this case, the command you need to run on the local system would be:

 $ ssh <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a> hostname

Now, you will be asked to provide the password for the remote user. Type the password and press Enter.

In the following output, “mypc” is the hostname of the remote system.


You can also enclose the command in a single (‘’) or double inverted commas (“”) like this:

 $ ssh <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>hostname

Execute Multiple Commands on Remote System

If you need to execute multiple commands on the remote system over SSH, separate them using (;) or (&&) and enclose them in inverted commas (“”). If you do not put the multiple commands in inverted commas, then only the first command will be executed on the remote machine, and the other commands will be executed on the local machine.

 $ ssh <User_name@Hostname/IP_Address >  “command1 && command2”

or

 $ ssh <User_name@Hostname/IP_Address >  “command1 ; command2”

Using the && separator

If you use the (&&) separator, the second command will run only if the first command is executed successfully. For instance, to run “mkdir” and “ls” commands on the remote system, you will need to issue the following command on your local system:

 $ ssh -t <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>mkdir myfiles && ls

Now, you will be asked to provide the password for the remote user. Type the password and press Enter.

Following is the output of the commands executed on a remote system. The second command “ls” will only be executed on the remote system if the first command “mkdir” succeeds.


This command is also useful when upgrading the system. Generally, a system upgrade is performed after an update. Therefore, using the above command, you can make the system upgrade to run only if the updates succeed.

Using the ; separator

If you use the (;) separator, the second command will run no matter if the first command was successful or not. For instance, to run “hostname” and “ip r” commands on the remote system, you will need to issue the following command on your local system:

 $ ssh <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>hostname ; ip r”

Now, you will be asked to provide the password for the remote user. Type the password and press Enter.

Following is the output of the commands executed on a remote system. The second command “ip r” will be executed even if the first command is unsuccessful.

Execute Command on Remote System with Sudo Privileges

Some commands on Linux requires elevated privileges like to install or remove any software, to add or remove a user, changing system configurations, etc. When you execute a command on the remote systems that require sudo privileges, you will be first asked for the password for the remote user, and then you will be asked for the sudo password to execute the command.

For instance, to update system repositories on the remote system, you will need to run the following command on your local system:

 $ ssh -t <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>sudo apt update”

Following is the output of the command executed on a remote system with elevated privileges.


Note: Then -t option is used here to forcefully allocate tty.

Execute Command on Remote System and Save its Output to Local System

The output of the commands executed on the remote systems can also be saved to the local system. For instance, the following command will execute the ifconfig command on the remote system, and then it will save its output to a file named ip.txt on the local machine’s desktop.

 $ ssh -t <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>ifconfig> ~/Desktop/ip.txt

Execute Local Script on Remote System

The script placed on the local system can also be executed on the remote system. Let’s explain this by creating a script named sample.sh in the local system.

 $ sudo nano sample.sh

Add below lines in the script file:

 #!/bin/bash

date

hostname

ip route

ping -c 3 google.com

Now, save and close the script.


Now, to execute the script sample.sh on the remote system, issue the below command in Terminal:

 $ ssh -t <a href="mailto:tin@192.168.72.148">tin@192.168.72.148</a>bash -s; < sample.sh

Here is the output after the script is executed on a remote system:


That is all there is to it! In this tutorial, we have shown you how to execute commands on a remote system over SSH. It makes working on the remote system very easy and also saves you from the hassle of logging in and out from the remote system multiple times.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.